setViewMatrix function
void
setViewMatrix()
Constructs an OpenGL view matrix in viewMatrix.
View transformation is the inverse of the model transformation.
View matrix is commonly used to compute the camera location/orientation into
the full model-view stack.
cameraPosition specifies the position of the camera.
cameraFocusPosition specifies the position the camera is focused on.
upDirection specifies the direction of the up vector (usually, +Y).
Implementation
void setViewMatrix(
Matrix4 viewMatrix,
Vector3 cameraPosition,
Vector3 cameraFocusPosition,
Vector3 upDirection,
) {
final z = (cameraPosition - cameraFocusPosition)..normalize();
final x = upDirection.cross(z)..normalize();
final y = z.cross(x)..normalize();
final rotatedEyeX = -x.dot(cameraPosition);
final rotatedEyeY = -y.dot(cameraPosition);
final rotatedEyeZ = -z.dot(cameraPosition);
viewMatrix.setValues(
x[0],
y[0],
z[0],
0.0,
x[1],
y[1],
z[1],
0.0,
x[2],
y[2],
z[2],
0.0,
rotatedEyeX,
rotatedEyeY,
rotatedEyeZ,
1.0,
);
}