setPerspectiveMatrix function

void setPerspectiveMatrix(
  1. Matrix4 perspectiveMatrix,
  2. double fovYRadians,
  3. double aspectRatio,
  4. double zNear,
  5. double zFar
)

Constructs an OpenGL perspective projection matrix in perspectiveMatrix.

fovYRadians specifies the field of view angle, in radians, in the y direction. aspectRatio specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio of x (width) to y (height). zNear specifies the distance from the viewer to the near plane (always positive). zFar specifies the distance from the viewer to the far plane (always positive).

Implementation

void setPerspectiveMatrix(Matrix4 perspectiveMatrix, double fovYRadians,
    double aspectRatio, double zNear, double zFar) {
  final height = math.tan(fovYRadians * 0.5);
  final width = height * aspectRatio;
  final near_minus_far = zNear - zFar;

  perspectiveMatrix
    ..setZero()
    ..setEntry(0, 0, 1.0 / width)
    ..setEntry(1, 1, 1.0 / height)
    ..setEntry(2, 2, (zFar + zNear) / near_minus_far)
    ..setEntry(3, 2, -1.0)
    ..setEntry(2, 3, (2.0 * zNear * zFar) / near_minus_far);
}