Cameras and OpenGL matrices

This documentation was generated with the assistance of AI. Please report any inaccuracies.

OpenGL describes the point of view with two 4x4 matrices: a projection matrix (the frustum: field of view, aspect ratio, near and far planes) and a model-view matrix (where the camera is and where it looks). Computer vision describes the same thing with a pinhole camera: intrinsic parameters (focal length, principal point, skewness), a rotation and a center.

The library converts between both, using PinholeCamera from irurueta-geometry. This is useful to render virtual content that matches a real, calibrated camera, for example in augmented reality.

All matrices are 16-length FloatArray instances in column-major order, the layout expected by android.opengl.Matrix and glUniformMatrix4fv.

CameraConverter

CameraConverter keeps a camera and its matrices in sync for a viewport size. Setting the camera (or its intrinsics, center or rotation) recomputes the matrices; setting a projection or model-view matrix recomputes the camera.

val converter = CameraConverter(width, height)
converter.isModelViewProjectionMatrixComputationEnabled = true
converter.setNearFarPlanes(0.1f, 100.0f)

// focal length (in pixels) and principal point (in the center of the viewport)
val focalLength = 1.5 * width
val intrinsics = PinholeCameraIntrinsicParameters(
    focalLength, focalLength, width / 2.0, height / 2.0, 0.0
)
// camera without rotation, placed 5 units away from the origin along the z axis
val rotation = Quaternion()
val center = InhomogeneousPoint3D(0.0, 0.0, 5.0)
converter.camera = PinholeCamera(intrinsics, rotation, center)

// 16-length arrays in column-major order, ready for glUniformMatrix4fv
val projectionMatrix = converter.projectionMatrix
val modelViewMatrix = converter.modelViewMatrix
val modelViewProjectionMatrix = converter.modelViewProjectionMatrix
Member Description

camera, cameraIntrinsicParameters, cameraCenter, cameraRotation

Camera defining the point of view, or any of its parts.

projectionMatrix, modelViewMatrix

OpenGL matrices for the current camera. Settable: the camera is then derived from them.

modelViewProjectionMatrix

Product of both. Only computed when isModelViewProjectionMatrixComputationEnabled is true.

nearPlane, farPlane, setNearFarPlanes

Clipping planes. Default DEFAULT_NEAR_PLANE (0.1) and DEFAULT_FAR_PLANE (1000).

width, height, setViewportSize

Viewport size in pixels. Usually the values received in onSurfaceChanged.

setValues(nearPlane, farPlane, width, height, camera)

Sets everything at once.

orientation, viewCamera, viewCameraIntrinsicParameters, viewCameraCenter, viewCameraRotation

The camera expressed in Android view coordinates. See Camera and display orientation.

CameraConverter is not thread safe. Use it from the GL thread, as the renderer example does.

CameraToOpenGlHelper

For one-off conversions without keeping state, CameraToOpenGlHelper computes the matrices directly. Every method has a variant writing into a provided array (to avoid allocations in onDrawFrame) and an AndReturnNew variant:

val projectionMatrix = CameraToOpenGlHelper.computeProjectionMatrixAndReturnNew(
    camera, width, height, 0.1f, 100.0f
)
val modelViewMatrix = CameraToOpenGlHelper.computeModelViewMatrixAndReturnNew(camera)
val modelViewProjectionMatrix =
    CameraToOpenGlHelper.computeModelViewProjectionMatrixAndReturnNew(
        projectionMatrix, modelViewMatrix
    )

Overloads accept a whole PinholeCamera, or its intrinsic parameters, rotation (Rotation3D or a 3x3 Matrix) and center separately.

OpenGlToCameraHelper

The inverse conversion recovers a pinhole camera from OpenGL matrices, for instance those supplied by an AR framework for each frame:

val camera = OpenGlToCameraHelper.computePinholeCameraAndReturnNew(
    projectionMatrix, viewMatrix, width, height
)
camera.decompose()
val intrinsics = camera.intrinsicParameters
val center = camera.cameraCenter
val rotation = camera.cameraRotation

computePinholeCameraIntrinsics and computePoseTransformation recover only the intrinsic parameters or only the pose.