Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 50 ยท 3D Transformations and Viewing

The Camera and View Transformation

Learn how a virtual camera works in 3D graphics: eye, target and up vector, the look-at view matrix, view space and an orbit camera, with live demos.

A 3D scene is just numbers in world space. To get a picture you need a viewpoint: where are we standing, and where are we looking? Graphics systems model this with a virtual camera. There is no physical camera in the computer, of course. Instead, the renderer applies a view transformation that re-expresses every point of the world relative to the camera, so that the camera sits at the origin and looks along a fixed direction. After that, projecting onto the screen is easy.

This lesson defines the camera with three simple inputs, builds the famous look-at matrix from them, and finishes with an orbit camera you can drag with the mouse.

Defining a camera

You describe a camera with three things:

  • Eye (position): where the camera is, in world coordinates.
  • Target (look-at point): the point it looks toward.
  • Up vector: a rough "which way is up" hint, usually the world's y-axis (0, 1, 0).

Why do we need the up vector? Eye and target fix the viewing direction, but the camera could still roll around that direction like a pilot rolling an airplane. The up vector removes the ambiguity. It does not have to be exactly perpendicular to the view direction; we will fix that in a moment.

From these inputs we derive a full set of three perpendicular axes for the camera, its basis. We use the OpenGL convention: the camera looks down its own negative z-axis, with x to the right and y up. So the camera's local +z points backward, away from the scene.

flowchart TD
  I["Inputs: eye, target, up"] --> F["forward f = normalize(target - eye)"]
  F --> S["right s = normalize(f x up)"]
  S --> U["true up u = s x f"]
  U --> B["Camera basis: s, u, -f"]

The look-at construction: two cross products turn a rough up hint into a perfect right-handed basis.

In words:

  1. Forward f points from the eye to the target, normalized.
  2. Right s is the cross product f x up, normalized. It is perpendicular to both the viewing direction and the up hint.
  3. True up u = s x f is perpendicular to both, so s, u and -f are three unit vectors at right angles to each other.

One failure case: if the viewing direction is parallel to the up vector (looking straight up or down), then f x up is the zero vector and normalizing it divides by zero. Real cameras handle this by choosing a different up vector or limiting the pitch.

From camera axes to the view matrix

The view transformation must do two things to every world point p:

  1. Move the world so the eye ends up at the origin. Subtract the eye position.
  2. Rotate the world so the camera axes line up with the x, y and z axes.

Rotating coordinates into a new basis is easy when the basis vectors are orthonormal: each new coordinate is the dot product of the old point with the corresponding axis. So the rotation matrix has the camera axes as its rows:

TEXT
        |  s.x   s.y   s.z   0 |   | 1 0 0 -eye.x |
View =  |  u.x   u.y   u.z   0 | * | 0 1 0 -eye.y |
        | -f.x  -f.y  -f.z   0 |   | 0 0 1 -eye.z |
        |   0     0     0    1 |   | 0 0 0    1    |

Multiplied out, the last column becomes (-dot(s, eye), -dot(u, eye), dot(f, eye)). This is the matrix produced by gluLookAt in OpenGL and by lookAt functions in math libraries such as glm.

A useful way to think about it: moving the camera is the same as moving the world in the opposite direction. The view matrix is the inverse of the camera's own placement in the world. Its rotation part is the transpose (which equals the inverse) of the camera's rotation, and its translation part undoes the camera's position.

You can check a look-at matrix with three sanity tests: the eye must map to (0, 0, 0); the target must land on the negative z-axis at (0, 0, -distance); a point to the camera's right must get a positive x. The console below does exactly this.

JS Build a look-at matrix and check it

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The eye maps to the origin, the target to (0, 0, -6.164) (its distance, on the negative z-axis), and the last point gets x = 1 and z = -2: one unit to the right, two ahead.

View space and what it buys you

After multiplication by the view matrix, coordinates are in view space (also called eye or camera space). In view space:

  • the camera is at the origin;
  • visible things have negative z, and the more negative, the farther away;
  • -z is the distance in front of the camera, which is exactly what the perspective divide in the next lesson needs.

Lighting is also often computed in view space, because the eye position is a constant, the origin. And a point with positive z in view space is behind the camera and must be discarded or clipped before projection, since dividing by a negative distance would flip it upside down.

The full chain you have built so far is:

TEXT
p_view = View * Model * p_object

The model matrix places the object in the world (3D transformation matrices); the view matrix places the world in front of the camera. The projection matrix comes next, in orthographic and perspective projection.

An orbit camera

A very common camera, used in model viewers and editors, keeps looking at a target while the eye moves on a sphere around it. Three parameters describe it:

  • azimuth (angle around the vertical axis),
  • elevation (angle above the ground plane),
  • distance from the target.

The eye is computed with spherical coordinates:

TEXT
eye = target + distance * ( cos(elev) * sin(azim),  sin(elev),  cos(elev) * cos(azim) )

Dragging the mouse horizontally changes azimuth, vertically changes elevation (clamped to just under plus or minus 90 degrees to avoid the parallel-up problem), and the wheel changes distance. Each frame, rebuild the look-at matrix from the new eye.

The next demo is a tiny software renderer for a scene of a ground grid and three cubes. Drag on the canvas or use the sliders. It uses a simple projection of x / -z, which the next lesson derives properly, and clips each line against the camera's near plane so that geometry behind you does not smear across the screen.

Orbit camera around a small scene

Try moving the distance slider to its minimum: the cubes grow, and some lines cross the near plane and get clipped instead of wrapping around the screen.

Other camera styles

The look-at construction is general. A first-person camera stores a position and yaw/pitch angles, computes the forward vector from them, and uses target = eye + forward. A fly-through camera does the same but also allows roll. A follow camera places the eye behind a moving object and looks at it. The matrix is always built the same way; only the way you choose eye and target changes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which three inputs does a look-at camera need?

    Eye and target give the viewing direction, and the up hint fixes the roll about it. Field of view and planes belong to the projection.
  2. 2. In the OpenGL convention, which way does the camera look in view space?

    View space puts the camera at the origin looking down -z, so visible points have negative z.
  3. 3. Why are the camera axes the rows of the rotation part of the view matrix?

    The camera's own orientation would have the axes as columns. The view matrix undoes it, and the inverse of an orthogonal matrix is its transpose, which turns columns into rows.
  4. 4. What goes wrong when the viewing direction is exactly parallel to the up vector?

    Parallel vectors have a zero cross product. Use another up vector or limit the pitch of the camera.
  5. 5. What does the view matrix do to the eye position?

    By construction the camera sits at the origin of view space.

Summary

  • A camera is defined by eye, target and an up hint; two cross products produce the orthonormal basis s, u, -f.
  • The look-at view matrix has those axes as rows and translation (-s.eye, -u.eye, f.eye); it moves the world so the camera is at the origin looking down -z.
  • Moving the camera equals moving the world the opposite way: the view matrix is the inverse of the camera's placement.
  • In view space visible points have negative z, and anything with positive z is behind the camera and must be clipped.
  • An orbit camera computes the eye from azimuth, elevation and distance, then calls look-at each frame.

Next, we flatten the view onto the screen: Orthographic and Perspective Projection.