Computer Graphics - Matrix
In computer graphics, matrices are powerful mathematical tools used to transform objects in 2D or 3D space. They provide a compact and efficient way to apply transformations such as translation, rotation, and scaling to points or entire models. OpenGL uses matrices extensively to control how objects are positioned and oriented in the scene.
What Is a Matrix?
A matrix is a rectangular grid of numbers arranged in rows and columns. In graphics, we commonly use 4×4 matrices for 3D transformations (and 3×3 matrices for 2D).
Example of a 4×4 matrix:
\[\begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \end{bmatrix}\]In OpenGL, we use column-major order, meaning columns are stored in memory before rows.
How a Matrix Transforms a Vector
To move a vertex (or vector) in 3D space, we multiply it by a transformation matrix:
\[\mathbf{v'} = M \times \mathbf{v}\]where:
- v = the original position (a 4D vector: $[x, y, z, 1]$)
- M = the transformation matrix
- v′ = the transformed position
This allows us to apply multiple transformations (like rotate → scale → move) through matrix multiplication.
Types of Transformations
Translation
Moves an object along the X, Y, or Z axis.
\[T(x, y, z) = \begin{bmatrix} 1 & 0 & 0 & x \\ 0 & 1 & 0 & y \\ 0 & 0 & 1 & z \\ 0 & 0 & 0 & 1 \end{bmatrix}\]When multiplied by a vertex, it adds $(x, y, z)$ to its position.
Scaling
Changes the size of an object along each axis.
\[S(s_x, s_y, s_z) = \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]For example, scaling by $(2, 2, 2)$ doubles the size of the object.
Rotation
Rotates an object around an axis (X, Y, or Z).
Rotate around the X-axis:
\[R_x(\theta) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]Rotate around the Y-axis:
\[R_y(\theta) = \begin{bmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]Rotate around the Z-axis:
\[R_z(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]Matrix–Vector Multiplication
In OpenGL, a vertex (or point in space) is represented as a 4D vector:
\[\mathbf{v} = \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}\]A transformation matrix (such as a model, view, or projection matrix) is a 4×4 matrix:
\[M = \begin{bmatrix} m_{11} & m_{12} & m_{13} & m_{14} \\ m_{21} & m_{22} & m_{23} & m_{24} \\ m_{31} & m_{32} & m_{33} & m_{34} \\ m_{41} & m_{42} & m_{43} & m_{44} \end{bmatrix}\]When we multiply the matrix by the vector, we get a new transformed vector:
\[\mathbf{v'} = M \times \mathbf{v}\]or expanded:
\[\begin{bmatrix} x' \\ y' \\ z' \\ w' \end{bmatrix} = \begin{bmatrix} m_{11}x + m_{12}y + m_{13}z + m_{14} \\ m_{21}x + m_{22}y + m_{23}z + m_{24} \\ m_{31}x + m_{32}y + m_{33}z + m_{34} \\ m_{41}x + m_{42}y + m_{43}z + m_{44} \end{bmatrix}\]Each component of the resulting vector is a weighted combination of the original coordinates.
This operation allows a single matrix to perform translation, rotation, scaling, or all of them at once.
Combining Transformations
You can combine transformations by multiplying matrices.
For example:
\[M = T \times R \times S\]The order matters — matrix multiplication is not commutative!
In OpenGL, the last transformation specified is applied first to the object.
Common Matrices in OpenGL
OpenGL typically uses three main transformation matrices:
- Model Matrix – transforms local object coordinates into world space.
- View Matrix – represents the camera position and orientation.
- Projection Matrix – converts 3D coordinates to 2D screen space.
Together, they form the MVP matrix (Model × View × Projection):
\[\text{gl_Position} = Projection \times View \times Model \times \text{vec4(position, 1.0)};\]Summary
- Matrices represent transformations in a consistent, compact way.
- Multiplying a vector by a matrix moves it through space.
- Translation moves, rotation spins, and scaling resizes.
- Multiple transformations combine through matrix multiplication.
- OpenGL uses 4×4 matrices for all 3D transformations.
Tip: Understanding matrices is key to mastering OpenGL. They define how every object moves, rotates, and scales in your 3D world!