### 引言

GAMES101现代图形学入门是由闫令琪老师教授。这节课继续讲解三维变换，并且主要来说相对比较困难的观测变换，包括视图、投影、正交投影以及透视。

### 3D Transforms

Use homogeneous coordinates again:
• 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

$\begin{pmatrix}x’ \\ y’ \\ z’ \\ 1\end{pmatrix} = \begin{pmatrix}a & b & c & t_x \\ d & e & f & t_y \\ g & h & i & t_z \\ 0 & 0 & 0 & 1\end{pmatrix} \cdot \begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix}$

What’s the order?

Linear Transform first or Translation first?

Scale：$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}$

Translation：$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}$

#### 3D Rotations

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

$R_{xyz}(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma)$

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

#### Rodrigues’ Rotation Formula

Rotation by angle α around axis n：

$R(n,\alpha)=\cos{(\alpha)}I+(1-\cos{(\alpha)})nn^T+\sin{(\alpha)}\begin{pmatrix}0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0\end{pmatrix}$

How to prove this magic formula?

Check out the supplementary material on the course website!

### View transformation

#### View/Camera 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)

• 首先找一个好的地方搭好场景摆好pose（模型变换）
• 第二步找到好的角度/位置将相机放置好（视图变换）
• 下一步是“茄子！”可以拍照（投影变换）

How to perform view transformation?

Define the camera first

• Position $\vec e$

• Look-at / gaze direction $\hat g$

• Up direction $\hat t$

(assuming perp. to look-at)

Key observation

• If the camera and all objects move together,

the “photo” will be the same

How about that we always transform the camera to

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

Transform the camera by $M_{view}$

• So it’s located at the origin, up at Y, look at Z

Mview in math?

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

• 中心从$\vec e$平移到原点
• 观察方向由$\hat g$旋转到-Z
• 向上方向由$\hat t$旋转到Y
• 将$\hat g \times \hat t$旋转至X

$M_{view}$ in math?

• Let’s write $M_{view} = R_{view}T_{view}$

• Translate e to origin

$T_{view}=\begin{bmatrix}1 & 0 & 0 & -x_e \\ 0 & 1 & 0 & -y_e \\ 0 & 0 & 1 & -z_e \\ 0 & 0 & 0 & 1\end{bmatrix}$

• 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

Summary

• Transform objects together with the camera
• Until camera’s at the origin, up at Y, look at -Z

Also known as ModelView Transformation

But why do we need this?

• For projection transformation!

#### Projection Transformation

Projection in Computer Graphics

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

Perspective projection vs. orthographic projection

##### Orthographic Projection

A simple way of understanding

• Camera located at origin, looking at -Z, up at Y (looks familiar?)
• Drop Z coordinate
• Translate and scale the resulting rectangle to $[-1, 1]^2$

In general

• We want to map a cuboid $[l, r] \times [b, t] \times [f, n]$ to the “canonical (正则、规范、标准)” cube $[-1, 1]^3$Slightly different orders (to the “simple way”)

• Center cuboid by translating

• Scale into “canonical” cube

Transformation matrix?

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

$M_{ortho} = \begin{bmatrix}\frac{2}{r-l} & 0 & 0 & 0 \\ 0 & \frac{2}{t-b} & 0 & 0 \\ 0 & 0 & \frac{2}{n-f} & 0 \\ 0 & 0 & 0 & 1\end{bmatrix}\begin{bmatrix}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{bmatrix}$

Caveat

• Looking at / along -Z is making near and far not intuitive (n > f)
• FYI: that’s why OpenGL (a Graphics API) uses left hand coords.

#### Perspective Projection

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

Before we move on

Recall: property of homogeneous coordinates

• $(x, y, z, 1), (kx, ky, kz, k != 0), (xz, yz, z^2, z != 0)$ all represent the same point $(x, y, z)$ in 3D
• e.g. $(1, 0, 0, 1)$ and $(2, 0, 0, 2)$ both represent $(1, 0, 0)$

Simple, but useful

How to do perspective projection

• First “squish” the frustum into a cuboid (n -> n, f -> f) ($M_{persp \to ortho}$)
• Do orthographic projection ($M_{ortho}$, already known!)

In order to find a transformation

• Recall the key idea: Find the relationship between transformed points (x’, y’, z’) and the original points (x, y, z)

In order to find a transformation

• Find the relationship between transformed points (x’, y’, z’) and the original points (x, y, z)

$y’ = \frac{n}{z}y$ $\space x’ = \frac{n}{z}x$ (similar to y’)

In homogeneous coordinates,

$\begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix} \to \begin{pmatrix}nx/z \\ ny/z \\ unknown \\ 1\end{pmatrix} == \begin{pmatrix}nx \\ ny \\ still unknown \\ z\end{pmatrix}$

So the “squish” (persp to ortho) projection does this

$M^{(4 \times 4)}_{persp \to ortho} \begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix} = \begin{pmatrix}nx \\ ny \\ unknown \\ z\end{pmatrix}$

Already good enough to figure out part of $M_{persp->ortho}$

$M_{persp \to ortho} = \begin{pmatrix}n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ ? & ? & ? & ? \\ 0 & 0 & 1 & 0\end{pmatrix}$

How to figure out the third row of $M_{persp \to ortho}$

• Any information that we can use?

Observation: the third row is responsible for z’

• Any point on the near plane will not change
• Any point’s z on the far plane will not change

Any point on the near plane will not change

$M^{(4 \times 4)}_{persp \to ortho}\begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix} = \begin{pmatrix}nx \\ ny \\ unknown \\ z\end{pmatrix} \to \begin{pmatrix}x \\ y \\ n \\ 1\end{pmatrix} == \begin{pmatrix}nx \\ ny \\ n^2 \\ n\end{pmatrix}$

So the third row must be of the form (0 0 A B)

$\begin{pmatrix}0 & 0 & A & B\end{pmatrix}\begin{pmatrix}x \\ y \\ n \\ 1\end{pmatrix} = n^2$

What do we have now?

$\begin{pmatrix}0 & 0 & A & B\end{pmatrix}\begin{pmatrix}x \\ y \\ n \\ 1\end{pmatrix} = n^2 \to An + B = n^2$

Any point’s z on the far plane will not change

$\begin{pmatrix}0 \\ 0 \\ f \\ 1\end{pmatrix} \to \begin{pmatrix}0 \\ 0 \\ f \\ 1\end{pmatrix} == \begin{pmatrix}0 \\ 0 \\ f^2 \\ f\end{pmatrix} \to Af+b=f^2$

Solve for A and B

Finally, every entry in $M_{persp \to ortho}$ is known!

What’s next?

• Do orthographic projection (Mortho) to finish
• $M_{persp} = M_{ortho}M_{persp \to ortho}$