### Transforms

• Many different coordinate systems in graphicsWorld, model, body, arms, …
• To relate them, we must transform between them
• Also, for modeling objects. I have a teapot, but
• Want to place it at correct location in the world
• Want to view it from different angles (HW 1)
• Want to scale it to make it bigger or smaller
• Demo of HW 1

#### 目标

• This unit is about the math for these transformations
• Represent transformations using matrices and matrixvector multiplications.
• Demos throughout lecture: HW 1 and Applet
• Transformations Game Applet

#### 基本内容

• Object in model coordinates
• Transform into world coordinates
• Represent points on object as vectors
• Multiply by matrices
• Demos with applet

• 2D transformations: rotation, scale, shear
• Composing transforms
• 3D rotations
• Translation: Homogeneous Coordinates (next time)
• Transforming Normals (next time)

#### 缩放变换

$Scale(s_x,s_y)= \begin{pmatrix}s_x & 0 \\ 0 & s_y \end{pmatrix} \qquad S^{-1} = \begin{pmatrix} s_x^{-1} & 0 \\ 0 & s_y^{-1}\end{pmatrix}$

$\begin{pmatrix}s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & s_z\end{pmatrix}\begin{pmatrix}x \\ y \\ z\end{pmatrix}=\begin{pmatrix}s_xx \\ s_yy \\ s_zz\end{pmatrix}$

#### 错切变换

$Shear=\begin{pmatrix}1 & a \\ 0 & 1\end{pmatrix} \qquad S^{-1}=\begin{pmatrix}1 & -a \\ 0 & 1\end{pmatrix}$

#### 旋转变换

• 2D simple, 3D complicated. [Derivation? Examples?]

• 2D?

$\begin{bmatrix}x’ \\ y’\end{bmatrix}=\begin{bmatrix}\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix}$

$R(X+Y)=R(X)+R(Y)$

• Linear

• Commutative

$o = (r\cos\theta,r\sin\theta)$

$o’=(r\cos{(\phi+\theta)}),r\sin{(\phi+\theta)}))$

$\cos{(\phi+\theta)}=\cos{\phi}\cos{\theta}-\sin{\phi}\sin{\theta}$

$\sin{(\phi+\theta)}=\sin{\phi}\cos{\theta}+\cos{\phi}\sin{\theta}$

$O’_x=r\cos{\phi}(O_x)\cos{\theta}-r\sin{\phi}(O_y)\sin{\theta}=x\cos{\theta}-y\sin{\theta}$

#### 组合变换

• Often want to combine transforms

• E.g. first scale by 2, then rotate by 45 degrees

• Advantage of matrix formulation: All still a matrix

• Not commutative!! Order matters

$x_3=Rx_2 \qquad x_2=Sx_1$

$x_3=R(Sx_1)=(RS)x_1$

$x_3\neq SRx_1$

• Say I want to invert a combination of 3 transforms

• Option 1: Find composite matrix, invert

• Option 2: Invert each transform and swap order

• Obvious from properties of matrices, demo

$M=M_1M_2M_3$

$M^{-1}=M_3^{-1}M_2^{-1}M_1^{-1}$

$M^{-1}M=M_3^{-1}(M_2^{-1}(M_1^{-1}M_1)M_2)M_3$

#### 三维旋转

Review of 2D case

$\begin{bmatrix}x’ \\ y’\end{bmatrix}=\begin{bmatrix}\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix}$

Orthogonal? $R^TR=I$

$\begin{pmatrix}\cos & \sin \\ -\sin & \cos\end{pmatrix}\begin{pmatrix}\cos & -\sin \\ \sin & \cos\end{pmatrix}=\begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix}$

$R_z=\begin{pmatrix}\cos & -\sin & 0 \\ \sin & \cos & 0 \\ 0 & 0 & 1\end{pmatrix} \qquad R_x = \begin{pmatrix}1 & 0 & 0 \\ 0 & \cos & -\sin \\ 0 & \sin & \cos \end{pmatrix}$

$R_y=\begin{pmatrix}\cos & 0 & \sin \\ 0 & 1 & 0 \\ -\sin & 0& \cos\end{pmatrix}$

Always linear, orthogonal $R^TR=I$

• Rows/cols orthonormal $R(X+Y)=R(X)+R(Y)$

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal vectors

$R_{uvw}=\begin{pmatrix}x_u & y_u & z_u \\ x_v & y_v & z_v \\ x_w & y_w & z_w\end{pmatrix} \qquad u=x_uX+y_uY+z_uZ$

$R_p=\begin{pmatrix}x_u & y_u & z_u \\ x_v & y_v & z_v \\ x_w & y_w & z_w\end{pmatrix}\begin{pmatrix}x_p \\ y_p \\ z_p\end{pmatrix}=?$

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal vectors

• Effectively, projections of point into new coord frame

• New coord frame uvw taken to cartesian components xyz

• Inverse or transpose takes xyz cartesian to uvw

• Not Commutative (unlike in 2D)!!
• Rotate by x, then y is not same as y then x
• Order of applying rotations does matter
• Follows from matrix multiplication not commutative
• R1 * R2 is not the same as R2 * R1
• Demo: HW1, order of right or up will matter

• Rotate by an angle θ about arbitrary axis a

• Homework 1: must rotate eye, up direction
• Somewhat mathematical derivation but useful formula
• Problem setup: Rotate vector b by θ about a

• Helpful to relate b to X, a to Z, verify does right thing

• For HW1, you probably just need final formula

• Step 1: b has components parallel to a, perpendicular
• Parallel component unchanged (rotating about an axis leaves that axis unchanged after rotation, e.g. rot about z)
• Step 2: Define c orthogonal to both a and b
• Analogous to defining Y axis
• Use cross products and matrix formula for that
• Step 3: With respect to the perpendicular comp of b
• Cos θ of it remains unchanged
• Sin θ of it projects onto vector c

#### 齐次坐标

• Translation: Homogeneous Coordinates

• Transforming Normals

• Rotations revisited: coordinate frames

• gluLookAt (quickly)

• E.g. move x by +5 units, leave y, z unchanged
• We need appropriate matrix. What is it?

$\begin{pmatrix}x’ \\ y’ \\ z’ \end{pmatrix}=\begin{pmatrix}?\end{pmatrix}\begin{pmatrix}x \\ y \\ z\end{pmatrix}=\begin{pmatrix}x+5 \\ y \\ z\end{pmatrix}$

• Add a fourth homogeneous coordinate (w=1)

• 4x4 matrices very common in graphics, hardware

• Last row always 0 0 0 1 (until next lecture)

$\begin{pmatrix}\end{pmatrix} = \begin{pmatrix}1 & 0 & 0 & 5 \\ 0 & 1 & 0 & 0 \\ 0 &0 & 1 & 0 \\ 0 & 0 & 0 &1 \end{pmatrix} \begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix} = \begin{pmatrix}x + 5 \\ y \\ z \\ 1\end{pmatrix}$

Homogeneous coordinates

• Divide by 4th coord (w) to get (inhomogeneous) point

• Multiplication by w > 0, no effect

• Assume w ≥ 0. For w > 0, normal finite point. For w = 0, point at infinity (used for vectors to stop translation)

• Unified framework for translation, viewing, rot…

• Can concatenate any set of transforms to 4x4 matrix

• No division (as for perspective viewing) till end

• Simpler formulas, no special cases

• Standard in graphics software, hardware

$T=\begin{pmatrix}1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_z \\ 0 & 0 & 0 & 1\end{pmatrix}=\begin{pmatrix}I_3 & T \\ 0 & 1\end{pmatrix}$

$P’=TP=\begin{pmatrix}1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_z \\ 0 & 0 & 0 & 1\end{pmatrix}\begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix}=\begin{pmatrix}x+T_x \\ y + T_y \\ z + T_z \\ 1\end{pmatrix}=P+T$

• Order matters!! TR is not the same as RT (demo)

• General form for rigid body transforms

• We show rotation first, then translation (commonly used to position objects) on next slide. Slide after

that works it out the other way

• Demos with applet

$P’=(TR)P=MP=RP+T$

$P’=(RT)P=MP=R(P+T)=RP+RT$

#### 法向变换

• Important for many tasks in graphics like lighting
• Do not transform like points e.g. shear
• Algebra tricks to derive correct transform

$t \to Mt \qquad n \to Qn \qquad Q=?$

$n^Tt=0$

$n^TQ^TMt=0 \to Q^TM=I$

$Q=(M^{-1})^T$

#### 旋转和坐标

• All of discussion in terms of operating on points

• But can also change coordinate system

• Example, motion means either point moves backward, or coordinate system moves forward

• Can differ both origin and orientation (e.g. 2 people)

• One good example: World, camera coord frames (H1)

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal vectors

#### 推导gluLookAt

Defines camera, fundamental to how we view images

• gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)

• Camera is at eye, looking at center, with the up direction being up

• May be important for HW1

• Combines many concepts discussed in lecture

• Core function in OpenGL for later assignments

• gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)

• Camera is at eye, looking at center, with the up direction being up

• First, create a coordinate frame for the camera

• Define a rotation matrix

• Apply appropriate translation for camera (eye) location

We want to associate w with a, and v with b

• But a and b are neither orthogonal nor unit norm

• And we also need to find u

• We want to position camera at origin, looking down –Z dirn

• Hence, vector a is given by eyecenter

• The vector b is simply the up vector

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal vectors

### Viewing

• We have seen transforms (between coord systems)
• But all that is in 3D
• We still need to make a 2D picture
• Project 3D to 2D.How do we do this?
• This lecture is about viewing transformations