Matrix Transformations in Computer Graphics

In computer graphics, matrices are fundamental tools used to transform objects in 2D and 3D space. These transformations include translation, rotation, and scaling . This page explains how matrices enable these operations with mathematical examples and an interactive app.

Homogeneous Coordinates

Before diving into transformations, we need to understand homogeneous coordinates. In a 3D system, we represent a point as \((x, y, z, 1)\) and a vector as \((x, y, z, 0)\). This allows us to represent both transformations and translations using matrices.

Basic Transformations

1. Translation

Translation moves an object by a displacement vector \((t_x, t_y, t_z)\). The translation matrix is:

\[ T(t_x, t_y, t_z) = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

For a point \(P = (x, y, z, 1)\), the translated point \(P'\) is:

\[ P' = T \cdot P = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} x + t_x \\ y + t_y \\ z + t_z \\ 1 \end{pmatrix} \]

2. Rotation

Rotation transforms an object around an axis. Here are rotation matrices for the three primary axes:

Rotation around X-axis by angle \(\theta\):

\[ R_x(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation around Y-axis by angle \(\theta\):

\[ R_y(\theta) = \begin{pmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation around Z-axis by angle \(\theta\):

\[ R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

3. Scaling

Scaling changes the size of an object by factors \((s_x, s_y, s_z)\). The scaling matrix is:

\[ S(s_x, s_y, s_z) = \begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Complex Example: Composite Transformations

In practice, we often apply multiple transformations in sequence. Matrix multiplication allows us to combine these operations efficiently.

Let's consider rendering a cube with vertices at \((\pm 1, \pm 1, \pm 1)\).

We want to transform a cube as follows:

  1. Scale the cube by factors (2, 1, 0.5)
  2. Rotate it 45\(^\circ{}\) around the Y-axis
  3. Translate it by (10, 5, -3)

The composite transformation matrix would be:

\[ M = T(10, 5, -3) \cdot R_y(45^\circ{}) \cdot S(2, 1, 0.5) \]

Substituting the matrices:

\[ M = \begin{pmatrix} 1 & 0 & 0 & 10 \\ 0 & 1 & 0 & 5 \\ 0 & 0 & 1 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} \cos(45^{\circ}) & 0 & \sin(45^{\circ}) & 0 \\ 0 & 1 & 0 & 0 \\ -\sin(45^{\circ}) & 0 & \cos(45^{\circ}) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 2 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0.5 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Using \(\cos(45^{\circ}) = \sin(45^{\circ}) = \frac{\sqrt{2}}{2} \approx 0.7071\):

\[ M = \begin{pmatrix} 1 & 0 & 0 & 10 \\ 0 & 1 & 0 & 5 \\ 0 & 0 & 1 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 0.7071 & 0 & 0.7071 & 0 \\ 0 & 1 & 0 & 0 \\ -0.7071 & 0 & 0.7071 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 2 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0.5 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Multiplying the last two matrices first:

\[ R_y(45^{\circ}) \cdot S(2, 1, 0.5) = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 0 \\ 0 & 1 & 0 & 0 \\ -1.4142 & 0 & 0.3536 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Then multiplying with the translation matrix:

\[ M = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 10 \\ 0 & 1 & 0 & 5 \\ -1.4142 & 0 & 0.3536 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Applying all the three transformations to the vertex \( (1,1,1) \) for example, we obtain a new vertex at : \[ M \cdot \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 10 \\ 0 & 1 & 0 & 5 \\ -1.4142 & 0 & 0.3536 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix}11.7678\\ 6\\ -4.0606\\ 1\end{pmatrix} \]

Note: In computer graphics, transformations are typically applied from right to left in matrix multiplication, contrary to the order we'd describe the operations in English.

Conclusion

Matrices provide an elegant and efficient way to represent and combine transformations in computer graphics. By using homogeneous coordinates and matrix multiplication, we can easily implement complex 3D scenes with rotation, translation, and scaling, all fundamental operations in modern rendering engines.

The power of the matrix approach is that any sequence of transformations can be combined into a single matrix, which can then be efficiently applied to all vertices in a 3D model.

In computer graphics, matrices are fundamental tools used to transform objects in 2D and 3D space. This interactive demonstration allows you to explore how different transformations affect a 3D object and see the corresponding matrices.

Interactive Demo

Transform the cube that is in the screen by rotating, translating and scaling it. Use one transformation at the time in order to better understand each transformation. The transformation matrices are also displayed at the bottom of the page.

Translation

Rotation (degrees)

Scale

Transformation Matrices

Homogeneous Coordinates

Before diving into transformations, we need to understand homogeneous coordinates. In a 3D system, we represent a point as \((x, y, z, 1)\) and a vector as \((x, y, z, 0)\). This allows us to represent both transformations and translations using matrices.

Basic Transformations

1. Translation

Translation moves an object by a displacement vector \((t_x, t_y, t_z)\). The translation matrix is:

\[ T(t_x, t_y, t_z) = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

2. Rotation

Rotation transforms an object around an axis. Here are rotation matrices for the three primary axes:

Rotation around X-axis by angle \(\theta\):

\[ R_x(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation around Y-axis by angle \(\theta\):

\[ R_y(\theta) = \begin{pmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation around Z-axis by angle \(\theta\):

\[ R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

3. Scaling

Scaling changes the size of an object by factors \((s_x, s_y, s_z)\). The scaling matrix is:

\[ S(s_x, s_y, s_z) = \begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]