This article is a derivative of Homogeneous Coordinates and Matrix Derivation of Basic Geometric Transformations in 2D Graphics . Understanding 2D transformations makes it easy to derive 3D transformations.
Basic Geometric Transformations in 3D Graphics#
Translation#
Describes the transformation from point (x, y, z) to (x + dx, y + dy, z + dz).
Introducing homogeneous coordinates, it can be expressed as (x, y, z, 1) and transformed to (x + dx, y + dy, z + dz, 1).
Given:
Copy ( 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 ) ( x y z 1 ) = ( x + d x y + d y z + d z 1 ) \begin{pmatrix}
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{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
x + d_x \\
y + d_y \\
z + d_z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x + d x y + d y z + d z 1
We have:
Copy { a 11 x + a 12 y + a 13 z + a 14 = x + d x a 21 x + a 22 y + a 23 z + a 24 = y + d y a 31 x + a 32 y + a 33 z + a 34 = z + d z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = x + d_x \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = y + d_y \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = z + d_z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = x + d x a 21 x + a 22 y + a 23 z + a 24 = y + d y a 31 x + a 32 y + a 33 z + a 34 = z + d z a 41 x + a 42 y + a 43 z + a 44 = 1
Solving for:
Copy ( 1 0 0 d x 0 1 0 d y 0 0 1 d z 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & dx \\
0 & 1 & 0 & dy \\
0 & 0 & 1 & dz \\
0 & 0 & 0 & 1
\end{pmatrix} 1 0 0 0 0 1 0 0 0 0 1 0 d x d y d z 1
Scaling#
Describes the transformation from point (x, y, z) to (sx * x, sy * y, sz * z), where sx, sy, sz are constants.
Introducing homogeneous coordinates, it can be expressed as (x, y, z, 1) and transformed to (sx * x, sy * y, sz * z, 1).
Given:
Copy ( 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 ) ( x y z 1 ) = ( s x ∗ x s y ∗ y s z ∗ z 1 ) \begin{pmatrix}
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{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
s_x * x \\
s_y * y \\
s_z * z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = s x ∗ x s y ∗ y s z ∗ z 1
We have:
Copy { a 11 x + a 12 y + a 13 z + a 14 = s x ∗ x a 21 x + a 22 y + a 23 z + a 24 = s y ∗ y a 31 x + a 32 y + a 33 z + a 34 = s z ∗ z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = s_x * x \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = s_y * y \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = s_z * z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = s x ∗ x a 21 x + a 22 y + a 23 z + a 24 = s y ∗ y a 31 x + a 32 y + a 33 z + a 34 = s z ∗ z a 41 x + a 42 y + a 43 z + a 44 = 1
Solving for:
Copy ( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) \begin{pmatrix}
s_x & 0 & 0 & 0 \\
0 & s_y & 0 & 0 \\
0 & 0 & s_z & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1
Rotation#
For 3D rotation, it can be decomposed into the product of three matrices (rotation around the x-axis, rotation around the y-axis, rotation around the z-axis).
Rotation around the z-axis —— Rz (β)#
Do you remember the derivation of 2D rotation? When we rotate on the plane formed by the x and y axes, it can also be viewed as rotation around the z-axis. So the 3D rotation is easy to understand, it's just adding an additional dimension that remains fixed.
That is:
Copy ( 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 ) ( x y z 1 ) = ( x c o s β − y s i n β y c o s β + x s i n β z 1 ) \begin{pmatrix}
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{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
xcosβ - ysinβ \\
ycosβ + xsinβ \\
z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x cos β − ys in β ycos β + x s in β z 1
We have:
Copy { a 11 x + a 12 y + a 13 z + a 14 = x c o s β − y s i n β a 21 x + a 22 y + a 23 z + a 24 = y c o s β + x s i n β a 31 x + a 32 y + a 33 z + a 34 = z a 41 x + a 42 y + a 43 z + a 44 = 1 \begin{cases}
a_{11}x + a_{12}y + a_{13}z + a_{14} = xcosβ - ysinβ \\
a_{21}x + a_{22}y + a_{23}z + a_{24} = ycosβ + xsinβ \\
a_{31}x + a_{32}y + a_{33}z + a_{34} = z \\
a_{41}x + a_{42}y + a_{43}z + a_{44} = 1 \\
\end{cases} ⎩ ⎨ ⎧ a 11 x + a 12 y + a 13 z + a 14 = x cos β − ys in β a 21 x + a 22 y + a 23 z + a 24 = ycos β + x s in β a 31 x + a 32 y + a 33 z + a 34 = z a 41 x + a 42 y + a 43 z + a 44 = 1
Solving for:
Copy R z β = ( c o s β − s i n β 0 0 s i n β c o s β 0 0 0 0 1 0 0 0 0 1 ) R_{z}β =
\begin{pmatrix}
cosβ & -sinβ & 0 & 0 \\
sinβ & cosβ & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R z β = cos β s in β 0 0 − s in β cos β 0 0 0 0 1 0 0 0 0 1
Rotation around the x-axis —— Rx (α)#
Similarly (keeping x fixed):
Copy R x α = ( 1 0 0 0 0 c o s α − s i n α 0 0 s i n α c o s α 0 0 0 0 1 ) R_{x}α =
\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & cosα & -sinα & 0 \\
0 & sinα & cosα & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R x α = 1 0 0 0 0 cos α s in α 0 0 − s in α cos α 0 0 0 0 1
Rotation around the y-axis —— Ry (γ)#
Similarly (keeping y fixed):
Copy R y γ = ( c o s γ 0 s i n γ 0 0 1 0 0 − s i n γ 0 c o s γ 0 0 0 0 1 ) R_{y}γ =
\begin{pmatrix}
cosγ & 0 & sinγ & 0 \\
0 & 1 & 0 & 0 \\
-sinγ & 0 & cosγ & 0 \\
0 & 0 & 0 & 1
\end{pmatrix} R y γ = cos γ 0 − s inγ 0 0 1 0 0 s inγ 0 cos γ 0 0 0 0 1
So we have the rotation matrix in 3D:
Copy R = R x α R y γ R z β R = R_{x}αR_{y}γR_{z}β R = R x α R y γ R z β
But you may wonder how to decompose the rotation around an axis θ into α, β, γ? Let's leave this question for another article.
However, currently the rotation axes are limited to passing through the origin. If the rotation axis does not pass through the origin, we need to first move the axis to the origin, then rotate, and finally move it back to its original position. Any complex transformation can be decomposed into the composition of several simple transformations.
Shearing#
Shearing along the X-axis#
Describes the transformation from (x, y, z) to (x + m * y + n * z, y, z), where m and n are tangent functions.
Copy ( 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 ) ( x y z 1 ) = ( x + m ∗ y + n ∗ z y z 1 ) \begin{pmatrix}
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{pmatrix}
\begin{pmatrix}
x \\
y \\
z \\
1
\end{pmatrix}
=
\begin{pmatrix}
x + m * y + n * z \\
y \\
z \\
1
\end{pmatrix} a 11 a 21 a 31 a 41 a 12 a 22 a 32 a 42 a 13 a 23 a 33 a 43 a 14 a 24 a 34 a 44 x y z 1 = x + m ∗ y + n ∗ z y z 1
Solving for:
Copy ( 1 m n 0 0 1 0 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix}
1 & m & n & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 0 0 0 m 1 0 0 n 0 1 0 0 0 0 1
Shearing along the Y-axis#
Describes the transformation from (x, y, z) to (x, y + m * x + n * z, z).
Skipping the solution, I believe you can understand the meaning of the row and column terms.
Copy ( 1 0 0 0 m 1 n 0 0 0 1 0 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & 0 \\
m & 1 & n & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 m 0 0 0 1 0 0 0 n 1 0 0 0 0 1
Shearing along the Z-axis#
Describes the transformation from (x, y, z) to (x, y, z + m * x + n * y).
Copy ( 1 0 0 0 0 1 0 0 m n 1 0 0 0 0 1 ) \begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
m & n & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{pmatrix} 1 0 m 0 0 1 n 0 0 0 1 0 0 0 0 1