modelMatrix: Rotation, Translation and Scaling

Transformations

There are three kind of basic linear transformation, we call this affine transformation.

Translation

For every vertex coordinate of shape SS, point p(x,y,z)p(x, y, z) apply a transition and get point p(x,y,z)p(x', y', z')

So we have

Equation 1.0

x=x+Txy=y+Tyz=z+Tzx' = x + T_x \\ y' = y + T_y \\ z' = z + T_z

In this case Tz is 00, very intuitive!

Rotation

First, let’s assume we are rotating around z-axis. Pick any point pp. point pp move to point pp' after the rotation.

We have

Equation 1.1

y=rsinαy = r\sin\alpha
Similarly
x=rcos(α+β)y=rsin(α+β)x'= r\cos(\alpha + \beta) \\\\ y'= r\sin(\alpha + \beta)
Use the addition theorem of trigonometric functions
x=r(cosαcosβsinαsinβ)y=r(sinαcosβ+cosαsinβ)x'= r(\cos\alpha\cos\beta - \sin\alpha\sin\beta) \\\\ y'= r(\sin\alpha\cos\beta + \cos\alpha\sin\beta)
By assigning Equation 1.1, you get the following expressions

Equation 1.2

x=xcosβysinβy=xsinβ+ycosβz=zx'= x\cos\beta-y\sin\beta \\\\ y'= x\sin\beta + y\cos\beta \\\\ z = z'

Scaling

Clearly, We have

Equation 1.3

x=Sx×xy=Sy×yz=Sz×zx'= S_x \times x \\\\ y'= S_y \times y \\\\ z = S_z \times z

Matrices

You will find it time consuming to write a mathematical expression every time you need a new set of transformation. To solve this problem, we introduce a new tool: Matrix.

Rotation Matrix

First, there is transformation

[xyz]=[abcdefghi]×[xyz]\begin{bmatrix} x' \\ y' \\ z' \end{bmatrix} = \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} \times \begin{bmatrix} x \\ y \\ z \end{bmatrix}

For now, you just hard code how matrix multiplication works, you could learn linear algebra later.

x=ax+by+czy=dx+ey+fzz=gx+hy+izx'= ax + by + cz \\\\ y'= dx + ey + fz \\\\ z' = gx + hy + iz

comparing with Equation 1.2, we know that a=cosβa=\cos\beta, b=sinβb=-\sin\beta and c=0c=0. Also, d=sinβd=\sin\beta, e=cosβe=\cos\beta f=0f=0 g=0g=0 h=0h=0 i=1i=1, then we have matrix

[xyz]=[cosβsinβ0sinβcosβ0001]×[xyz]\begin{bmatrix} x' \\ y' \\ z' \end{bmatrix} = \begin{bmatrix} \cos\beta & -\sin\beta & 0 \\ \sin\beta & \cos\beta & 0 \\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} x \\ y \\ z \end{bmatrix}

We call this a transformation matrix

Translation Matrix

What about translation? You can’t product x=x+Txx = x + T_x by 3x3 matrix. Again, yuo have to hard code the resolution. the answer is to use to 4x4 matrix called Homogeneous Transformation Matrix

[xyz1]=[100Tx010Ty001Tx0001]×[xyz1]\begin{bmatrix} x' \\ y' \\ z' \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_x \\ 0 & 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}

You might wonder why there are four coordinates for p(x,y,z)p(x, y, z), this is called Homogeneous coordinates and you can ignore it.

Scaling Matrix

We can get a scaling matrix:

[xyz1]=[Sx0000Sy0000Sz00001]×[xyz1]\begin{bmatrix} x' \\ y' \\ z' \\ 1 \end{bmatrix} = \begin{bmatrix} S_x & 0 & 0 & 0 \\ 0 & S_y & 0 & 0 \\ 0 & 0 & S_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix}

modelMatrix

Say we want to do a translation TT, then rotation RR followed by scaling SS. First we make a translation matrix multiply origin Matrix AA

step1:

TAT \cdot A

step2:

RTAR \cdot T \cdot A

step3:

SRTAS \cdot R \cdot T \cdot A

An important thing you need to know is that

SRARSA(SR)TA=S(RT)AS \cdot R \cdot A \neq R \cdot S \cdot A \\ (S \cdot R) \cdot T \cdot A = S \cdot (R \cdot T) \cdot A

means that matrix multiplication is associative but not commutative. We call this kind of matrix M=SRTM = S \cdot R \cdot T modelMatrix