Understanding Perspective Projection Matrices

Jul 30, 2024

Notes on Perspective Projection Matrix

Introduction to Matrices in Transformations

  • Matrices are used to encode transformations:
    • Translating vectors
    • Rotating vectors
    • Scaling vectors
  • Multiplying matrices by vectors changes their coordinates (x, y, z).

Projection Phase

  • Beyond transformations, matrices can also represent projection phases.
  • Purpose: Project 3D objects from world space to 2D screen space.
  • Types of Projection:
    • Perspective Projection: Realistic depth perception.
    • Orthographic Projection: All objects appear the same size regardless of depth.

Goals of the Perspective Projection Matrix

  • The perspective projection matrix serves three important functions:
    1. Aspect Ratio Adjustment:
      • Accounts for different screen resolutions and aspect ratios (e.g., 16:10, 16:9).
      • Adjust x and y values based on the height and width of the screen.
    2. Field of View (FOV):
      • Determines the angle of viewing (e.g., 60 degrees, 90 degrees).
      • Affects the scaling of x and y values based on the FOV angle.
    3. Normalization:
      • Maps all values into a range between -1 and 1 (Normalized Device Coordinates).
      • Important for achieving a consistent output across devices.

Aspect Ratio

  • Defined as the ratio of height to width.
  • For screen space conversion:
    • Multiply original x values by the aspect ratio scaling factor.

Field of View (FOV)

  • Describes how much of the scene is visible at any given moment.
  • Scaling factor based on the tangent of half the FOV angle:
    • Larger FOV results in smaller object sizes on the screen.
    • Inverse relationship: larger FOV → smaller objects, smaller FOV → larger objects.

Z Value Normalization

  • Objects exist in world space with varying depth (z values).
  • Need to normalize z values between two key planes:
    • zNear: Closest visible object.
    • zFar: Farthest visible object.
  • Mapping z values into a range between 0 and 1:
    • Formula: (zFar / (zFar - zNear))

Constructing the Projection Matrix

  • Combine all scaling factors:
    • Multiply x by aspect ratio and FOV scaling factor.
    • Multiply y by FOV scaling factor.
    • Multiply z by the normalization factor (lambda).

Matrix Representation

  • The projection matrix can be represented in a tabular form.
  • Each entry in the matrix corresponds to specific scaling and normalization factors.
    • e.g., Position (0, 0) = Aspect Ratio
    • e.g., Position (1, 1) = Inverse Tangent of FOV
    • e.g., Position (2, 2) = zFar/(zFar - zNear)

Perspective Divide

  • The perspective divide occurs after the projection matrix multiplication.
  • Important to save the original z value for later use (e.g., texture mapping).
  • Store this value in the w component of the resulting vertex.

Implementation in Code

  • Create a function to generate the projection matrix based on FOV, aspect ratio, zNear, and zFar.
  • Implement a separate function for multiplying the projection matrix by vertex vectors and performing the perspective divide.

Key Takeaways

  • Understanding matrix multiplication is essential for transforming vectors and performing projections.
  • The projection matrix efficiently adjusts all necessary factors to simulate realistic 3D rendering on a 2D screen.