The idea: six spaces, three matrices, one divide
A 3D model is stored in its own coordinates. To draw it, every vertex is moved through a chain of coordinate spaces: object → world → eye → clip → NDC → window. Three steps are a 4 × 4 matrix: the model matrix M, the view matrix V and the projection matrix P. Then the GPU divides by w and maps the result to pixels. In a vertex shader the three matrices are one line:
gl_Position = P * V * M * vec4(position, 1.0);
The page follows one vertex at a time. Each matrix fills in, the vertex flies in as a column with
w = 1, the product flies on to the next space. The top view shows the vertex in the world, and the
800 × 600 window shows where it lands. The page uses OpenGL's rules: right-handed axes, the camera looks
down −z, NDC is −1…1 on every axis. The matrices are the ones glm::lookAt and
glm::perspective build.
| space | what it is | reached by |
|---|---|---|
| object | the model's own axes, as the artist made it | (the input) |
| world | one shared frame for the whole scene | M, the model matrix |
| eye (view) | camera at the origin, looking down −z | V, the view matrix |
| clip | 4 numbers (x, y, z, w); the clip test runs here | P, the projection matrix |
| NDC | normalized device coordinates, the cube −1…1 | divide x, y, z by w |
| window | pixels, plus a depth from 0 to 1 | the viewport transform |
The model matrix: T · R · S
The cube has corners (±1, ±1, ±1). Its model matrix is M = T · R · S: scale by 1.5, turn 30° around y, move to (1, 0, −2). A matrix product acts on a column from the right, so you read it right to left: S first, T last. The product is one matrix:
M = [ 1.299 0 0.75 1 ] top-left 3 × 3 = R · S
[ 0 1.5 0 0 ] (1.5 · cos 30° = 1.299,
[ −0.75 0 1.299 −2 ] 1.5 · sin 30° = 0.75)
[ 0 0 0 1 ] last column = the move
For A = (1, 1, 1) row 1 gives 1.299·1 + 0·1 + 0.75·1 + 1·1 = 3.049, and the whole product is world (3.049, 1.5, −1.451). The move sits in the last column, so only a point with w = 1 picks it up; a direction (w = 0) is turned but not moved. Swap the order to R · T and the cube is moved first, then swung around the world origin. (Demo 1: model matrix.) Why the order matters is the subject of Composing Transforms.
The view matrix: lookAt(eye, target, up)
The camera is given by three things: where the eye is, the point it looks at, and which way is up.
glm::lookAt turns them into three unit vectors at right angles:
- f = normalize(target − eye), forward;
- s = normalize(f × up), the camera's right;
- u = s × f, the camera's own up.
Camera 1 sits at (1, 4.5, 4) and looks at the cube centre (1, 0, −2). target − eye = (0, −4.5, −6) has length 7.5, so f = (0, −0.6, −0.8), s = (1, 0, 0) and u = (0, 0.8, −0.6). The rows of V are s, u and −f: they turn the world onto the camera's axes. The last column is −s·eye, −u·eye and f·eye (−1, −1.2, −5.9): it moves the eye to the origin. A ends up at eye (2.049, 0.871, −6.161), 6.161 in front of the camera. In OpenGL eye space, in front means negative z. (Demo 2: view matrix.) V is the inverse of the camera's own placement in the world; the general change of frame is on Changing Coordinates in 3D.
Move the camera and only V changes. From camera 2 at (5.5, 0, 4), f = (−0.6, 0, −0.8), s = (0.8, 0, −0.6), u = (0, 1, 0), and the same A lands at pixel (516.713, 433.661) instead of (572.821, 373.427). (Demo 8: move the camera.)
The projection matrix: perspective(fovy, aspect, near, far)
The page uses glm::perspective(60°, 800 / 600, 1, 20). With f = 1 / tan(fovy / 2):
| entry | formula | value |
|---|---|---|
| x scale | f / aspect | 1.732 / 1.333 = 1.299 |
| y scale | f = 1 / tan 30° | 1.732 |
| z scale | (far + near) / (near − far) | 21 / −19 = −1.105 |
| z offset | 2 · far · near / (near − far) | 40 / −19 = −2.105 |
| last row | (0, 0, −1, 0) | w_clip = −z_eye |
The last row is the key: it copies the distance in front of the camera, −z_eye, into w. The matrix itself is linear; the divide by w that follows is what makes far things small. A goes to clip (2.662, 1.508, 4.704, 6.161). (Demo 3: perspective matrix.) The pinhole picture behind x' = f·x / z is on Perspective Projection.
The clip test, before the divide
In clip space the view frustum has become a simple box: a vertex is inside when w > 0 and −w ≤ x ≤ w, −w ≤ y ≤ w, −w ≤ z ≤ w. The test needs no divide, only comparisons.
- A, camera 1: −6.161 ≤ 2.662 ≤ 6.161 and so on; all pass.
- C = (4, 0, 0), camera 2: clip (7.738, 0, 5.391, 6.782). x = 7.738 > w = 6.782, so C is past the right side. Its NDC x would be 1.141, and its pixel x 856.380, off the 800-pixel window. (Demo 5: a vertex outside the frustum.)
- Q = (0, 3, 5), camera 1: eye (3.75, −0.297, 0.396). Eye z is positive, so Q is behind the camera, and clip w = −0.396 < 0. No point with w < 0 passes −w ≤ x ≤ w. (Demo 6: a vertex behind the camera.)
Why test before dividing? Dividing by a negative w flips every sign. Q is to the right of the camera (eye x = 3.75), but x / w = −12.297 would put it far to the left, and a triangle through it would be drawn across the screen. So the clipper cuts triangles at the frustum first and only then divides. How a triangle is cut is on Sutherland–Hodgman Clipping and The Graphics Pipeline.
Divide by w, then the viewport
For a vertex that passed, NDC = (x / w, y / w, z / w). For A: (2.662 / 6.161, 1.508 / 6.161,
4.704 / 6.161) = (0.432, 0.245, 0.764). Every axis is now −1…1. The viewport set by
glViewport(0, 0, 800, 600) and glDepthRange(0, 1) stretches it to pixels:
x_win = (x_ndc + 1) / 2 · 800 = 572.821
y_win = (y_ndc + 1) / 2 · 600 = 373.427 (y counts up from the bottom)
depth = (z_ndc + 1) / 2 = 0.882
(Demo 4: divide by w, then the viewport.) Turning the three window positions of a triangle into pixels is on Rasterization.
Depth is not linear
Only the z row and w matter for depth: depth = (far / (far − near)) · (1 − near / distance). With near 1 and far 20:
| distance (−z_eye) | 1 | 2 | 5 | 10 | 20 |
|---|---|---|---|---|---|
| depth | 0 | 0.526 | 0.842 | 0.947 | 1 |
So far-away surfaces share very few depth values and can fight over which is in front (z-fighting). Moving the near plane out helps much more than pulling the far plane in. (Demo 7: depth is not linear.)
See also Affine Transforms (homogeneous coordinates in 2D), 3D Rotation Matrix and Cross Product (how s and u are made).
See also Broad-Phase Collision Detection for what a game does with the world-space boxes of its objects before drawing them.
What the page leaves out
Normals, which need the inverse transpose of M; orthographic projection (glm::ortho);
reversed-z and infinite far planes used to fix depth precision; Direct3D and Vulkan conventions (depth
0…1 in NDC, y down in Vulkan); a camera with roll or an up vector parallel to f, where f × up is zero;
actually clipping the triangles (the page only flags the vertex); perspective-correct interpolation;
and the window y flip that many image formats need.