Introduction

There’s no camera object inside a graphics card. What a game calls “the camera” is really two of the matrices from part 5: the view matrix, which says where the camera is and which way it’s looking, and the projection matrix, which says what kind of lens it has. This post covers:

  • How the view matrix is built, and why it’s the inverse of the camera’s position
  • The frustum: the shape of space a camera can see
  • Field of view, aspect ratio and the near and far planes
  • The difference between perspective and orthographic projection

The View Matrix: Moving the World, Not the Camera

Placing a camera is just like placing any other object: it has a position and a direction, so we could build a model matrix for it from a rotation and a translation. Call that the camera’s world matrix: it takes points from the camera’s own space into world space.

But we need the opposite. We want to take every point in the world and express it relative to the camera. A matrix that undoes another matrix is called its inverse, so:

view matrix = inverse of the camera's world matrix

If the camera moves 5 units to the right, the view matrix moves the whole world 5 units to the left. If the camera turns left, the view matrix turns the world right. Everything ends up arranged around a camera sitting at the origin, which is view space.

In practice, most engines build the view matrix directly with a look-at function: give it the camera’s position (the eye), a point to look at (the target) and which way is roughly up, and it works out the camera’s three axes with a little vector maths (mainly the cross product, which finds the direction at right angles to two others). The rows of the rotation part of the resulting matrix are exactly those three axes: right, up and backwards (backwards because, in OpenGL’s convention, the camera looks down −z). You can see it in the demo below.

The Frustum

A camera can’t see everything. It sees a region shaped like a pyramid lying on its side, with the camera at the tip, cut off near the tip and again further away. That shape is called the view frustum (“frustum” is the word for a cone or pyramid with its top sliced off). Anything outside it isn’t drawn.

The demo shows the scene from outside on one side, with the camera (orange) and its frustum outlined, and what that camera actually sees on the other. Drag the overview to look around it.

A camera's frustum, and what it sees

Left: the scene from outsideRight: what the camera sees

View matrix (rows 1 to 3 of the rotation part are the camera’s right, up and back directions):

Projection matrix:

Field of view

The field of view (FOV) is the angle between the top and bottom of the frustum (some engines measure side to side instead). A wide angle sees more of the scene, but each object takes up less of the picture, and objects near the edges of the picture look stretched outwards. (Straight lines still stay straight, unlike with a real fish-eye lens, which bends them.) A narrow angle is like a telephoto lens: it magnifies, and flattens the sense of depth. The width of the frustum comes from the height multiplied by the aspect ratio, the image’s width divided by its height, so the picture isn’t stretched.

Press Dolly zoom to see the famous film trick: the camera moves back while the field of view narrows, keeping the ball the same size. The ball doesn’t change, but the pillars behind it seem to swing towards it, because the strength of the perspective is changing.

Near and far planes

The frustum is cut off at a near plane and a far plane (in maths, a plane is a flat surface that goes on forever). Nothing closer than the near plane or further than the far plane is drawn. Drag the near slider up until it slices through the ball, or pull the far slider in until the distant pillars vanish.

Why not make the near plane tiny and the far plane enormous? (It can never be exactly 0: the projection divides by distance, and dividing by zero breaks the maths.) The depth buffer (part 11) stores depth with limited precision, spread very unevenly between near and far. A near plane that’s too close wastes most of that precision right in front of the camera, and distant surfaces start to flicker through each other. The usual advice is to push the near plane out as far as you can get away with.

Perspective and Orthographic Projection

A perspective projection makes things smaller the further away they are, using the divide by w from part 5. It’s how our eyes and real cameras work, and most 3D games use it.

An orthographic projection doesn’t shrink distant objects at all. The frustum becomes a plain box, and parallel lines stay parallel on screen. Switch the demo to Orthographic and look at the pillars: the far ones are exactly as big as the near ones. It’s used for 2D games, strategy and “isometric-looking” games, technical drawings, user interfaces, and for the light’s point of view when drawing shadows (Shadow Mapping). In the projection matrix, you can see the difference in the bottom row: for perspective it’s (0, 0, −1, 0), which copies the depth into w; for orthographic it’s (0, 0, 0, 1), so w stays 1 and the divide does nothing.

Under the hood

Engines usually skip drawing objects that are completely outside the frustum before sending them to the graphics card at all. This is frustum culling: the CPU tests a simple box or sphere that surrounds each object against the frustum’s six sides, which can be read straight out of the view and projection matrices multiplied together. It’s often the single biggest saving in a game’s drawing code, because most of a large world is behind or beside the camera. Anything that slips through is still safe: the hardware clips triangles to the frustum after the vertex shader, as described in part 5.

Summary

  • A camera is two matrices: the view matrix (where it is) and the projection matrix (its lens).
  • The view matrix is the inverse of the camera’s world matrix, usually built with look-at.
  • The camera sees a frustum, shaped by the field of view, aspect ratio, near and far planes.
  • Perspective shrinks distant things; orthographic doesn’t.
  • Keep the near plane as far out as you can, for depth precision.

Next, part 7 looks at the program that applies all these matrices to every vertex: the vertex shader.

Further Reading