What chain of coordinate spaces does a mesh vertex cross from its model data to a screen pixel, and what does each step do?
answer
- one frame per stage
- authored pivot, then one shared level frame
- camera moved to the origin
- four components before the divide
- divide by w, then scale to pixels
basics
~20 sModel space, then world space through the model matrix, view space through the view matrix, clip space through the projection matrix, normalised device coordinates after dividing by w, and screen pixels after the viewport mapping.
solid answer
~40 sWith column vectors the vertex goes `clip = P * V * M * p`, so the rightmost matrix applies first. The **model matrix** `M` places the mesh's own pivot-relative vertices in the shared **world** frame. The **view matrix** `V` is the inverse of the camera's world transform, so in **view space** the camera sits at the origin looking down its forward axis. The **projection matrix** `P` maps the visible volume into four-component **clip space**; for a perspective camera it copies view depth into `w`. Dividing `x`, `y` and `z` by `w` gives **normalised device coordinates**, where the visible region has `x` and `y` between -1 and 1. Finally the **viewport mapping** scales NDC to pixel coordinates, usually flipping `y` because pixel rows grow downwards.
go deeper
Recall the six spaces in order, which matrix or step enters each one, and that the view matrix is the inverse of the camera's transform.
Explain why the divide by w creates foreshortening, why orthographic leaves w at 1, and how the viewport mapping flips y for pixel coordinates.
Use the chain to debug: a mirrored marker means a point behind the camera, a mirrored image means one flipped axis, and an upside-down position usually means a missing y flip.
Be ready to justify one documented convention for the codebase (vector convention, handedness, up axis, NDC depth range) so tools, gameplay and rendering code never translate between them by accident.
## The chain at a glance Every vertex a game draws or picks is described in a sequence of **coordinate spaces**, and each arrow between them is one transform. This answer assumes **column vectors**, so a point is transformed as `p' = M * p` and in a product such as `P * V * M * p` the rightmost matrix applies first. Under the row-vector convention the same chain is written `p * M * V * P`; the spaces are identical. | Space | Origin and axes | Reached by | What code uses it for | |---|---|---|---| | **Model** (local, object) | the mesh's authored pivot | nothing; this is the data | vertex positions as modelled | | **World** | one shared origin for the level | model matrix `M` | gameplay, physics, lighting, AI | | **View** (camera, eye) | the camera at the origin | view matrix `V` | depth sorting, view-relative effects | | **Clip** | four components `(x, y, z, w)` | projection matrix `P` | testing against the view volume | | **NDC** | a box, `x` and `y` in `[-1, 1]` | divide by `w` | resolution-independent position | | **Screen** (window) | pixels, plus a depth value | viewport mapping | picking, UI anchoring | ## Model and world space An artist builds a crate around its own pivot, so its vertices are stored in **model space** and are the same for every crate in the level. Each instance carries a **model matrix**, usually built from translation, rotation and scale, that moves those vertices into **world space**, the one frame every system agrees on. Objects parented to other objects chain their matrices (parent times child) before reaching world space. ## View space Projection is simplest when the camera sits at the origin and looks along an axis, so the next step moves the whole world rather than the camera. The **view matrix** is therefore the **inverse** of the camera's own world transform: moving the camera two metres right is the same as moving the world two metres left. A common right-handed convention puts the camera looking down `-z` with `+y` up and `+x` right; many engines instead use a left-handed frame looking down `+z`. Either works as long as the view and projection matrices agree. ## Clip space and the divide The **projection matrix** maps the visible volume (a truncated pyramid called the **frustum** for a perspective camera, a box for an orthographic one) into **clip space**, a four-component homogeneous space. A perspective matrix copies the point's distance in front of the camera into `w`. Dividing `x`, `y` and `z` by `w` (the **perspective divide**) produces **normalised device coordinates**, and because far points have a larger `w` they shrink toward the centre: that division is where foreshortening comes from. An orthographic matrix leaves `w` at 1, so the divide changes nothing and size does not depend on distance. - In NDC the visible region is `-1 <= x <= 1` and `-1 <= y <= 1`. - The depth range differs by graphics API: some use `[-1, 1]` for `z`, others `[0, 1]`. Code that crosses that boundary must know which one it targets. - Points behind the camera end up with `w <= 0`; geometry is clipped against the view volume before the divide precisely so that such points never get divided. ## NDC to screen The **viewport mapping** turns NDC into pixels for a viewport of a given size and offset. With the pixel origin at the top-left and pixel rows growing downwards: - `pixelX = viewportX + (ndc.x + 1) / 2 * width` - `pixelY = viewportY + (1 - ndc.y) / 2 * height` The `1 - ndc.y` term is the **y flip**: NDC `y` grows upwards while pixel rows grow downwards. Depth is mapped into the depth buffer's range at the same time. ## Slips interviewers listen for 1. Writing the product in the wrong order for the convention, for example `M * V * P * p` with column vectors. 2. Using the camera's world transform as the view matrix instead of its inverse. 3. Treating clip space as NDC and forgetting the divide by `w`. 4. Forgetting the y flip between NDC and pixels, so screen positions come out upside down. 5. Rebuilding `P * V` per object instead of once per frame per camera, then multiplying each object's `M` on the right. A candidate who can name each space, say which matrix enters it and say what the space is good for can answer almost every follow-up on this topic, because picking, health bars over heads and custom camera code are all walks forwards or backwards along this one chain.
- Why keep a separate world space instead of going straight from model space to view space?World space is the frame every system shares: gameplay distances, physics, lighting, AI and audio all need positions that do not depend on which camera is looking. Several cameras (split screen, a minimap, a shadow-casting light) can each have their own view matrix over the same world. Renderers still often concatenate `V * M` into one model-view matrix per object, but the world frame stays the reference everything else is written in.
- What happens to a point behind a perspective camera as it moves along this chain?In view space it has the wrong sign of depth, so the projection gives it `w <= 0`. Dividing by a negative `w` flips the signs of `x` and `y`, which would place it mirrored through the screen centre. The pipeline avoids this by clipping in clip space before the divide; any code that projects points itself, such as a health-bar anchor, has to test `w` first.
- Where does a direction vector, such as a surface normal or a light direction, differ from a point on this chain?A direction has no position, so it must ignore translation: it is carried with `w = 0`, or transformed by the upper 3x3 part of the matrix only. It does not go through the perspective divide, and normals under non-uniform scale need their own matrix. Lighting is usually computed in world or view space, before projection, for that reason.
- The view matrix is the camera's world transform, not its inverse.
- Clip space and normalised device coordinates are the same thing.
- The projection matrix outputs pixel coordinates directly.
- Mesh files store each object's vertices in world space.
- The multiplication order does not matter as long as all three matrices are used.