Translate With Dots

How to 3D

Chapter 4: Transformation Machinery

Translate With Dots

Earlier we saw that to translate a vector \(\mathbf{p}\) is to add it component-wise with a vector of offsets. Translation has this vector notation:

$$\begin{aligned} p'_x &= p_x + \textrm{offset}_x \\ p'_y &= p_y + \textrm{offset}_y \\ p'_z &= p_z + \textrm{offset}_z \end{aligned}$$

Those right-hand sides don't look like dot products at all. Nevertheless, as with scaling and rotation, we want to rewrite the equations above to use dot products. They will have this form:

$$\begin{aligned} p'_x &= \mathbf{a} \cdot \mathbf{p} \\ p'_y &= \mathbf{b} \cdot \mathbf{p} \\ p'_z &= \mathbf{c} \cdot \mathbf{p} \end{aligned}$$

Let's again expand out these equations to see how the dot products might be constructed:

$$\begin{aligned} p'_x &= a_x \times p_x + a_y \times p_y + a_z \times p_z \\ p'_y &= b_x \times p_x + b_y \times p_y + b_z \times p_z \\ p'_z &= c_x \times p_x + c_y \times p_y + c_z \times p_z \\ \end{aligned}$$

Neither \(p_y\) nor \(p_z\) are involved in the scalar definition of \(p'_x\), so we zero out their coefficients in \(\mathbf{a}\). The coefficient for \(p_x\) is an implicit 1. That leads us to this possible definition:

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

But that can't be right. There's no mention of \(\textrm{offset}_x\) anywhere. There's not even a place to introduce it. Perhaps translation cannot be recast as a dot product? Fear not. To make translation fit in the dot product machinery, we will bend the rules. From out of the blue, we will add 1 as a fourth component of \(\mathbf{p}\):

$$\mathbf{p} = \begin{bmatrix}p_x & p_y & p_z & 1\end{bmatrix}$$

This special fourth component is called the homogeneous coordinate. We've seen it at work already in vertex shaders when we turn the 3-vector position into the 4-vector gl_Position:

gl_Position = vec4(position, 1.0);
gl_Position = vec4(position, 1.0);

The 1 serves as a coefficient for \(\textrm{offset}_x\). It allows us to write \(p'_x\) as this dot product:

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

Components \(p_y\) and \(p_z\) are expressed similarly. Altogether, translation has this dot product form:

$$\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}$$

By adding a homogeneous coordinate, we've recast translation as a series of dot products. The next step is to get the graphics card to evaluate all of the dot products in parallel.

← Rotate with DotsTranslation Matrix →