Application: Perspective Rendering
How does a computer draw a 3D scene on a flat screen so that distant things look smaller? With a perspective projection: divide by depth. That division is not linear, but a trick called homogeneous coordinates turns the whole pipeline (move, rotate, project) into matrix products followed by one division. This lesson builds it from scratch and renders a wireframe cube.
The pinhole camera
Put the camera at the origin looking along the axis, with the image plane at distance (the focal length) in front of it. A point with projects to where the line from the camera to crosses the plane . By similar triangles:
Doubling the distance halves the image size. That is perspective, and it is not linear: the map divides by a coordinate, and it is not even defined at . Compare orthographic projection , which is linear (a projection) but keeps far objects as large as near ones, like an engineering drawing.
Homogeneous coordinates
Represent the point by the 4-vector , and agree that and any non-zero multiple of it describe the same point . Two problems are solved at once:
1. Translation becomes linear. A shift by is the 4×4 matrix
so rotations, scales and translations all compose by matrix multiplication (Homogeneous Coordinates).
2. Perspective becomes a matrix followed by a division. The 3×4 matrix
is linear on homogeneous coordinates; the non-linear step is only the final perspective divide. GPUs do exactly this: the vertex shader outputs a 4-vector (clip coordinates) and fixed hardware divides by (Clip Space, NDC and Screen).
The pipeline
object coords ──M (model)──▶ world ──V (view)──▶ camera ──P──▶ homogeneous image ──÷w──▶ screenThe whole chain is one matrix applied to every vertex, then one division per vertex. Graphics APIs use a 4×4 version of that also keeps a depth value for the z-buffer (Perspective and Orthographic Projection, 3D Graphics: Transform Pipelines).
import numpy as np
def translate(tx, ty, tz):
T = np.eye(4); T[:3, 3] = [tx, ty, tz]; return T
def rot_y(deg):
t = np.radians(deg); c, s = np.cos(t), np.sin(t)
R = np.eye(4); R[0, 0], R[0, 2], R[2, 0], R[2, 2] = c, s, -s, c; return R
f = 2.0
P = np.array([[f, 0, 0, 0], [0, f, 0, 0], [0, 0, 1, 0]])
p_object = np.array([1.0, 1.0, 1.0, 1.0]) # a cube corner, homogeneous
M = translate(0, 0, 5) @ rot_y(30) # rotate, then push 5 units away
h = P @ M @ p_object
print("homogeneous image:", np.round(h, 4).tolist())
print("screen point:", np.round(h[:2] / h[2], 4).tolist())
for z in (2, 4, 8): # same point, further away
h = P @ np.array([1.0, 1.0, z, 1.0])
print(f"z = {z}: screen x = {h[0] / h[2]:.3f}")homogeneous image: [2.7321, 2.0, 5.366]
screen point: [0.5091, 0.3727]
z = 2: screen x = 1.000
z = 4: screen x = 0.500
z = 8: screen x = 0.250
Rendering a wireframe cube
A cube has 8 corners and 12 edges. Transform the corners with one matrix product, divide, and draw the edges as lines:
import numpy as np
import matplotlib.pyplot as plt
from itertools import product
corners = np.array([[x, y, z, 1.0] for x, y, z in product([-1, 1], repeat=3)]).T # 4 x 8
edges = [(i, j) for i in range(8) for j in range(i + 1, 8)
if np.sum(corners[:3, i] != corners[:3, j]) == 1] # differ in one coord
def rot(axis, deg):
t = np.radians(deg); c, s = np.cos(t), np.sin(t)
R = np.eye(4); i, j = {"x": (1, 2), "y": (2, 0)}[axis]
R[i, i], R[i, j], R[j, i], R[j, j] = c, -s, s, c
return R
M = rot("x", 25) @ rot("y", 35)
M[:3, 3] = [0, 0, 4.5] # translation: 4.5 units in front of the camera
P = np.array([[2.0, 0, 0, 0], [0, 2.0, 0, 0], [0, 0, 1, 0]])
h = P @ M @ corners # 3 x 8: all corners at once
screen = h[:2] / h[2] # perspective divide
plt.figure(figsize=(4.5, 4.5))
for i, j in edges:
plt.plot(screen[0, [i, j]], screen[1, [i, j]], color="tab:blue")
plt.gca().set_aspect("equal"); plt.title("perspective wireframe cube")
print("edges:", len(edges))
print("depths of corners:", np.round(h[2], 2).tolist())edges: 12
depths of corners: [3.85, 5.34, 4.7, 6.18, 2.82, 4.3, 3.66, 5.15]
The face nearer to the camera (smaller depth) is drawn larger, which is what gives the picture depth.
In software
- Every 3D engine (OpenGL, Vulkan, Direct3D, WebGPU, Unity, Godot, three.js) runs this pipeline on the GPU for every vertex of every frame. The model, view and projection matrices are uploaded as uniforms; the divide by and the mapping to pixels are done by fixed-function hardware.
- Perspective-correct texturing: because the divide is non-linear, attributes such as texture coordinates must be interpolated as and across a triangle, then divided per pixel; plain linear interpolation makes textures swim.
- Computer vision runs the pipeline backwards: a camera's 3×4 projection matrix () is estimated from images, and points are triangulated from two views. Augmented reality and photogrammetry depend on it.
- Why homogeneous coordinates use for points and for directions: a direction ignores translation, which is exactly right for normals and velocities.
Exercises
Exercise 1: Project by hand
With , where does the point land on the image plane? And ?
Answer
Both land at .
Solution
The second point is twice as far along the same ray from the camera, so it lands on the same pixel: perspective projection is not one-to-one.
Exercise 2: Show it is not linear
Using , show that projecting and separately and adding is not the same as projecting .
Answer
, but .
Solution
Exercise 3: Compose translations
Write the 4×4 matrices for "translate by " and "translate by ", multiply them, and interpret the product.
Answer
The product is the translation by .
Solution
Translations add their offsets, and in homogeneous coordinates that happens through ordinary matrix multiplication.
import numpy as np
def T(tx, ty, tz):
M = np.eye(4); M[:3, 3] = [tx, ty, tz]; return M
print(T(1, 2, 3) @ T(-4, 0, 5))[[ 1. 0. 0. -3.]
[ 0. 1. 0. 2.]
[ 0. 0. 1. 8.]
[ 0. 0. 0. 1.]]
Exercise 4: Vanishing point
Points on the line run straight away from the camera as grows. With , project them for . Where do they converge?
Answer
They converge to , the vanishing point of lines parallel to the -axis.
Solution
The image is , which tends to as . All lines parallel to the viewing direction meet at the image centre, like railway tracks at the horizon.
for t in (0, 10, 100, 1000):
z = 2 + t
print(t, (round(1 / z, 4), round(-1 / z, 4)))0 (0.5, -0.5)
10 (0.0833, -0.0833)
100 (0.0098, -0.0098)
1000 (0.001, -0.001)
Test yourself
Why do graphics pipelines use 4-component homogeneous coordinates for 3D points?
- To store a colour
- So translation and perspective can be written as matrix products
- Because GPUs cannot handle 3-vectors
- To make rotations faster
With , where does the point project?
Which step of the perspective pipeline is NOT linear?
- The model matrix
- The view matrix
- Multiplying by the projection matrix
- The divide by