Camera
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
The scene is rendered from a pinhole camera. CubeTextureView uses
irurueta-android-glutils to convert a PinholeCamera
from irurueta-geometry into the OpenGL model, view and
projection matrices.
All the properties in this page require the OpenGL surface to exist. Set them from
OnSurfaceChangedListener or later; otherwise an IllegalStateException is thrown. See
Waiting for the surface.
|
Pinhole camera model
A pinhole camera projects a 3D point into a 2D image point (in homogeneous coordinates) as
where
Setting the whole camera
import com.irurueta.android.glutils.OpenGlToCameraHelper
import com.irurueta.geometry.PinholeCamera
import com.irurueta.geometry.Point3D
import com.irurueta.geometry.Rotation3D
fun createCamera(width: Int, height: Int): PinholeCamera {
// OpenGL projection matrix, defined column-wise as in android.opengl.Matrix.
// These values come from an ARCore session on a Pixel 2 device.
val projectionMatrix = floatArrayOf(
2.8693345f, 0.0f, 0.0f, 0.0f,
0.0f, 1.5806589f, 0.0f, 0.0f,
-0.004545755f, 0.009158132f, -1.002002f, -1.0f,
0.0f, 0.0f, -0.2002002f, 0.0f
)
// Convert the projection matrix into pinhole camera intrinsic parameters
val intrinsics = OpenGlToCameraHelper.computePinholeCameraIntrinsicsAndReturnNew(
projectionMatrix,
width,
height
)
// Camera located at the origin with no rotation, looking towards the cube
return PinholeCamera(intrinsics, Rotation3D.create(), Point3D.create())
}
cubeView.camera = createCamera(width, height)
Setting camera may throw CameraException if the camera is numerically unstable (for instance, if its intrinsic
parameters cannot be decomposed).
Setting camera parameters individually
Intrinsic parameters, center and rotation can also be changed independently:
import com.irurueta.geometry.InhomogeneousPoint3D
import com.irurueta.geometry.PinholeCameraIntrinsicParameters
import com.irurueta.geometry.Quaternion
// Intrinsic parameters: focal lengths, principal point and skewness (in pixels)
cubeView.cameraIntrinsicParameters = PinholeCameraIntrinsicParameters(
1500.0, 1500.0, // horizontal and vertical focal length
0.0, 0.0, // horizontal and vertical principal point
0.0 // skewness
)
// Move the camera 0.5 units to the left of the origin
cubeView.cameraCenter = InhomogeneousPoint3D(-0.5, 0.0, 0.0)
// Rotate the camera 10 degrees around its vertical axis
cubeView.cameraRotation = Quaternion(0.0, Math.toRadians(10.0), 0.0)
Passing null to any of these setters throws IllegalArgumentException.
Clipping planes
Vertices nearer to the camera than the near plane, or further than the far plane, are not drawn. Defaults are
CubeRenderer.DEFAULT_NEAR_PLANE (0.1f) and CubeRenderer.DEFAULT_FAR_PLANE (1000.0f).
// Vertices nearer than 0.1 or further than 100 units from the camera are not drawn
cubeView.setNearFarPlanes(0.1f, 100.0f)
// Or set planes and camera at once, requesting a single frame
cubeView.setValues(0.1f, 100.0f, camera)
setValues(nearPlane, farPlane, camera) updates both clipping planes and the camera at once and requests a single
frame.
Camera in Android view coordinates
The camera property follows the usual 3D convention where the y axis points upwards. When the cube must be aligned
with Android view coordinates (where the y axis points downwards), for instance to overlay it on a camera preview,
set the display orientation first. Once the orientation is known (not ORIENTATION_UNKNOWN), the view camera
properties become available:
import com.irurueta.android.glutils.CameraToDisplayOrientation
// Display orientation (for instance, obtained from the device camera and display rotation).
// Once orientation is known, view camera properties become available.
cubeView.orientation = CameraToDisplayOrientation.ORIENTATION_0_DEGREES
// Camera expressed in Android view coordinates (y axis pointing downwards)
val viewCamera = cubeView.viewCamera
| Property | Description |
|---|---|
|
|
|
Camera expressed in view coordinates. Setting a non-null value while the orientation is unknown, or |
|
Individual parameters of the view camera. Setting them while the orientation is unknown throws
|
Next: Animation.