Introduction#
Part 4 showed how a matrix can move, rotate and scale a mesh. In a real game, every vertex goes through not one but a whole chain of transformations on its way to the screen, and each step moves it into a new coordinate space. This post covers:
- What a coordinate space is, and why graphics uses several of them
- The five spaces every vertex passes through: local, world, view, clip (and NDC) and screen
- The model, view and projection matrices that connect them
- Where the famous divide by w fits in (w is the extra coordinate from part 4), which is how perspective happens
The demo lets you watch an entire scene move through each space, so you can see what each matrix actually does.
What Is a Coordinate Space?#
A coordinate is only meaningful once you know what it’s measured from. “Three steps forward” means something different depending on where you’re standing and which way you’re facing. A coordinate space is a choice of origin (the point (0, 0, 0)) and of directions for the x, y and z axes. The same point in the world has different coordinates in different spaces, just as your house has a different “address” in a street map, a country map and a satellite photo.
Graphics uses several spaces because each one makes a particular job easy:
- Modelling a chair is easiest when the chair’s own centre is the origin
- Placing chairs around a room is easiest with one shared origin for the whole room
- Working out what the camera sees is easiest when the camera is the origin
- Working out which pixel a point lands on is easiest in a space that matches the screen
The matrices from part 4 are how we get from one space to the next.
The Journey#
Use the buttons (or Next) to move the scene from one space to the next, and drag the picture to look around (or select it and use the arrow keys). You’re watching from outside, as an observer: the outlined box is what the scene’s camera can see. The table underneath follows a single point, the pink corner of the roof, through every space. The field of view slider changes how wide an angle the camera sees, like zooming a camera lens out or in.
One scene in each coordinate space
1. Local space#
Each mesh is built in its own local space (also called object or model space), usually with the origin at its centre or at its base. The house’s vertices are stored like this. Local space doesn’t know or care where the house will end up.
2. World space#
The model matrix moves each object from its local space into world space: one shared space for the whole scene. It’s built from the scale, rotation and translation of part 4. Try Turn the house while in world space: only the model matrix changes, so only the house moves. Every object has its own model matrix; the two crates use the same mesh with different model matrices.
3. View space#
Next, the view matrix moves everything so that the camera ends up at the origin, looking straight down an axis. This is view space (also called camera or eye space). It might seem odd to move the whole world rather than the camera, but the result on screen is identical, and it makes the next step much simpler. If you’ve ever felt like a train next to yours was moving when it was really your own train, it’s the same idea. Part 6, Cameras, covers how the view matrix is built.
By convention in OpenGL and WebGL, the camera looks down the negative z axis in view space, with x to the right and y up. Other engines choose differently, which is the subject of part 8.
4. Clip space and NDC#
The projection matrix is where the 3D view gets turned into something that can become a 2D image. It maps the camera’s viewing volume, called the frustum (a pyramid with its top cut off), onto a simple box. The result is clip space, and the vertex shader’s job is to output each vertex’s position in clip space.
Clip space coordinates have four numbers, (x, y, z, w), and this is where the extra coordinate from part 4 earns its keep. A perspective projection matrix copies each point’s depth (how far it is in front of the camera) into w. The graphics card then divides x, y and z by w. Dividing by the depth makes far-away things smaller, which is exactly what perspective is. The result is called normalised device coordinates, or NDC: everything the camera can see ends up inside a cube from −1 to +1 on each axis (in OpenGL and WebGL; Direct3D, Vulkan and Metal use 0 to 1 for z).
Step from View to Clip → NDC in the demo and watch the frustum become a cube. The far end of the box shrinks the most, so distant objects are squashed smaller, while nearby objects are stretched bigger. Widen the field of view and the squashing gets stronger. Compare the clip and NDC rows in the table: NDC is just clip divided by w.
The divide by w isn’t done by your shader. Your vertex shader outputs a clip-space position, and the fixed-function hardware takes it from there. Before dividing, it clips triangles against the edges of the visible volume: in OpenGL’s convention, a point is inside if each of x, y and z is between −w and +w. That’s why it’s called clip space. Doing this before the divide matters: a point behind the camera has a negative w, and dividing by it would flip it to the wrong side of the screen. Then it divides by w, and finally applies the viewport transform.
5. Screen space#
The last step, the viewport transform, stretches NDC to the size of the canvas in pixels: −1 to +1 in x becomes 0 to 1280, for example. The z value is kept (scaled to 0 to 1 by default) for the depth test in part 11. This is screen space (or window space), and it’s where rasterisation happens. Notice in the table that screen y is measured from the top here, as most screen and image coordinates are. OpenGL’s own window coordinates actually count from the bottom; part 8 covers this kind of difference.
Putting It Together: Model, View, Projection#
So a vertex’s journey is:
The first three steps are matrices, so they can be multiplied into a single model-view-projection matrix, often called the MVP:
A typical vertex shader does little more than this multiplication. The CPU builds the matrices once per object per frame, and the graphics card applies them to every vertex. The divide by w and the viewport transform are done by the hardware afterwards.
Some calculations are easier in particular spaces, so shaders often keep intermediate results too. Lighting is commonly done in world space or view space, for example, because distances and angles there are the same as in the real scene. In clip space and NDC they’ve been squashed by perspective.
Questions You Might Have#
Which way is up?
It depends on the convention. This site, like most WebGL code, uses y up. Some tools and engines, such as Blender and Unreal Engine, use z up instead, which is one of the things to watch for when moving models between programs (Matrix Conventions).
Summary#
- A coordinate space is a choice of origin and axes; the same point has different coordinates in each.
- Local → world uses the model matrix, world → view uses the view matrix, view → clip uses the projection matrix.
- The vertex shader outputs clip space. The hardware clips, divides by w (giving perspective and NDC), and maps to screen space pixels.
- P × V × M combines the three matrices into one.
Next, part 6 builds the view and projection matrices properly: cameras.
Further Reading#
- LearnOpenGL, Coordinate Systems: the same five spaces, with the code to set them up.
- Song Ho Ahn, OpenGL Transformation and OpenGL Projection Matrix: clear diagrams of each space, and the projection matrix derived step by step.
- WebGL2 Fundamentals, 3D Perspective: how the divide by w produces perspective, built up gradually.
- MDN, WebGL model view projection: a walkthrough of clip space and the MVP matrix in WebGL.