TExcelTAD
Home › Math for Games › Clip Space, NDC and Screen

Clip Space, NDC and Screen

After the projection matrix, a vertex is in clip space: a 4D vector (x, y, z, w). Three more steps turn it into a pixel, and the GPU does all of them for you. You still need to understand them to place UI over 3D objects, draw health bars above enemies, or debug a scene that renders nothing.

1. Clipping

A point is inside the view frustum when

text
-w <= x <= w,   -w <= y <= w,   -w <= z <= w     (OpenGL convention)
 0 <= z <= w                                     (Direct3D, Vulkan, Metal)

Triangles that cross the boundary are cut. Points with w <= 0 are behind the camera. If you project them anyway and divide by a negative w, they appear mirrored on the screen, which is the classic "the enemy marker shows up behind me" bug.

2. The perspective divide: NDC

Dividing by w gives normalized device coordinates: x and y from -1 (left/bottom) to 1 (right/top), and a depth z.

3. The viewport transform: pixels

NDC is stretched to the window size. Screens usually have the origin in the top-left with y growing down, so y flips:

text
px = (ndc.x + 1) / 2 * width
py = (1 - ndc.y) / 2 * height

The whole pipeline in one program

This program takes the eight corners of a cube, runs them through model, view and projection matrices, divides by w, maps them to a 48×20 character "screen" and plots them. It is a complete (if tiny) software vertex pipeline.

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

struct Vec3 { double x, y, z; };
struct Vec4 { double x, y, z, w; };
struct Mat4 {
    double m[4][4]{};
    static Mat4 identity() { Mat4 r; for (int i = 0; i < 4; i++) r.m[i][i] = 1; return r; }
};
Mat4 operator*(const Mat4& A, const Mat4& B) {
    Mat4 R;
    for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) for (int k = 0; k < 4; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
    return R;
}
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 translate(Vec3 t) { Mat4 r = Mat4::identity(); r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z; return r; }
Mat4 rotateY(double a) {
    Mat4 r = Mat4::identity(); double c = std::cos(a), s = std::sin(a);
    r.m[0][0] = c; r.m[0][2] = s; r.m[2][0] = -s; r.m[2][2] = c; return r;
}
Mat4 perspective(double fovY, double aspect, double n, double f) {
    double t = 1 / 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;
}

int main() {
    const int W = 48, H = 20;
    const double PI = std::numbers::pi;
    Mat4 model = translate({0, 0, -5}) * rotateY(PI / 6);           // cube 5 units in front, turned 30 deg
    Mat4 view = Mat4::identity();                                    // camera at the origin
    Mat4 proj = perspective(PI / 3, 1.2, 0.1, 100);                  // character cells are tall, so a small aspect
    Mat4 mvp = proj * view * model;

    std::vector<std::string> screen(H, std::string(W, '.'));
    int index = 0;
    for (int x : {-1, 1}) for (int y : {-1, 1}) for (int z : {-1, 1}) {
        Vec4 clip = mvp * Vec4{double(x), double(y), double(z), 1};
        double nx = clip.x / clip.w, ny = clip.y / clip.w;
        int px = static_cast<int>((nx + 1) / 2 * W), py = static_cast<int>((1 - ny) / 2 * H);
        if (px >= 0 && px < W && py >= 0 && py < H) screen[py][px] = char('0' + index);
        std::cout << "corner " << index << " (" << std::setw(2) << x << "," << std::setw(2) << y << "," << std::setw(2) << z
                  << ") w=" << std::fixed << std::setprecision(2) << clip.w << " -> pixel (" << px << ", " << py << ")\n";
        index++;
    }
    for (const auto& row : screen) std::cout << row << "\n";
}
corner 0 (-1,-1,-1) w=5.37 -> pixel (15, 13)
corner 1 (-1,-1, 1) w=3.63 -> pixel (20, 14)
corner 2 (-1, 1,-1) w=5.37 -> pixel (15, 6)
corner 3 (-1, 1, 1) w=3.63 -> pixel (20, 5)
corner 4 ( 1,-1,-1) w=6.37 -> pixel (25, 12)
corner 5 ( 1,-1, 1) w=4.63 -> pixel (34, 13)
corner 6 ( 1, 1,-1) w=6.37 -> pixel (25, 7)
corner 7 ( 1, 1, 1) w=4.63 -> pixel (34, 6)
................................................
................................................
................................................
................................................
................................................
....................3...........................
...............2..................7.............
.........................6......................
................................................
................................................
................................................
................................................
.........................4......................
...............0..................5.............
....................1...........................
................................................
................................................
................................................
................................................
................................................

Corners with a larger w are farther away and land closer to the center of the screen. The nearest edge (corners 1 and 3, w = 3.63) spans rows 5 to 14, while the farthest edge (corners 4 and 6, w = 6.37) spans only rows 7 to 12, although both are 2 units tall. That is perspective, produced entirely by the divide.

Projecting a world point for UI

Drawing a name tag over a character is exactly the pipeline above for one point, plus a behind-the-camera check:

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

struct Vec3 { double x, y, z; };
struct Pixel { double x, y; };

// Camera at the origin looking down -Z, vertical FOV 60 deg, 1920x1080 screen.
std::optional<Pixel> worldToScreen(Vec3 p) {
    const double W = 1920, H = 1080, aspect = W / H;
    const double f = 1 / std::tan(std::numbers::pi / 6);
    double w = -p.z;                                   // depth
    if (w <= 0.1) return std::nullopt;                 // behind (or on) the camera
    double nx = (f / aspect) * p.x / w, ny = f * p.y / w;
    if (nx < -1 || nx > 1 || ny < -1 || ny > 1) return std::nullopt;   // off screen
    return Pixel{(nx + 1) / 2 * W, (1 - ny) / 2 * H};
}

int main() {
    std::cout << std::fixed << std::setprecision(1);
    Vec3 heads[] = {{0, 0, -10}, {2, 1.5, -10}, {2, 1.5, 10}, {30, 0, -10}};
    for (Vec3 h : heads) {
        auto px = worldToScreen(h);
        std::cout << "(" << h.x << ", " << h.y << ", " << h.z << "): ";
        if (px) std::cout << "draw tag at pixel (" << px->x << ", " << px->y << ")\n";
        else std::cout << "hidden\n";
    }
}
(0.0, 0.0, -10.0): draw tag at pixel (960.0, 540.0)
(2.0, 1.5, -10.0): draw tag at pixel (1147.1, 399.7)
(2.0, 1.5, 10.0): hidden
(30.0, 0.0, -10.0): hidden

The third point is the mirror image of the second, but behind the camera. Without the w <= 0.1 check, its tag would be drawn on screen, mirrored. Engines expose this as Unity's Camera.WorldToScreenPoint (check that the returned z > 0), Unreal's ProjectWorldLocationToScreen (returns false when it fails) and Godot's Camera3D.unproject_position (pair it with is_position_behind).

Note: Pixel origin conventions differ: Unity's screen coordinates start at the bottom-left, most UI systems, Unreal, Godot and the browser start at the top-left. And on high-DPI screens, "pixels" for the mouse may be logical points, not physical pixels.

Test yourself

What does a clip-space w <= 0 mean for a point?

  1. It is very far away
  2. It is behind the camera
  3. It is exactly on screen
  4. It is a direction

NDC y = 1 maps to which screen row in a top-left origin window of height H?

  1. H
  2. H / 2
  3. 0 (the top)
  4. -1

Why does the name tag of an enemy behind you appear mirrored on screen without a check?

  1. The FOV is too large
  2. Dividing by a negative w flips the signs of x and y
  3. The viewport is upside down
  4. Because of z-fighting