skip to content

How do you derive a perspective projection matrix from vertical field of view, aspect ratio and near and far planes, and how does orthographic differ?

level: seniorimportance: should knowfreq 40%

answer

  1. similar triangles at depth one
  2. a matrix cannot divide
  3. let w carry the depth
  4. two equations for depth
  5. orthographic keeps w at one

basics

~20 s

Similar triangles give x / -z, scaled by 1 / tan(fovY / 2) over aspect; the matrix stores -z in w so the perspective divide does it, and two constants map near and far to the NDC depth limits. Orthographic keeps w at 1.

solid answer

~50 s

Assume right-handed view space looking down `-z`, column vectors and NDC depth in `[-1, 1]`. Let `f = 1 / tan(fovY / 2)`. By similar triangles the point lands on the image plane at `x / -z`, so `x_ndc = (f / aspect) * x / -z` and `y_ndc = f * y / -z`. A matrix cannot divide, so its bottom row is `(0, 0, -1, 0)`: it copies `-z` into `w` and the perspective divide performs the division. Depth uses `z_clip = A*z + B`; requiring `z = -near` to give -1 and `z = -far` to give +1 yields `A = -(far + near) / (far - near)` and `B = -2*far*near / (far - near)`, so NDC depth is affine in `1 / z`. An orthographic matrix only scales and offsets a box into NDC and keeps `w = 1`, so size does not change with distance.

code

pseudocode · 14 lines
pseudocode
function perspective(fovY, aspect, near, far):     // right-handed, NDC depth [-1, 1]
    f = 1 / tan(fovY / 2)                            // fovY in radians
    return Matrix4Rows(
        (f / aspect, 0,  0,                              0),
        (0,          f,  0,                              0),
        (0,          0, -(far + near) / (far - near),   -2 * far * near / (far - near)),
        (0,          0, -1,                              0))

function orthographic(l, r, b, t, near, far):
    return Matrix4Rows(
        (2 / (r - l), 0,            0,                 -(r + l) / (r - l)),
        (0,           2 / (t - b),  0,                 -(t + b) / (t - b)),
        (0,           0,           -2 / (far - near),  -(far + near) / (far - near)),
        (0,           0,            0,                  1))

go deeper

for a junior

Recall the four perspective inputs, that the matrix puts depth into w, and that orthographic keeps w at 1 so size ignores distance.

for a middle

Explain the similar-triangle step, where the 1 / tan(fovY / 2) factor and the aspect division come from, and why the bottom row is (0, 0, -1, 0).

for a senior

Derive the depth constants from the near and far conditions for both depth ranges and diagnose stretched images, clipped near geometry and degree-radian slips.

for a principal

Decide whether the game fixes vertical or horizontal field of view across aspect ratios and which depth convention the engine builds its matrices for, then enforce one shared helper.

## Conventions for this derivation The result depends on conventions, so fix them first: **right-handed** view space with the camera at the origin looking down `-z`, `+y` up, **column vectors** (`clip = P * v`), and **NDC depth in `[-1, 1]`**. The `[0, 1]` depth variant is given at the end of the depth step. The inputs are the **vertical field of view** `fovY`, the **aspect ratio** `aspect = width / height`, and the distances `near` and `far` to the two clipping planes, both positive. ## Step 1: similar triangles for x and y A view-space point `(x, y, z)` in front of the camera has `z < 0`, so its distance in front is `-z`. Projected onto a plane one unit in front of the camera, by **similar triangles**, it lands at `(x / -z, y / -z)`. The visible half-height on that plane is `tan(fovY / 2)`, and the half-width is `aspect * tan(fovY / 2)`. Mapping those edges to NDC -1 and +1 gives, with `f = 1 / tan(fovY / 2)`: - `x_ndc = (f / aspect) * x / -z` - `y_ndc = f * y / -z` For `fovY = 90` degrees, `f = 1`; with a 16:9 aspect the x scale is `9 / 16 = 0.5625`. ## Step 2: putting the divide into w A 4x4 matrix is linear: it can scale and add but cannot divide by a coordinate. So the matrix writes the numerators into `x` and `y` and stores `-z` in `w` through a bottom row of `(0, 0, -1, 0)`. The **perspective divide** by `w` that follows produces exactly the formulas above. This is why clip space is four-dimensional and why a perspective matrix's bottom row is not `(0, 0, 0, 1)`. ## Step 3: solving for depth Depth must go through the same divide, so `z_clip = A*z + B` and `z_ndc = (A*z + B) / -z`. Two conditions fix the two unknowns: 1. `z = -near` must give `z_ndc = -1`: `-A*near + B = -near`. 2. `z = -far` must give `z_ndc = 1`: `-A*far + B = far`. 3. Subtracting gives `A = -(far + near) / (far - near)`, then `B = -2*far*near / (far - near)`. For the `[0, 1]` depth range the targets are 0 and 1 instead, giving `A = -far / (far - near)` and `B = -far*near / (far - near)`. Either way `z_ndc = -A - B / z`, which is affine in `1 / z` rather than in `z`. That is the price of sharing one `w`: depth resolution is concentrated near the camera, and how much the near-to-far ratio costs in practice is a depth-buffer question beyond the matrix itself. The full symmetric matrix: ``` | f/aspect 0 0 0 | | 0 f 0 0 | | 0 0 -(far+near)/(far-near) -2*far*near/(far-near) | | 0 0 -1 0 | ``` An **off-centre** frustum, needed for split screen or a stereo eye, replaces the first two rows with `2*near/(r - l)`, `2*near/(t - b)` and offsets `(r + l)/(r - l)`, `(t + b)/(t - b)` in the third column, where `l, r, b, t` are the near-plane edges. ## The orthographic matrix An **orthographic** projection maps a box `[l, r] x [b, t] x [-far, -near]` linearly onto NDC: - `x_ndc = 2x / (r - l) - (r + l) / (r - l)` - `y_ndc = 2y / (t - b) - (t + b) / (t - b)` - `z_ndc = -2z / (far - near) - (far + near) / (far - near)` Its bottom row is `(0, 0, 0, 1)`, so `w` stays 1 and the divide does nothing. | Property | Perspective | Orthographic | |---|---|---| | Inputs | `fovY`, `aspect`, `near`, `far` | box edges `l, r, b, t`, `near`, `far` | | Bottom row | `(0, 0, -1, 0)` | `(0, 0, 0, 1)` | | Size vs distance | shrinks as `1 / depth` | constant | | Parallel lines | converge to vanishing points | stay parallel | | Depth mapping | affine in `1 / z` | linear in `z` | | Typical use | first and third person | isometric strategy views, 2D, UI, directional-light shadow maps | ## Where it bites - **Vertical vs horizontal field of view.** With `fovY` fixed, a wider window shows more to the sides; the horizontal angle is `2 * atan(aspect * tan(fovY / 2))`. A designer's "90 degrees" must say which axis it means. - **Stale aspect.** Building the matrix once and not on resize stretches everything horizontally or vertically. - **Degrees vs radians** inside `tan` produces a wildly wrong `f` with no error. - **Mismatched depth range.** A matrix built for `[-1, 1]` used where `[0, 1]` is expected gives everything closer than about twice the near distance a negative NDC depth, so that geometry is clipped.

  • How do you convert a horizontal field of view a designer specified into the vertical one the matrix needs?
    Both angles share the same image plane, so `tan(fovX / 2) = aspect * tan(fovY / 2)`. Solve for `fovY = 2 * atan(tan(fovX / 2) / aspect)`. Keeping the vertical angle fixed across aspect ratios shows more to the sides on wide screens; keeping the horizontal one fixed crops the top and bottom instead, which is a product decision, not a maths one.
  • Why can a perspective matrix not produce depth that is linear in view-space z after the divide?
    All four clip components are divided by the same `w = -z`. For depth to stay linear in `z` after the divide, `z_clip` would have to be quadratic in `z`, which a linear map cannot produce. So the matrix can only give `z_ndc = -A - B / z`, affine in `1 / z`. Linear depth must be reconstructed later from that value, or written separately.
  • When would you choose an orthographic camera for gameplay rather than a perspective one?
    When distance should not change size or angles: isometric and top-down strategy views, 2D games drawn with a 3D renderer, editor views and UI. Grid tiles and unit footprints read identically across the screen. The cost is no depth cue from size, so height and layering must be shown another way, such as shadows or outlines.

  • The projection matrix itself divides x and y by depth.
  • NDC depth from a perspective matrix is linear in view-space z.
  • Field of view is always measured horizontally.
  • An orthographic matrix is a perspective matrix with a very small field of view.
  • Aspect ratio only needs to be set once at startup.