GAMES101-04 Transformation Cont

3D Transformations

Add a fouth coordinate (w-coordinate)

  • 3D point = \((x, y, z, 1)^T\)
  • 3D vector = \((x, y, z, 0)^T\)

In general, (x, y, z, w)(w != 0) is the 3d point: (x/w, y/w, z/w)

Use 4×4 matrices for affine transformations

\[ \left(\begin{array}{c} x^{\prime} \\ y^{\prime} \\ z^{\prime} \\ 1 \end{array}\right)=\left(\begin{array}{llll} a & b & c & t_{x} \\ d & e & f & t_{y} \\ g & h & i & t_{z} \\ 0 & 0 & 0 & 1 \end{array}\right) \cdot\left(\begin{array}{c} x \\ y \\ z \\ 1 \end{array}\right) \]

What's the order?

Linear Transform first or Translation first?

Rotation around x-, y-, or z-axis

20220527182842

\[ \begin{array}{l} \mathbf{R}_{x}(\alpha)=\left(\begin{array}{cccc} 1 & 0 & 0 & 0 \\ 0 & \cos \alpha & -\sin \alpha & 0 \\ 0 & \sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 0 & 1 \end{array}\right) \\ \mathbf{R}_{y}(\alpha)=\left(\begin{array}{cccc} \cos \alpha & 0 & \sin \alpha & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \alpha & 0 & \cos \alpha & 0 \\ 0 & 0 & 0 & 1 \end{array}\right) \\ \mathbf{R}_{z}(\alpha)=\left(\begin{array}{cccc} \cos \alpha & -\sin \alpha & 0 & 0 \\ \sin \alpha & \cos \alpha & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right) \end{array} \]

3D Rotations

Compose any 3D rotation from Rx, Ry, Rz?

\[ \mathbf{R}_{x y z}(\alpha, \beta, \gamma)=\mathbf{R}_{x}(\alpha) \mathbf{R}_{y}(\beta) \mathbf{R}_{z}(\gamma) \]

  • So-called Euler angles
  • Often used in flight simulators: roll, pitch, yaw

20220527183141

Rodrigues’ Rotation Formula

Rotation by angle α around axis n

\[ \mathbf{R}(\mathbf{n}, \alpha) =\cos (\alpha) \mathbf{I} +(1-\cos (\alpha)) \mathbf{n} \mathbf{n}^{T} +\sin (\alpha) \underbrace{ \left(\begin{array}{ccc} 0 & -n_{z} & n_{y} \\ n_{z} & 0 & -n_{x} \\ -n_{y} & n_{x} & 0 \end{array}\right) }_{\mathbf{N}} \]

View/Canera Transformation

What is view transformation?

Think about how to take a photo

  • Find a good place and arrange people (model transformation)
  • Find a good “angle” to put the camera (view transformation)
  • Cheese! (projection transformation)

Define the camera

20220527184850

  • Position
  • Look-at / gaze direction
  • Up direction (assuming perp. to look-at)

we always transform the camera to

  • The origin, up at Y, look at -Z
  • And transform the objects along with the camera

the camera from xy-z to etg, is equal to the object form etg to xy-z

20220527185318

  • Translates e to origin
  • Rotates g to -Z
  • Rotates t to Y
  • Rotates (g x t) To X
  • Difficult to write!

\(M_{view}\) in math

  • Let’s write\(M_{view}=R_{view}T_{view}\)
  • Translate e to origin

\[ T_{\text {view }}=\left[\begin{array}{cccc} 1 & 0 & 0 & -x_{e} \\ 0 & 1 & 0 & -y_{e} \\ 0 & 0 & 1 & -z_{e} \\ 0 & 0 & 0 & 1 \end{array}\right] \]

  • Rotate g to -Z, t to Y, (g x t) To X
  • Consider its inverse rotation: X to (g x t), Y to t, Z to -g

\[ R_{\text {view }}^{-1}=\left[\begin{array}{cccc} x_{\hat{g} \times \hat{t}} & x_{t} & x_{-g} & 0 \\ y_{\hat{g} \times \hat{t}} & y_{t} & y_{-g} & 0 \\ z_{\hat{g} \times \hat{t}} & z_{t} & z_{-g} & 0 \\ 0 & 0 & 0 & 1 \end{array}\right] \quad \quad R_{v i e w}=\left[\begin{array}{cccc} x_{\hat{g} \times \hat{t}} & y_{\hat{g} \times \hat{t}} & z_{\hat{g} \times \hat{t}} & 0 \\ x_{t} & y_{t} & z_{t} & 0 \\ x_{-g} & y_{-g} & z_{-g} & 0 \\ 0 & 0 & 0 & 1 \end{array}\right] \\ \quad M_{v i e w}=R_{view}T_{view}= \left[\begin{array}{cccc} x_{\hat{g} \times \hat{t}} & y_{\hat{g} \times \hat{t}} & z_{\hat{g} \times \hat{t}} & -x_{e} \\ x_{t} & y_{t} & z_{t} & -x_{e} \\ x_{-g} & y_{-g} & z_{-g} & -x_{e} \\ 0 & 0 & 0 & 1 \end{array}\right] \]

Projection Transformation

  • 3D to 2D
  • Orthographic projection
  • Perspective projection

20220527190332

Orthographic Projection

  • Camera located at origin, looking at -Z, up at Y
  • Drop Z coordinate
  • Translate and scale the resulting rectangle to [-1, 1]

20220527190517

Translate (center to origin) first, then scale (length/width/height to 2)

\[ M_{\text {ortho }}=\left[\begin{array}{cccc} \frac{2}{r-l} & 0 & 0 & 0 \\ 0 & \frac{2}{t-b} & 0 & 0 \\ 0 & 0 & -\frac{2}{f-n} & 0 \\ 0 & 0 & 0 & 1 \end{array}\right]\left[\begin{array}{cccc} 1 & 0 & 0 & -\frac{r+l}{2} \\ 0 & 1 & 0 & -\frac{t+b}{2} \\ 0 & 0 & 1 & -\frac{n+f}{2} \\ 0 & 0 & 0 & 1 \end{array}\right] \]

20220527190642

Perspective Projection

  • Most common in Computer Graphics, art, visual system
  • Further objects are smaller
  • Parallel lines not parallel; converge to single point

How to do perspective projection

  • First “squish” the frustum into a cuboid (n -> n, f -> f) (Mpersp->ortho)
  • Do orthographic projection (Mortho, already known!)

20220527191041

20220527191318

\[ M_{\text {persp } \rightarrow \text { ortho }}=\left(\begin{array}{cccc} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \end{array}\right) \]

What’s next? \(M_{\text {persp }}=M_{\text {ortho }} M_{\text {persp } \rightarrow \text { ortho }}\)