setViewMatrix function

void setViewMatrix(
  1. Matrix4 viewMatrix,
  2. Vector3 cameraPosition,
  3. Vector3 cameraFocusPosition,
  4. Vector3 upDirection,
)

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);
}