Local, World and View Space
A vertex of a 3D model travels through a chain of coordinate spaces before it becomes a pixel. Each arrow in the chain is one matrix:
local (model) --model--> world --view--> view (camera) --projection--> clip --divide by w--> NDC --viewport--> screen| Space | Origin and axes | Who uses it |
|---|---|---|
| Local / model / object | the model's own pivot, as the artist built it | mesh files, skeletal bones, colliders |
| World | the level's origin | gameplay: positions, physics, AI |
| View / camera / eye | the camera at the origin, looking down -Z |
lighting tricks, fog, depth sorting |
| Clip | 4D, before the perspective divide | the GPU's clipping stage |
| NDC | a cube from -1 to 1 | the rasterizer |
| Screen / window | pixels, origin at a corner | UI, mouse input |
This lesson covers the first three. The next two cover projection and the screen.
Local to world: the model matrix
You have already built this one: model = T * R * S from the object's transform (and its parents' transforms, via the hierarchy). It takes vertices from the mesh file into the level.
World to view: the view matrix
The camera is an object in the world with its own transform cameraWorld. To draw the scene from its point of view, we move the whole world so the camera sits at the origin looking down -Z. That is the inverse of the camera's transform:
view = inverse(cameraWorld)In practice nobody stores the camera's matrix and inverts it; you build the view matrix directly with look-at: from the camera's position, a target, and an up hint, build the camera's right, up and back axes with cross products (as in the Cross Product lesson), and put them in the rows of the rotation part (rows, because it is the transposed, inverse rotation).
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec3 { double x, y, z; };
Vec3 operator-(Vec3 a, Vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Vec3 cross(Vec3 a, Vec3 b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
Vec3 normalize(Vec3 v) { double l = std::sqrt(dot(v, v)); return {v.x / l, v.y / l, v.z / l}; }
struct Mat4 { double m[4][4]{}; };
Vec3 transformPoint(const Mat4& M, Vec3 p) {
return {M.m[0][0] * p.x + M.m[0][1] * p.y + M.m[0][2] * p.z + M.m[0][3],
M.m[1][0] * p.x + M.m[1][1] * p.y + M.m[1][2] * p.z + M.m[1][3],
M.m[2][0] * p.x + M.m[2][1] * p.y + M.m[2][2] * p.z + M.m[2][3]};
}
// Right-handed look-at, like glm::lookAt: the camera looks down -Z in view space.
Mat4 lookAt(Vec3 eye, Vec3 target, Vec3 up) {
Vec3 f = normalize(target - eye); // forward
Vec3 r = normalize(cross(f, up)); // right
Vec3 u = cross(r, f); // true up
Mat4 v;
v.m[0][0] = r.x; v.m[0][1] = r.y; v.m[0][2] = r.z; v.m[0][3] = -dot(r, eye);
v.m[1][0] = u.x; v.m[1][1] = u.y; v.m[1][2] = u.z; v.m[1][3] = -dot(u, eye);
v.m[2][0] = -f.x; v.m[2][1] = -f.y; v.m[2][2] = -f.z; v.m[2][3] = dot(f, eye);
v.m[3][3] = 1;
return v;
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Mat4 view = lookAt({0, 2, 6}, {0, 0, 0}, {0, 1, 0});
struct { const char* name; Vec3 p; } pts[] = {
{"target (origin)", {0, 0, 0}},
{"camera itself ", {0, 2, 6}},
{"box to the right", {3, 0, 0}},
{"tree beyond it ", {0, 0, -20}},
};
for (auto& p : pts)
std::cout << p.name << " -> view " << transformPoint(view, p.p) << "\n";
}target (origin) -> view (0.000, 0.000, -6.325)
camera itself -> view (0.000, 0.000, 0.000)
box to the right -> view (3.000, 0.000, -6.325)
tree beyond it -> view (0.000, 6.325, -25.298)
Read the results in view space: the camera itself lands on (0, 0, 0); the target is straight ahead (x = 0, y = 0, negative z, about 6.3 units away); the box to the right has positive x; the far tree has a large negative z. Depth in view space is just -z. Fog, LOD selection and "sort transparent objects back to front" all use it.
Moving between spaces in gameplay code
The same matrices answer everyday questions:
| Question | Computation |
|---|---|
| Where is this mesh vertex in the world? | model * local |
| Where is the player relative to the turret? | inverse(turretWorld) * playerWorld |
| How far in front of the camera is this enemy? | -(view * enemyWorld).z |
| Which direction is "camera forward" in the world? | the camera's local -Z axis, i.e. -(third column of cameraWorld) |
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Vec3 { double x, y, z; };
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
// A camera's world rotation: yaw 30 degrees left, as a 3x3 block (columns = camera axes).
double a = std::numbers::pi / 6, c = std::cos(a), s = std::sin(a);
double R[3][3] = {{c, 0, s}, {0, 1, 0}, {-s, 0, c}};
Vec3 right {R[0][0], R[1][0], R[2][0]}; // column 0
Vec3 up {R[0][1], R[1][1], R[2][1]}; // column 1
Vec3 forward {-R[0][2], -R[1][2], -R[2][2]}; // minus column 2: the camera looks down -Z
std::cout << "camera right " << right << "\n";
std::cout << "camera up " << up << "\n";
std::cout << "camera forward " << forward << "\n";
}camera right (0.866, 0.000, -0.500)
camera up (0.000, 1.000, 0.000)
camera forward (-0.500, 0.000, -0.866)
A first-person controller moves the player along forward and right projected onto the ground, which is why "W" walks where you look and not along the world's -Z.
Note: In Unity and Godot, "camera forward" is the camera's local
+Z(Unity) or-Z(Godot, like this course). Unity'stransform.forwardis+Zbecause Unity is left-handed. Unreal's forward is+X. Check before you negate.
Test yourself
How does the view matrix relate to the camera's world transform?
- They are the same matrix
- The view matrix is its inverse
- The view matrix is its transpose plus translation
- They are unrelated
In this course's right-handed view space, how far in front of the camera is a point with view-space z = -12?
- -12 units (behind)
- 12 units
- 0 units
- It depends on the FOV
Which order does a vertex go through?
- world → local → view → clip
- local → world → view → clip → NDC → screen
- screen → NDC → clip → world
- local → view → world → screen