5. View & Projection Matrices

The model/view/projection pipeline that turns world coordinates into pixels on screen

You've seen how to project a single point and how to rotate with matrices and quaternions. Now it's time to connect these ideas into the MVP pipeline — the sequence of transforms every 3D engine uses to go from an object's local coordinates to the final pixel on screen.

Three Coordinate Spaces

Every vertex in a 3D scene passes through three transformations before it reaches the screen:

  1. Model transform — places the object in the world
  2. View transform — moves the world so the camera is at the origin
  3. Projection transform — flattens 3D into 2D with perspective

Each transform is a 4x4 matrix. Multiply them together and you get a single matrix that does everything in one step.

The MVP Pipeline

clip = Projection * View * Model * vertex
One matrix multiplication chain takes a vertex from object space all the way to clip space.

The Model Matrix

Every object has its own local coordinate system. A cube might be defined with corners at (-1, -1, -1) to (1, 1, 1). The model matrix scales, rotates, and translates it into its position in the world.

M_model = T * R * S

The order matters — scale first, then rotate, then translate. Reversing the order produces completely different results.

Building a Model Matrix

A 4x4 matrix can encode all three operations at once using homogeneous coordinates:

┌ sx*r00  sy*r01  sz*r02  tx ┐
│ sx*r10  sy*r11  sz*r12  ty │
│ sx*r20  sy*r21  sz*r22  tz │
└   0       0       0     1  ┘

The upper-left 3x3 block handles rotation and scale. The rightmost column handles translation. The bottom row stays [0, 0, 0, 1].

The View Matrix

The camera could be anywhere in the scene, looking in any direction. Rather than moving the camera, we move the entire world so the camera ends up at the origin, looking down the negative Z axis.

The LookAt Matrix

The classic way to build a view matrix is with three inputs:

  • eye — where the camera is
  • target — what it's looking at
  • up — which direction is "up" (usually (0, 1, 0))

From these, we derive three orthonormal axes:

forward = normalize(eye - target)
right   = normalize(cross(up, forward))
newUp   = cross(forward, right)

The view matrix is the inverse of placing a camera at eye with those axes:

┌ r.x  r.y  r.z  -dot(r, eye) ┐
│ u.x  u.y  u.z  -dot(u, eye) │
│ f.x  f.y  f.z  -dot(f, eye) │
└  0    0    0        1        ┘

Because it's an orthonormal rotation followed by a translation, the inverse is just the transpose of the rotation with a negated dot-product translation — no expensive matrix inversion needed.

Interactive demo: drag to orbit the camera around a scene and see the view matrix update in real time.

The Projection Matrix

We covered perspective division in the projection article. The projection matrix formalizes this into a single 4x4 matrix that maps the view frustum (a truncated pyramid) into a unit cube called clip space.

Perspective Projection

The frustum is defined by four parameters:

  • fov — vertical field of view angle
  • aspect — width / height ratio
  • near — distance to the near clipping plane
  • far — distance to the far clipping plane
┌ 1/(aspect*tan(fov/2))   0                0             0          ┐
│       0            1/tan(fov/2)           0             0          │
│       0                  0          -(f+n)/(f-n)   -2fn/(f-n)     │
└       0                  0              -1             0          ┘

The -1 in the bottom row is what triggers the perspective divide — after multiplication, the GPU divides by the W component (which now holds -z), making distant objects smaller.

Orthographic Projection

For 2D games, CAD, and UI overlays, we often want no perspective — objects stay the same size regardless of depth. The orthographic matrix maps a rectangular box into clip space:

┌ 2/(r-l)    0        0      -(r+l)/(r-l) ┐
│    0    2/(t-b)      0      -(t+b)/(t-b) │
│    0       0     -2/(f-n)   -(f+n)/(f-n) │
└    0       0        0            1       ┘

No divide-by-W needed — the bottom row is [0, 0, 0, 1].

Interactive demo: switch between perspective and orthographic projection and see how the frustum shape changes.

Putting It All Together

Here's the full pipeline for a single vertex:

1. Start with vertex in model (local) space:  v_local
2. Model transform:   v_world = M_model * v_local
3. View transform:    v_view  = M_view  * v_world
4. Projection:        v_clip  = M_proj  * v_view
5. Perspective divide: v_ndc  = v_clip.xyz / v_clip.w
6. Viewport transform: pixel  = map v_ndc to screen coordinates

In practice, you multiply the three matrices together once (MVP = P * V * M) and send that single matrix to the GPU. Every vertex gets multiplied by the same MVP matrix.

The Depth Buffer

After the perspective divide, the Z component maps to a value between 0 and 1 (or -1 and 1, depending on the API). This normalized depth is stored in the depth buffer to determine which fragments are in front of which.

One subtlety: because of the 1/z relationship, depth precision is not uniform. You get much more precision near the near plane and very little near the far plane. This is why you want your near plane as far out as possible and your far plane as close as you can get away with.

Homogeneous Coordinates: Why 4x4?

You might wonder why we use 4x4 matrices instead of 3x3. The reason is translation. A 3x3 matrix can encode rotation and scale but not translation — translation is an addition, not a multiplication. By adding a fourth coordinate (w = 1 for points, w = 0 for directions), we can roll translation into the matrix multiply.

This trick — called homogeneous coordinates — also makes the perspective divide fall out naturally: after multiplying by the projection matrix, we divide by w to get back to 3D.

Common Pitfalls

Row-major vs. column-major. OpenGL uses column-major matrices; DirectX historically uses row-major. The math is the same but the memory layout differs, and the multiplication order reverses: MVP in column-major becomes PVM in row-major.

Z-fighting. When two surfaces have nearly identical depth values, the depth buffer can't decide which is in front, causing flickering. Fix: increase near-plane distance, reduce far-plane distance, or use a logarithmic depth buffer.

Gimbal lock in view matrices. If you build your view matrix from Euler angles, you can hit gimbal lock. Use a quaternion-based camera instead — see the quaternions article.

What's Next

You now have the full matrix pipeline — from local space to screen space. But what happens after projection? How do triangles become actual pixels? That's rasterization.