TExcelTAD
Home › Math for Games › Perspective and Orthographic Projection

Perspective and Orthographic Projection

The projection matrix squeezes the part of view space the camera can see (the view frustum) into a standard box the GPU understands. There are two kinds:

Perspective Orthographic
Shape of the visible volume a truncated pyramid (frustum) a box
Far things look smaller the same size
Used for 3D games, first/third-person views 2D games, UI, strategy maps, shadow maps, CAD
Parameters vertical FOV, aspect, near, far left, right, bottom, top, near, far

The idea behind perspective

A point at view-space position (x, y, z) with depth d = -z appears on screen at x / d, y / d: twice as far away, half as big. A matrix cannot divide, but homogeneous coordinates can: the projection matrix copies the depth into w, and the GPU divides x, y, z by w afterwards. That step is the perspective divide.

The OpenGL-style matrix (used by GLM's default glm::perspective, right-handed, depth mapped to -1..1) is:

text
f = 1 / tan(fovY / 2)

| f/aspect  0        0                         0                    |
| 0         f        0                         0                    |
| 0         0   (far+near)/(near-far)   2*far*near/(near-far)       |
| 0         0       -1                         0                    |

The -1 in the bottom row is what puts -z (the depth) into w.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Vec4 { double x, y, z, w; };
struct Mat4 { double m[4][4]{}; };
Vec4 operator*(const Mat4& M, Vec4 v) {
    double in[4] = {v.x, v.y, v.z, v.w}, o[4] = {};
    for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) o[r] += M.m[r][c] * in[c];
    return {o[0], o[1], o[2], o[3]};
}

Mat4 perspective(double fovY, double aspect, double n, double f) {
    double t = 1.0 / std::tan(fovY / 2);
    Mat4 p;
    p.m[0][0] = t / aspect;
    p.m[1][1] = t;
    p.m[2][2] = (f + n) / (n - f);
    p.m[2][3] = 2 * f * n / (n - f);
    p.m[3][2] = -1;
    return p;
}

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Mat4 P = perspective(60 * std::numbers::pi / 180, 16.0 / 9.0, 0.1, 100);
    // The same 1-unit offset to the right, at increasing distances.
    for (double depth : {2.0, 4.0, 8.0, 16.0}) {
        Vec4 clip = P * Vec4{1, 0, -depth, 1};
        double ndcX = clip.x / clip.w, ndcZ = clip.z / clip.w;
        std::cout << "depth " << std::setw(6) << depth << ": clip w=" << std::setw(6) << clip.w
                  << "  ndc x=" << z0(ndcX) << "  ndc z=" << ndcZ << "\n";
    }
}
depth  2.000: clip w= 2.000  ndc x=0.487  ndc z=0.902
depth  4.000: clip w= 4.000  ndc x=0.244  ndc z=0.952
depth  8.000: clip w= 8.000  ndc x=0.122  ndc z=0.977
depth 16.000: clip w=16.000  ndc x=0.061  ndc z=0.989

The x offset on screen halves each time the distance doubles. Now look at the NDC depth (ndc z): it climbs toward 1 very quickly. Depth precision is not linear: most of it is spent close to the near plane.

Near and far planes and z-fighting

That non-linearity is why the near plane matters so much. Distant surfaces that are close together get the same depth value and flicker through each other (z-fighting). Pushing the near plane out from 0.01 to 0.1 or 0.3 helps far more than pulling the far plane in.

cpp · try it yourself
#include <iostream>
#include <iomanip>

// NDC depth (-1..1) of a point at distance d, for an OpenGL-style perspective matrix.
double ndcDepth(double d, double n, double f) {
    double clipZ = ((f + n) / (n - f)) * -d + 2 * f * n / (n - f);
    return clipZ / d;                          // w = d
}

int main() {
    std::cout << std::fixed << std::setprecision(6);
    const double far = 1000;
    for (double near : {0.01, 0.3}) {
        std::cout << "near = " << near << "\n";
        for (double d : {1.0, 10.0, 100.0, 500.0}) {
            std::cout << "  d=" << std::setw(4) << static_cast<int>(d) << "  ndc z = " << ndcDepth(d, near, far) << "\n";
        }
        double gap = ndcDepth(501, near, far) - ndcDepth(500, near, far);
        std::cout << "  gap between d=500 and d=501: " << std::scientific << std::setprecision(2) << gap
                  << std::fixed << std::setprecision(6) << "\n";
    }
}
near = 0.010000
  d=   1  ndc z = 0.980020
  d=  10  ndc z = 0.998020
  d= 100  ndc z = 0.999820
  d= 500  ndc z = 0.999980
  gap between d=500 and d=501: 7.98e-08
near = 0.300000
  d=   1  ndc z = 0.400420
  d=  10  ndc z = 0.940582
  d= 100  ndc z = 0.994598
  d= 500  ndc z = 0.999400
  gap between d=500 and d=501: 2.40e-06

With near = 0.01, surfaces at 500 and 501 units differ by less than one step of a 24-bit depth buffer (about 1.2e-07 in NDC units), so they z-fight. With near = 0.3 the gap is 30 times bigger and easily resolved. Modern engines also use reversed-Z (depth 1 at the near plane, 0 at the far plane, with a floating-point depth buffer), which spreads precision much more evenly. Unreal does this by default; Unity on D3D/Vulkan/Metal does too.

Orthographic projection

An orthographic projection is just a scale and a translation that maps the box [left, right] × [bottom, top] × [-near, -far] onto the NDC cube. w stays 1, so distance has no effect on size.

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Mat4 { double m[4][4]{}; };

Mat4 ortho(double l, double r, double b, double t, double n, double f) {
    Mat4 o;
    o.m[0][0] = 2 / (r - l);  o.m[0][3] = -(r + l) / (r - l);
    o.m[1][1] = 2 / (t - b);  o.m[1][3] = -(t + b) / (t - b);
    o.m[2][2] = -2 / (f - n); o.m[2][3] = -(f + n) / (f - n);
    o.m[3][3] = 1;
    return o;
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    // A 2D game camera showing 16 x 9 world units centered on (0, 0).
    Mat4 O = ortho(-8, 8, -4.5, 4.5, 0.1, 100);
    for (double depth : {2.0, 50.0}) {
        double x = 4, y = 4.5, z = -depth;
        double nx = O.m[0][0] * x + O.m[0][3];
        double ny = O.m[1][1] * y + O.m[1][3];
        double nz = O.m[2][2] * z + O.m[2][3];
        std::cout << "point (4, 4.5) at depth " << depth << " -> ndc (" << nx << ", " << ny << ", " << nz << ")\n";
    }
}
point (4, 4.5) at depth 2.000 -> ndc (0.500, 1.000, -0.962)
point (4, 4.5) at depth 50.000 -> ndc (0.500, 1.000, -0.001)

The point is at the same NDC x and y at both depths: halfway to the right edge and exactly on the top edge. Only the depth changes, and it changes linearly. A 2D game's "camera size" or "zoom" setting is just the size of this box.

Field of view in practice

  • Vertical vs horizontal FOV: GLM, Unity and Godot (by default) take the vertical FOV; Unreal's camera takes the horizontal FOV. On a wider screen, a fixed vertical FOV shows more to the sides ("Hor+"), which is what players expect.
  • Converting: hFov = 2 * atan(tan(vFov / 2) * aspect). A 60° vertical FOV at 16:9 is about 91.5° horizontal.
  • Typical values: 60–75° vertical for third-person, 90–110° horizontal for PC first-person shooters. Always offer a slider: a narrow FOV makes some players motion-sick.

Test yourself

Which step makes distant objects smaller in a perspective projection?

  1. The model matrix
  2. Dividing x, y and z by w after the projection matrix
  3. Normalizing the vertex
  4. The viewport transform

Distant surfaces flicker through each other (z-fighting). What usually helps most?

  1. Pulling the far plane in a little
  2. Pushing the near plane out (e.g. 0.01 to 0.3)
  3. Increasing the FOV
  4. Using an orthographic camera for 3D

What does an orthographic projection do with distance?

  1. Shrinks far objects
  2. Nothing to the size: objects keep their size at any depth
  3. Enlarges far objects
  4. It removes the depth entirely