Homogeneous Coordinates
The trick that lets one matrix rotate, scale and move is to add a fourth coordinate, w, to every 3D vector and use 4×4 matrices. These are homogeneous coordinates, and they are why every engine and every shader is full of Mat4 and Vec4.
Points have w = 1, directions have w = 0
point (x, y, z) -> (x, y, z, 1)
direction (x, y, z) -> (x, y, z, 0)A translation matrix puts the offset in the last column:
T = | 1 0 0 tx | T * (x, y, z, 1) = (x + tx, y + ty, z + tz, 1)
| 0 1 0 ty | T * (x, y, z, 0) = (x, y, z, 0)
| 0 0 1 tz |
| 0 0 0 1 |The w component multiplies the last column. Points (w = 1) pick up the translation, directions (w = 0) ignore it. That is exactly right: moving a spaceship changes where it is, but not which way it is pointing or how fast it is going. This is the formal version of the point/direction distinction from the first Vectors lesson.
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec4 { double x, y, z, w; };
struct Mat4 {
double m[4][4]{}; // m[row][col], column vectors
static Mat4 identity() {
Mat4 r;
for (int i = 0; i < 4; i++) r.m[i][i] = 1;
return r;
}
};
Vec4 operator*(const Mat4& M, Vec4 v) {
double in[4] = {v.x, v.y, v.z, v.w}, out[4] = {};
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) out[r] += M.m[r][c] * in[c];
return {out[0], out[1], out[2], out[3]};
}
Mat4 translate(double x, double y, double z) {
Mat4 t = Mat4::identity();
t.m[0][3] = x; t.m[1][3] = y; t.m[2][3] = z;
return t;
}
std::ostream& operator<<(std::ostream& os, Vec4 v) {
return os << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(1);
Mat4 T = translate(10, 0, -5);
Vec4 position{1, 2, 3, 1};
Vec4 velocity{1, 2, 3, 0};
std::cout << "T * point = " << T * position << "\n";
std::cout << "T * direction = " << T * velocity << "\n";
}T * point = (11.0, 2.0, -2.0, 1.0)
T * direction = (1.0, 2.0, 3.0, 0.0)
A full Mat4 with TRS
Here are the building blocks most math libraries provide: identity, multiplication, translate, rotate, scale, plus helpers to transform a point or a direction. Real libraries add perspective, lookAt and inverse, which the next lessons cover.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Vec3 { double x, y, z; };
struct Mat4 {
double m[4][4]{};
static Mat4 identity() { Mat4 r; for (int i = 0; i < 4; i++) r.m[i][i] = 1; return r; }
};
Mat4 operator*(const Mat4& A, const Mat4& B) {
Mat4 R;
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++)
for (int k = 0; k < 4; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
return R;
}
Vec3 transformPoint(const Mat4& M, Vec3 p) { // w = 1
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]};
}
Vec3 transformDir(const Mat4& M, Vec3 d) { // w = 0
return {M.m[0][0] * d.x + M.m[0][1] * d.y + M.m[0][2] * d.z,
M.m[1][0] * d.x + M.m[1][1] * d.y + M.m[1][2] * d.z,
M.m[2][0] * d.x + M.m[2][1] * d.y + M.m[2][2] * d.z};
}
Mat4 translate(Vec3 t) { Mat4 r = Mat4::identity(); r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z; return r; }
Mat4 scale(Vec3 s) { Mat4 r = Mat4::identity(); r.m[0][0] = s.x; r.m[1][1] = s.y; r.m[2][2] = s.z; return r; }
Mat4 rotateY(double a) {
Mat4 r = Mat4::identity();
double c = std::cos(a), s = std::sin(a);
r.m[0][0] = c; r.m[0][2] = s;
r.m[2][0] = -s; r.m[2][2] = c;
return r;
}
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 tree: twice as tall, turned 90 degrees, standing at (5, 0, -3).
Mat4 model = translate({5, 0, -3}) * rotateY(std::numbers::pi / 2) * scale({1, 2, 1});
std::cout << "root " << transformPoint(model, {0, 0, 0}) << "\n";
std::cout << "top " << transformPoint(model, {0, 1, 0}) << "\n";
std::cout << "branch " << transformPoint(model, {0.5, 1, 0}) << "\n";
std::cout << "facing " << transformDir(model, {0, 0, -1}) << " (direction: no translation)\n";
std::cout << "matrix:\n";
for (auto& row : model.m) {
for (double v : row) std::cout << std::setw(8) << z0(v);
std::cout << "\n";
}
}root (5.000, 0.000, -3.000)
top (5.000, 2.000, -3.000)
branch (5.000, 2.000, -3.500)
facing (-1.000, 0.000, 0.000) (direction: no translation)
matrix:
0.000 0.000 1.000 5.000
0.000 2.000 0.000 0.000
-1.000 0.000 0.000 -3.000
0.000 0.000 0.000 1.000
Look at the printed matrix: the upper-left 3×3 block holds rotation and scale (its columns are the model's x, y and z axes in world space, the y axis twice as long), the last column holds the position, and the bottom row is 0 0 0 1. Being able to read a matrix dump like this is a real debugging skill.
Normals need special care
Surface normals are directions, so they use w = 0. But under non-uniform scale they bend the wrong way: squash a sphere flat, and its transformed normals no longer point straight out of the surface. The fix is to transform normals with the inverse transpose of the upper 3×3 block, then renormalize. Engines and shaders do this for you with a "normal matrix".
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec3 { double x, y, z; };
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
int main() {
std::cout << std::fixed << std::setprecision(3);
// A slope surface: tangent along (1, 1, 0), normal (-1, 1, 0). Scale x by 3.
Vec3 tangent{1, 1, 0}, normal{-1, 1, 0};
double sx = 3;
Vec3 t2{tangent.x * sx, tangent.y, tangent.z}; // tangents transform like points/directions
Vec3 nWrong{normal.x * sx, normal.y, normal.z}; // same matrix: wrong for normals
Vec3 nRight{normal.x / sx, normal.y, normal.z}; // inverse transpose of diag(3,1,1) = diag(1/3,1,1)
std::cout << "dot(tangent, wrong normal) = " << dot(t2, nWrong) << " (should be 0)\n";
std::cout << "dot(tangent, right normal) = " << dot(t2, nRight) << "\n";
}dot(tangent, wrong normal) = -8.000 (should be 0)
dot(tangent, right normal) = 0.000
The fourth row and division by w
So far the bottom row has always been 0 0 0 1, so w stays 1 for points. Projection matrices change that: they put the depth into w, and the GPU then divides x, y, z by w. That division is what makes far things small, and it is the subject of the Spaces section.
Tip: A common bug is transforming a direction (velocity, ray direction, normal) as a point. If your bullets fly off toward the world origin, or a ray's direction depends on where the shooter stands, look for a
w = 1that should bew = 0.
Test yourself
What w value does a direction such as a velocity get?
- 1
- 0
- -1
- Its length
In a column-vector TRS matrix, where is the translation stored?
- The bottom row
- The diagonal
- The last column (top three entries)
- The first column
Which matrix transforms normals correctly under non-uniform scale?
- The model matrix itself
- The inverse transpose of the upper 3×3
- The translation matrix
- The identity