Every time an object moves in a 3D scene, the camera turns or a model is drawn on screen, the computer performs the same operation on thousands or millions of vertices: a multiplication by a matrix. It is the common language of all 3D software, from modelling programs to game engines.
The three basic transformations
- Translation: moves a point by adding a vector (dx, dy, dz).
- Scale: multiplies the coordinates by factors (sx, sy, sz). Unequal factors distort the object; a negative factor mirrors it.
- Rotation: turns the point around an axis by a given angle.
Rotation and scale can be expressed with a 3 × 3 matrix. Translation cannot, because it is not a linear transformation: it moves the origin too. To treat all three the same way, an elegant device is used.
Homogeneous coordinates
A fourth coordinate, w, is added. A point (x, y, z) becomes (x, y, z, 1). With a 4 × 4 matrix, translation becomes a simple multiplication:
| 1 0 0 dx | | x | | x + dx |
| 0 1 0 dy | × | y | = | y + dy |
| 0 0 1 dz | | z | | z + dz |
| 0 0 0 1 | | 1 | | 1 |
Directions (such as normals or the direction of a light) are written with w = 0 instead: this way translation does not affect them, as it should not. A direction stays a direction even if the object moves.
The decisive advantage is that several transformations can be combined into a single matrix by multiplying them together. Instead of applying scale, then rotation, then translation to every vertex, you compute one matrix and apply it once.
Order matters
Matrix multiplication is not commutative: A × B is in general different from B × A. In practice this means that rotating then moving gives a different result from moving then rotating.
- If an object is first rotated around the origin and then moved, it spins on itself and then moves.
- If it is first moved and then rotated, it rotates around the world origin, tracing an orbit.
The usual order for placing an object is: scale, then rotation, then translation. With the column-vector convention, the combined matrix is written in reverse: M = T × R × S, and is applied to the vertex from right to left.
Coordinate spaces
A vertex passes through a series of reference frames before becoming a pixel:
| Space | Description | Matrix leading to the next |
|---|---|---|
| Object (local) | coordinates as defined by the modeller | Model |
| World | position in the scene | View |
| View (camera) | coordinates relative to the camera | Projection |
| Clip | homogeneous coordinates before division | division by w |
| Normalised device | standard cube, values between −1 and 1 (or 0 and 1 for depth, depending on the API) | viewport transform |
| Screen | pixels | — |
The first three matrices are often combined into the MVP (Model-View-Projection) matrix, computed once per object and passed to the graphics card.
The View matrix is, in essence, the inverse of the camera's position and orientation: instead of moving the camera through the world, the world is moved the opposite way so that the camera stays still at the origin, looking along an axis.
Perspective projection
In reality distant objects look smaller. Mathematically, this corresponds to dividing x and y by the distance from the camera. The perspective projection matrix prepares this division by copying depth into the w coordinate. After the multiplication, the graphics card divides x, y and z by w: this is the perspective divide.
The projection's parameters are:
- field of view: the width of the framing, like a lens's focal length;
- aspect ratio: screen width divided by height;
- near plane and far plane: the minimum and maximum visible distances.
The choice of near and far planes affects depth precision. With a near plane too close to the camera, precision is concentrated there and distant objects show z-fighting, surfaces that flicker because the card cannot determine which is in front.
Orthographic projection, on the other hand, does not divide by distance: identical objects look identical at any depth. It is used in technical drawings, CAD views and many games with an isometric view.
Rotations and quaternions
Describing rotations with three angles (Euler angles) is intuitive but has a well-known flaw, gimbal lock: in certain configurations two axes line up and a degree of freedom is lost. For animation and smooth interpolation, quaternions are used, four-component mathematical objects that represent a rotation without this problem and interpolate smoothly. At drawing time they are converted into a matrix.
In short
- Translation, rotation and scale are represented by 4 × 4 matrices thanks to homogeneous coordinates.
- Points have w = 1, directions w = 0: translation does not affect directions.
- Matrix multiplication is not commutative: the usual order is scale, rotation, translation.
- A vertex passes through object, world, view, clip and screen space; the MVP matrix sums them up.
- Perspective projection is completed by dividing by w; quaternions avoid gimbal lock.