3D transformations
Use homogeneous coordinates again:
- 3D point = ( x , y , z , 1 ) T (x,y,z,1)^T (x,y,z,1)T
- 3D vector = ( x , y , z , 0 ) T (x,y,z,0)^T (x,y,z,0)T
In general, ( x , y , z , w ) ( w ≠ 0 ) (x,y,z,w)(w\neq0) (x,y,z,w)(w=0) is the 3D point: ( x / w , y / w , z / w ) (x/w,y/w,z/w) (x/w,y/w,z/w)
Use 4 × 4 4\times4 4×4 matrices for affine transformations
( x ′ y ′ z ′ 1 ) = ( a b c t x d e f t u g h i t z 0 0 0 1 ) ⋅ ( x y z 1 ) \begin{pmatrix} x'\\y'\\z'\\1 \end{pmatrix}= \begin{pmatrix} a&b&c&t_x\\d&e&f&t_u\\g&h&i&t_z\\0&0&0&1 \end{pmatrix} \cdot \begin{pmatrix} x\\y\\z\\1 \end{pmatrix} x′y′z′1 = adg0beh0cfi0txtutz1 ⋅ xyz1
-
Scale
S ( s x , s y , s z ) = ( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) 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} S(sx,sy,sz)= sx0000sy0000sz00001 -
Translation
T ( t x , t y , t z ) = ( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) 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} T(tx,ty,tz)= 100001000010txtytz1 -
Rotation around x-, y-, or z-axis
R x ( α ) = ( 1 0 0 0 0 cos α − sin α 0 0 sin α cos α 0 0 0 0 1 ) R_x(\alpha)=\begin{pmatrix}1&0&0&0\\0&\cos{\alpha}&-\sin{\alpha}&0\\0&\sin{\alpha}&\cos{\alpha}&0\\0&0&0&1\end{pmatrix} Rx(α)= 10000cosαsinα00−sinαcosα00001
R y ( α ) = ( cos α 0 sin α 0 0 1 0 0 − sin α 0 cos α 0 0 0 0 1 ) R_y(\alpha)=\begin{pmatrix}\cos{\alpha}&0&\sin{\alpha}&0\\0&1&0&0\\-\sin{\alpha}&0&\cos{\alpha}&0\\0&0&0&1\end{pmatrix} Ry(α)= cosα0−sinα00100sinα0cosα00001
R z ( α ) = ( cos α − sin α 0 0 sin α cos α 0 0 0 0 1 0 0 0 0 1 ) R_z(\alpha)=\begin{pmatrix}\cos{\alpha}&-\sin{\alpha}&0&0\\\sin{\alpha}&\cos{\alpha}&0&0\\0&0&1&0\\0&0&0&1\end{pmatrix} Rz(α)= cosαsinα00−sinαcosα0000100001 -
3D Rotations
Compose any 3D rotation from R x , R y , R z R_x,R_y,R_z Rx,Ry,Rz?
R x y z ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) R_{xyz}(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma) Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)- So-called Euler angles
- Often used in flight simulators: roll, pitch, yaw
Rodrigues’ Rotaion Formula: Rotation by angle α \alpha α around axis n n n
R ( n , α ) = cos α I + ( 1 − cos α ) n n T + sin α ( 0 − n z n y n z 0 − n x − n y n x 0 ) ⏟ N \bold{R}(\bold{n},\alpha)=\cos{\alpha}\bold{I}+(1-\cos{\alpha})\bold{n}\bold{n}^T+\sin{\alpha}\underbrace{\begin{pmatrix}0&-n_z&n_y\\ n_z&0&-n_x\\ -n_y&n_x&0\end{pmatrix}}_\text{N} R(n,α)=cosαI+(1−cosα)nnT+sinαN 0nz−ny−nz0nxny−nx0
Viewing 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)
- How to perform view transformation?
- Define the camera first
- Position e ⃗ \vec{e} e
- Look-at / gaze direction g ^ \hat{g} g^
- Up direction t ^ \hat{t} t^
- 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
v
i
e
w
M_{view}
Mview
- So it’s located at the origin, up at Y, look at -Z
-
M
v
i
e
w
M_{view}
Mview in math?
- Let’s write M v i e w = R v i e w T v i e w M_{view}=R_{view}T_{view} Mview=RviewTview
- Translate e to origin
T v i e w = [ 1 0 0 − x e 0 1 0 − y e 0 0 1 − z e 0 0 0 1 ] 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} Tview= 100001000010−xe−ye−ze1 - Rotate g to -Z, t to Y, (g x t) to X
- Consder its inverse rotation: X to (g x t), Y to t, Z to -g
R v i e w − 1 = [ x g ^ × t ^ x t x − g 0 y g ^ × t ^ y t y − g 0 z g ^ × t ^ z t z − g 0 0 0 0 1 ] ⇒ R v i e w = [ x g ^ × t ^ y g ^ × t ^ z g ^ × t ^ 0 x t y t z t 0 x − g y − g z − g 0 0 0 0 1 ] R_{view}^{-1}= \begin{bmatrix} 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{bmatrix} \Rightarrow R_{view}= \begin{bmatrix} 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{bmatrix} Rview−1= xg^×t^yg^×t^zg^×t^0xtytzt0x−gy−gz−g00001 ⇒Rview= xg^×t^xtx−g0yg^×t^yty−g0zg^×t^ztz−g00001
- Summary
- Transform objects together with the camera
- Until camera’s at the origin, up at Y, look at -Z
- Also known as ModelView Transformation
Projection transformation
- Projection in Computer Graphics
- 3D to 2D
- Orthographic projection
- Perspective projection
Orthographic projection
- A simple way of understanding
- Camera located at origin, looking at -Z, up at Y
- Drop Z coordinate
- Translate and scale the result rectangle to [ − 1 , 1 ] 2 [-1,1]^{2} [−1,1]2
- In general
- We want to map a cubiod [ l , r ] × [ b , t ] × [ f , n ] [l,r]\times[b,t]\times[\bold{f},\bold{n}] [l,r]×[b,t]×[f,n] to the ‘canonical’ cube [ − 1 , 1 ] 3 [-1,1]^{3} [−1,1]3
- Slightly different orders
- Center cuboid by translating
- Scale into ‘canonical’ cube
- Transformation matrix?
- Translate (center to origin) first, then scale (length / width / height to 2)
M o r t h o = [ 2 r − l 0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ] [ 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ] 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} Mortho= r−l20000t−b20000n−f200001 100001000010−2r+l−2t+b−2n+f1
- Translate (center to origin) first, then scale (length / width / height to 2)
- Caveat
- Looking at / along -Z is making near and far not intuitive (n > f)
- FYI: that’s why OpenGL (a Graphic 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
- How to do perspective projection
- First ‘squish’ the frustum into a cuboid ( n → n n\rightarrow n n→n, f → f f\rightarrow f f→f) ( M p e r s p → o r t h o M_{persp\rightarrow ortho} Mpersp→ortho)
- Do orthographic projection ( M o r t h o M_{ortho} Mortho, already known!)
- In order to find a transformation
- Recall the key idea: Find the relationship between transformed points
(
x
′
,
y
′
,
z
′
)
(x',y',z')
(x′,y′,z′) and the original points
(
x
,
y
,
z
)
(x,y,z)
(x,y,z)
y ′ = n z y x ′ = n z x (similar to y’) y'=\frac{n}{z}y\qquad x'=\frac{n}{z}x\quad\text{(similar to y')} y′=znyx′=znx(similar to y’)
- Recall the key idea: Find the relationship between transformed points
(
x
′
,
y
′
,
z
′
)
(x',y',z')
(x′,y′,z′) and the original points
(
x
,
y
,
z
)
(x,y,z)
(x,y,z)
- In homongeneous coordinates,
( x y z 1 ) ⇒ ( n x / z n y / z unknown 1 ) = ( n x n y still unknown z ) \begin{pmatrix}x\\y\\z\\1\end{pmatrix}\Rightarrow \begin{pmatrix}nx/z\\ny/z\\\text{unknown}\\1\end{pmatrix}= \begin{pmatrix}nx\\ny\\\text{still unknown}\\z\end{pmatrix} xyz1 ⇒ nx/zny/zunknown1 = nxnystill unknownz - So the ‘squish’ (persp to ortho) projection does this
M p e r s p → o r t h o 4 × 4 ( x y z ) = ( n x n y unknown z ) M^{4\times4}_{persp\rightarrow ortho}\begin{pmatrix}x\\y\\z\end{pmatrix}=\begin{pmatrix}nx\\ny\\\text{unknown}\\z\end{pmatrix} Mpersp→ortho4×4 xyz = nxnyunknownz - Already good enough to figure out a part of
M
p
e
r
s
p
→
o
r
t
h
o
M_{persp\rightarrow ortho}
Mpersp→ortho
M p e r s p → o r t h o = ( n 0 0 0 0 n 0 n ? ? ? ? 0 0 1 0 ) M_{persp\rightarrow ortho}= \begin{pmatrix}n&0&0&0\\0&n&0&n\\?&?&?&?\\0&0&1&0\end{pmatrix} Mpersp→ortho= n0?00n?000?10n?0 - How to figure out the third row of
M
p
e
r
s
p
→
o
r
t
h
o
M_{persp\rightarrow ortho}
Mpersp→ortho
- Any information that we can use?
- Observation: the third row is responsible for z’
- Any point on the near plane will not change
- So the third row must be of the form ( 0 , 0 , A , B ) (0,0,A,B) (0,0,A,B)
- Any point’s z on the far plane will not change
- A n + b = n 2 An+b=n^2 An+b=n2
- A f + B = f 2 Af+B=f^2 Af+B=f2
- Any point on the near plane will not change
- Finally, every entry in M p e r s p → o r t h o M_{persp\rightarrow ortho} Mpersp→ortho is known!
- What’s next?
- Do orthographic projection ( M o r t h o M_{ortho} Mortho) to finish
- M p e r s p = M o r t h o M p e r s p → o r t h o M_{persp}=M_{ortho}M_{persp\rightarrow ortho} Mpersp=MorthoMpersp→ortho