setViewMatrix function
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 Vector3 z = (cameraPosition - cameraFocusPosition)..normalize();
final Vector3 x = upDirection.cross(z)..normalize();
final Vector3 y = z.cross(x)..normalize();
final double rotatedEyeX = -x.dot(cameraPosition);
final double rotatedEyeY = -y.dot(cameraPosition);
final double 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);
}