Transformation Matrices
- You can precompute one matrix (M) and apply it to many points
- Easy to chain transforms:
- This is how graphics pipelines (OpenGL, DirectX, etc.) work
Minimal pseudocode¶
Build final transformation matrix¶
M = T(cx, cy, cz) * Rz(θ) * T(-cx, -cy, -cz)
Apply to each point¶
1. Model matrix (M)¶
“Where is the object in the world?” - Takes your object’s local coordinates (its own origin) - Moves / rotates / scales it into the world
Example: - A cube defined around (0,0,0) - You want it at (10, 2, -5) and rotated → That transformation is the model matrix2. View matrix (V)¶
“Where is the camera, and what is it looking at?” - Moves the world so the camera is at the origin - Equivalent to “moving the camera,” but actually moves everything else
Key idea: - If camera moves forward, everything else moves backward But more precisely: - You don’t “move the camera” directly - You apply the inverse transform of the camera to everything else If the camera has: - position:C
- rotation: R
Then the view matrix is:
In practice:
3. Projection matrix (P)¶
“How do we turn 3D into 2D?” - Applies perspective (things further away look smaller) - Converts 3D coordinates into screen-like coordinates
This is where 3D becomes 2D. There are two common types: - Perspective projection (what you want for 3D) - Orthographic projection (no depth scaling) You almost always want perspective.Perspective projection (simple derivation)¶
The simplest form comes from similar triangles:
For a point (x, y, z):
Add field of view (FOV)¶
Instead of raw division, we scale it:
Where:Full projection matrix (standard form)¶
In homogeneous coordinates:
Where: -f = 1 / tan(FOV / 2)
- aspect = width / height
- near, far define depth range
Combined pipeline¶
All together:
Order (right → left): 1. Model → place object in world 2. View → move world relative to camera 3. Projection → apply perspectiveWhat happens after projection¶
After:
You do: