Translation Matrix

How to 3D

Chapter 4: Transformation Machinery

Translation Matrix

We've rewritten the transformations as dot products because graphics cards can evaluate several dot products at the same time. For translation, we want the GPU to compute these three dot products:

$$\begin{aligned} p'_x &= \begin{bmatrix}1 & 0 & 0 & \textrm{offset}_x\end{bmatrix} \cdot \mathbf{p} \\ p'_y &= \begin{bmatrix}0 & 1 & 0 & \textrm{offset}_y\end{bmatrix} \cdot \mathbf{p} \\ p'_z &= \begin{bmatrix}0 & 0 & 1 & \textrm{offset}_z\end{bmatrix} \cdot \mathbf{p} \end{aligned}$$

To parallelize their evaluation, we arrange these vectors into a matrix:

$$\begin{aligned} \begin{bmatrix} 1 & 0 & 0 & \textrm{offset}_x \\ 0 & 1 & 0 & \textrm{offset}_y \\ 0 & 0 & 1 & \textrm{offset}_z \end{bmatrix} \end{aligned}$$

For the time being, you should think of a matrix as nothing more than a stack of dot product operands. Each row is a vector that will be dotted with \(\mathbf{p}\). To compute the dot products, we perform a matrix-vector multiplication, which has this mathematical notation:

$$\begin{aligned} \mathbf{p'} &= \begin{bmatrix} 1 & 0 & 0 & \textrm{offset}_x \\ 0 & 1 & 0 & \textrm{offset}_y \\ 0 & 0 & 1 & \textrm{offset}_z \end{bmatrix} \times \mathbf{p} \\ \end{aligned}$$

When we explode the vectors, we see that \(\mathbf{p}\) has a homogeneous coordinate, but \(\mathbf{p'}\) does not:

$$\begin{aligned} \begin{bmatrix}p'_x \\ p'_y \\ p'_z\end{bmatrix} &= \begin{bmatrix} 1 & 0 & 0 & \textrm{offset}_x \\ 0 & 1 & 0 & \textrm{offset}_y \\ 0 & 0 & 1 & \textrm{offset}_z \end{bmatrix} \times \begin{bmatrix}p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\ \end{aligned}$$

The output from one transformation will often be input to another, so we want \(\mathbf{p'}\) to have a homogeneous coordinate too. Instead of explicitly tacking on the 1, we'll expand the matrix with a fourth row to automatically produce it. The fourth row will be dotted with \(\mathbf{p}\) just like the others. It will have three zeroes to cancel out \(p_x\), \(p_y\), and \(p_z\) and a 1 for the homogeneous coordinate's coefficient:

$$\begin{aligned} \begin{bmatrix}p'_x \\ p'_y \\ p'_z \\ 1\end{bmatrix} &= \begin{bmatrix} 1 & 0 & 0 & \textrm{offset}_x \\ 0 & 1 & 0 & \textrm{offset}_y \\ 0 & 0 & 1 & \textrm{offset}_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix}p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\ \end{aligned}$$

This equation is the standard form for translating 3D points. It is fast and compactly expressed as a single operation in most graphics libraries, including GLSL. Even though the positions are 3D, the matrix has four rows and four columns to accommodate the homogeneous coordinate.

The overhead of building these matrices is only worth it if we can express all our transformations as 4-by-4 matrices. Let's try scaling next.

← Translate with DotsScale Matrix →