Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 50 ยท 3D Transformations and Viewing

Orthographic and Perspective Projection

Learn how 3D points are projected onto a 2D screen: parallel (orthographic) and perspective projection, the perspective divide, FOV and matrices, with demos.

After the view transformation every point is described relative to the camera, but it still has three coordinates, and a screen has only two. Projection is the step that throws the third dimension away in a controlled way, so that the picture looks right. There are two families. Parallel (orthographic) projection simply drops depth, so objects keep their size at any distance. Perspective projection makes distant objects smaller, which is how eyes and cameras see the world.

In this lesson you will derive both, meet the perspective divide, build the standard projection matrices, and see why the near and far planes exist and why depth precision is such a delicate topic.

Orthographic (parallel) projection

In an orthographic projection all projection lines are parallel to the viewing direction. If the camera looks down -z, a view-space point (x, y, z) simply lands at (x, y): the z coordinate is ignored for position. Two consequences:

  • Parallel lines in 3D stay parallel in the picture.
  • Size does not depend on distance. A cube far away looks as large as one nearby.

That is exactly what technical drawings, CAD views, 2D games with a 3D look, and UI layers want. Real systems still keep z (scaled to a standard range) because the depth test needs it later.

Practically, you choose a box in view space, defined by left l, right r, bottom b, top t, near n and far f, and map it to the cube from -1 to +1 in all three axes, called normalized device coordinates (NDC). This is a scale and a translation per axis:

TEXT
Ortho = | 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      |

The minus sign in the z row appears because the camera looks down -z while NDC treats larger values as farther away. A point at distance n (view z = -n) maps to NDC z = -1, and a point at distance f maps to +1.

Perspective projection

Hold up a ruler at arm's length and then at twice that distance: the far one looks half as long. A perspective camera does the same. Put the image plane at distance d in front of the eye. A point (x, y, z) in view space (with z negative) projects to where the ray from the eye through the point crosses that plane. By similar triangles:

TEXT
x' = d * x / (-z)
y' = d * y / (-z)

Dividing by the distance is the heart of perspective. Everything else is bookkeeping. Look at how the numbers behave: doubling the distance halves the screen size, and a point on the line of sight (x = y = 0) always lands in the middle.

flowchart LR
  A["View space<br/>(x, y, z)"] -->|"Projection matrix"| B["Clip space<br/>(xc, yc, zc, wc)"]
  B -->|"divide by wc"| C["NDC<br/>(x/w, y/w, z/w)"]
  C -->|"viewport transform"| D["Screen pixels"]

From view space to pixels: the matrix prepares, the divide by w creates the perspective.

Why a matrix, and what is w?

Division is not a linear operation, so a matrix alone cannot do it. The trick of homogeneous coordinates (the fourth component) solves this. The projection matrix is built so that it copies the distance into w. The hardware then divides x, y and z by w afterward. This is the perspective divide.

The standard OpenGL-style perspective matrix uses a vertical field of view fovy, an aspect ratio a = width / height, and the near and far plane distances n and f. With g = 1 / tan(fovy / 2):

TEXT
Persp = | g/a   0        0                 0         |
        |  0    g        0                 0         |
        |  0    0   (f+n)/(n-f)      2fn/(n-f)       |
        |  0    0       -1                 0         |

Multiply a view-space point (x, y, z, 1) and you get a clip-space point whose w is -z, the distance in front of the camera. After the divide:

  • x_ndc = g/a * x / (-z)
  • y_ndc = g * y / (-z)
  • z_ndc is a function of the distance that goes from -1 at the near plane to +1 at the far plane.

Anything outside the range -1 to +1 in x, y or z lies outside the viewing volume and is clipped. The last step, the viewport transform, maps NDC to pixels: px = (x_ndc + 1) / 2 * width and py = (1 - y_ndc) / 2 * height (the flip because screen y points down; see coordinate systems).

Field of view

The field of view (FOV) is the opening angle of the viewing pyramid. A small FOV (a telephoto lens, say 20 degrees) magnifies and flattens the scene. A large FOV (a wide-angle lens, 100 degrees or more) shows more of the world, exaggerates depth and stretches objects near the edges. Games typically use 60 to 100 degrees vertically. Because g = 1 / tan(fovy / 2), halving the tangent of the half-angle doubles the on-screen size.

Frustum

The visible region of a perspective camera is a pyramid with its tip cut off by the near plane and closed by the far plane. That shape is called a view frustum. An orthographic view volume is a plain box.

The demo: wireframe box in both projections

The box below is long in the z direction, so it points away from you. Switch between projections and change the field of view. In the orthographic view, the long edges stay parallel; in perspective, they converge toward a vanishing point. All the math is done with the actual matrices from this lesson, followed by the divide and the viewport transform.

Orthographic versus perspective wireframe

Red edges run along the depth direction. With a yaw near zero you look down the box in perspective and the red edges nearly meet at the center, which is the vanishing point. In the orthographic view they never converge, however you rotate.

Near and far planes, and depth precision

Why do we need a near plane at all? Because the formula divides by distance, and a point at distance zero would divide by zero. The near plane also keeps w positive. The far plane bounds the depth range so it can be stored in a fixed number of bits.

Perspective depth is not linear. Mathematically, with d as the distance in front of the camera:

TEXT
z_ndc = (f+n)/(f-n) - 2fn / ((f-n) * d)

Most of the available precision is spent close to the near plane. The next console run shows how the NDC depth (mapped to 0..1) behaves for different near-plane choices.

JS How depth values are distributed

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

No output.

With near = 0.1, an object only 2 units away already has a depth of about 0.95, so everything from 2 to 100 units is squeezed into the last 5 percent of the range. With near = 1, the same object sits near 0.5. That squeeze is why pushing the near plane as far out as your scene allows is the best single fix for depth artifacts. The z-buffer lesson shows what those artifacts look like.

Putting it all together

The complete journey of a vertex is now:

  1. Model matrix: object space to world space.
  2. View matrix: world space to view space (the camera).
  3. Projection matrix: view space to clip space.
  4. Perspective divide by w: clip space to NDC.
  5. Viewport transform: NDC to pixel coordinates.

Engines usually multiply the first three into a single model-view-projection (MVP) matrix once per object, so every vertex costs one matrix-vector multiplication.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main visual difference between orthographic and perspective projection?

    Orthographic projection ignores depth for position, so equal objects have equal size anywhere. Perspective divides by distance, so far objects shrink and parallel lines converge.
  2. 2. What does the perspective divide do?

    The projection matrix puts the distance into w. Dividing by w afterward produces the shrinking effect of perspective and yields NDC.
  3. 3. Which change makes the scene look more zoomed in (more magnified)?

    A smaller field of view is like a telephoto lens: the same part of the world is spread over the whole screen.
  4. 4. Why must the near plane be greater than zero in perspective projection?

    w is the distance, so zero is impossible, and a tiny near value squeezes almost all depth values near 1, hurting precision.
  5. 5. A point lies exactly on the camera's line of sight (x = y = 0 in view space). Where does it land after projection?

    x_ndc and y_ndc are both 0, and the viewport maps (0, 0) to the middle of the screen.

Summary

  • Orthographic projection drops depth for position and keeps sizes constant; perspective divides by distance so far things look smaller.
  • The perspective matrix copies the distance into w; dividing by w gives normalized device coordinates in the range -1 to +1.
  • Field of view controls the zoom: g = 1 / tan(fovy / 2).
  • Near and far planes bound the view frustum. Perspective depth is nonlinear, so a near plane that is too close wastes precision.
  • The vertex path is model, view, projection, divide by w, viewport.

Now that points land on the screen, we can connect them into shapes: Polygon Meshes and Wireframe Rendering.