Lecture 5: Transforms (Cont)

Computer Graphics and Imaging UC Berkeley CS184/284A, Spring 2017 3D Transforms 3D Transformations

Use homogeneous coordinates again: • 3D point = (x, y, z, 1)T • 3D vector = (x, y, z, 0)T

Use 4×4 matrices for affine transformations

x abctx x y deft y ⇥ = y⇥ ⇥ z ghitz · z ⇧ 1 ⌃ ⇧000 1⌃ ⇧1⌃ ⇧ ⌃ ⇧ ⌃ ⇧ ⌃ ⇤ ⌅ ⇤ ⌅ ⇤ ⌅

CS184/284A Ren Ng 3D Transformations

sx 000 Scale 0 sy 00 S(sx,sy,sz)= ⇥ 00sz 0 ⇧ 0001⌃ ⇧ ⌃ ⇤ ⌅

Translation 100tx 010ty T(tx,ty,tz)= ⇥ 001tz ⇧000 1⌃ ⇧ ⌃ ⇤ ⌅ Coordinate Change (Frame-to-world) uvwo F(u, v, w, o)= 0001  CS184/284A Ren Ng 3D Transformations

Rotation around x-, y-, or z-axis y Rotation 10 0 0 around 0 cos sin 0 x-axis R ()= x 0 sin cos 0⇥ x ⇧00 0 1⌃ ⇧ ⌃ ⇤ ⌅ cos 0 sin 0 0100z R ()= y sin 0 cos 0⇥ ⇧ 0001⌃ ⇧ ⌃ ⇤ ⌅ cos sin 00 sin cos 00 R ()= z 0010⇥ ⇧ 0001⌃ ⇧ ⌃ CS184/284A ⇤ ⌅ Ren Ng 3D Rotations

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

Rxyz(, ⇥, ⇤)=Rx() Ry(⇥) Rz(⇤) • So-called Euler angles • Often used in flight simulators: roll, pitch, yaw

CS184/284A Ren Ng 3D Rotations

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

Rxyz(, ⇥, ⇤)=Rx() Ry(⇥) Rz(⇤) • So-called Euler angles • Often used in flight simulators: roll, pitch, yaw

• Problem: Gimbal Lock!

CS184/284A Ren Ng f(x)=T (S (x)) f(x)=S (T (x)) 3,1 0.5 0.5 3,1 f(x)=g(x)+b Euclidean: f(x)=T3,1(S0.5(x)) f(x)=S0.5(Tf3(,x1)(x))f(y) = x y | | | | f(x)=g(x)+b f(x)=R⇡/4S[1.5,1.5]x f(x)=T3,1(S0.5(x)) f(x)=S0.5(T3,1(x)) Euclidean: x = 22 f(x) f(y) = x y f(x)=g(x)+b | | | | x = ⇥0.51⇤ Euclidean: f(x)=R⇡/4S[1.5,1.5]x f(x) f(xy=) ⇥=e2 +x e3⇤y x = 22 | | | | f(x)=xR⇡=2/4Se[11.5+2,1.5]ex2 x = ⇥0.51⇤ 3D Rotation Around Arbitrary Axisx =x =220.51 x =⇥e + e ⇤ 2 3 e ⇥ e ⇤ x = ⇥0.511 ⇤ 2 x =2e +2e Rotations arbitrary: 1 2 y Construct orthonormal frame x =⇥e2 + e3⇤ x = 0.51 u v w x =2e1+2e2 transformation F with p, u, v, w, 1 T e1⇥ e2 ⇤ R = R x = 0.51 where p and w match the rotation axis u u u Rotations arbitrary: x y z R e=1⇥ ev2 ⇤xv v u v w uvw 2 x y z 3 p w w w Rotations arbitrary: x y z -1 ux uy uz 4 5 Apply the transform (F Rz(θ) FR ) = v v v Ruuvwvu =w100 uvw 2 x yz z 3 w w w 1 T x y z RRuvw v==R⇥010⇤ 4 5 ux uy uz Ruvww = ⇥001⇤ R = v v v Interpretations (both valid): uvw 2 x y z 3 wx w⇥y uxwz vx⇤ wx 1 T Ruvw = R4 uvw = uy 5vy wy • to Z axis, rotate, then move back Ruvwu = 1002 3 uz vx wz 4 5 • Cast w-axis rotation in new coordinate frameRuvwv = ⇥010⇤ Ruvww = ⇥001⇤

CS184/284A Ren⇥ uNgx vx⇤ wx 1 T R = R = u v w uvw uvw 2 y y y3 uz vx wz 4 5

4

4 4 Rodrigues’ Rotation Formula

Rotation by angle α around axis n

0 nz ny T R(n, ) = cos() I + (1 cos()) nn + sin() nz 0 nx n n 0 ⇥ y x ⇤ N ⌅ ⌥ ⌃⇧ How to prove this magic formula? • Matrix N computes a cross-product: N x = n × x • Assume orthonormal system e1, e2, n Rn = n

Re1 = cos e1 + sin e2

Re2 = sin e1 + cos e2 CS184/284A Ren Ng Many Other Representations of Rotations

Quaternions Exponential map …

CS184/284A Ren Ng Hierarchical Transforms Skeleton - Linear Representation head torso right upper arm right lower arm right hand left upper arm left lower arm left hand right upper leg right lower leg right foot left upper leg left lower leg left foot

CS184/284A Ren Ng Linear Representation

Each shape associated with its own transform A single edit can require updating many transforms • E.g. raising arm requires updating transforms for all arm parts

CS184/284A Ren Ng Skeleton - Hierarchical Representation torso head right arm upper arm lower arm hand left arm upper arm lower arm hand right leg upper leg lower leg foot left leg upper leg lower leg foot

CS184/284A Ren Ng Hierarchical Representation

Grouped representation () • Each group contains subgroups and/or shapes • Each group is associated with a transform relative to parent group • Transform on leaf-node shape is concatenation of all transforms on from root node to leaf • Changing a group’s transform affects all parts • Allows high level editing by changing only one node • E.g. raising left arm requires changing only one transform for that group

CS184/284A Ren Ng Skeleton - Hierarchical Representation

translate(0, 10); drawTorso(); pushmatrix(); // push a of transform onto stack translate(0, 5); // right-multiply onto current transform rotate(headRotation); // right-multiply onto current transform drawHead(); popmatrix(); // pop current transform off stack pushmatrix(); translate(-2, 3); rotate(rightShoulderRotation); drawUpperArm(); pushmatrix(); translate(0, -3); rotate(elbowRotation); drawLowerArm(); pushmatrix(); right translate(0, -3); lower right rotate(wristRotation); right arm arm drawHand(); hand group popmatrix(); group popmatrix(); popmatrix(); ….

CS184/284A Ren Ng Skeleton - Hierarchical Representation

translate(0, 10); drawTorso(); pushmatrix(); // push a copy of transform onto stack translate(0, 5); // right-multiply onto current transform rotate(headRotation); // right-multiply onto current transform drawHead(); popmatrix(); // pop current transform off stack pushmatrix(); translate(-2, 3); rotate(rightShoulderRotation); drawUpperArm(); pushmatrix(); translate(0, -3); rotate(elbowRotation); drawLowerArm(); pushmatrix(); right translate(0, -3); lower right rotate(wristRotation); right arm arm drawHand(); hand group popmatrix(); group popmatrix(); popmatrix(); ….

CS184/284A Ren Ng

Viewing and Perspective “Standard” Camera Space

y z We will use this convention for “standard” camera coordinates: • camera located the origin looking down negative z-axis x • • vertical vector is y-axis • (x-axis) orthogonal to y & z

CS184/284A Ren Ng “Standard” Camera Coordinates y y z

x x

Resulting image (z-axis pointing away from scene)

CS184/284A Ren Ng Consider A Camera Pointing in The World

CS184/284A Ren Ng Consider A Camera Pointing in The World

u = up vector

v = view direction e = eye point (position of camera)

CS184/284A Ren Ng Camera “Look-At” Transformation

u = up vector

v = view direction e = eye point (position of camera)

Input: e, u & v given in world space coordinates Output: transform matrix from world space

CS184/284Ato standard camera space Ren Ng Camera “Look-At” Transformation

Inverse: Matrix from standard camera to world space (Why? This is a coordinate frame transform to (e,r,u,-v))

r u v e x x x x ry uy vy ey r u v e ⇥ z z z z ⇧ 00 0 1⌃ ⇧ ⌃ ⇤ ⌅ “Look-at” transform is the inverse of above matrix: 1 rx ux vx ex rx ry rz 0 100 ex r u v e u u u 0 010e y y y y⇥ = x y z ⇥ y⇥ rz uz vz ez vx vy vz 0 · 001 ez ⇧ 00 0 1⌃ ⇧ 0001⌃ ⇧000 1⌃ ⇧ ⌃ ⇧ ⌃ ⇧ ⌃ ⇤ ⌅ ⇤ ⌅ ⇤ ⌅

CS184/284A Ren Ng Viewing

Perspective Orthographic

CS184/284A Ren Ng Perspective Projection

Credit: jefflynchphoto.com Perspective in Art

CS184/284A Ren Ng Perspective in Art

CS184/284A Giotto 1290 Ren Ng Perspective in Art

CS184/284A Giotto 1290 Ren Ng Perspective in Art

Brunelleschi experiment c. 1413 Perspective in Art

Delivery of the Keys (Sistine Chapel), Perugino, 1482 Perspective in Art

The Last Supper, Leonardo da Vinci, 1499 Pinhole Camera (Camera Obscura)

Mo Tzu (c. 470–c. 390 BC)

Aristotle (384–322 BC)

Ibn al-Haytham (965–1040)

Shen Kuo (1031–1095)

Roger Bacon (c. 1214–1294)

Johannes Kepler (1571–1630)

A. H. Zewail, Phil. Trans. R. Soc. A 2010;368:1191-1204

CS184/284A Ren Ng Pinhole Camera (Camera Obscura)

CS184/284A Ren Ng Projective Transforms

x, y (x, y, z)T

Scene point

-z Image C point d

Inverted image (as in real pinhole camera)

CS184/284A Ren Ng Projective Transforms

x, y (x, y, z)T

Image point Scene point

-z C

d

Upright image

CS184/284A Ren Ng Projective Transforms

Standard perspective projection • Center of projection: (0, 0, 0)T • Image plane at z = d

x, y (x,y,z)T x d/z x · x d/z y 0y d/z1 ⇠= · 0 1 7! · y d/z! C z d · -z B C @ A @ A z = d

CS184/284A Ren Ng Projective Transforms

Standard perspective projection x d/z x · • Center of projection: (0, 0, 0)T y y d/z 0 1 7! 0 · 1 z d • Image plane at z = d B C @ A @ A

Perspective foreshortening • Need division by z • Matrix representation? ➡Homogeneous coordinates!

CS184/284A Ren Ng Homogenous Coordinates (3D)

wx x 10 0 0 wy y 01 0 0 p = M = 0 1 0wz1 0z1 00 1 0 B w C B1C B001/d 0C B C B C B C @ A @ A @ A x x xd/z y y yd/z q = M = 0z1 0 z 1 0 d 1 B1C Bz/dC B 1 C B C B C B C @ A @ A @ A

CS184/284A Ren Ng Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

CS184/284A Ren Ng Perspective Projection

(1, 1, 1)

(right, top, –near)

(left, bottom, –near) z = –far

z = –near (–1, –1, –1)

Camera Coordinates Normalized Device Coords “NDC” CS184/284A Later we will “flatten” NDC to get screen coordinates Ren Ng Perspective Transform Matrix

near right 00 0right near 0 top 00top P = 2 far+near 2far near 3 2 3 00 ⇤ near far near far near 6 00 10 7 6 1 7 6 7 6 7 4 near 5 4 5 near = 2 far+near 2far near 3 near ⇤ ⇤ far near far near 6 near 7 6 7 CS184/284A4 1 5 Ren Ng 1 = 2 far+near 3 far near 6 1 7 6 7 4 1 5 1 = 2 13 6 1 7 6 7 4 5 Perspective Transform Matrix Example

near near right right0000 0 0right right near near 0 top0 top 0000top top P = 2P = 2 far+nearfar+near2far near2far3 2near 3 23 3 0000 ⇤ ⇤ near near far near far nearfar nearfar near 6 006 001010 7 6 17 67 1 7 6 6 7 6 7 67 7 4 4near near 5 4 5 45 5 near near = 2 = 2 far+nearfar+near2far near2far3 near 3 near near ⇤ ⇤ ⇤ far near⇤ farnearfar nearfar near 6 6 near near 7 7 6 6 7 7 4 14 1 5 5 1 1 = 2 far= +2nearfar3+near 3 far nearfar near 6 16 71 7 6 6 7 7 4 1 4 1 5 5 1 1 = = 2 13 2 13 6 1 7 6 1 7 6 7 6 7 4 5 4 5

CS184/284A Ren Ng Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

CS184/284A Ren Ng Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

CS184/284A Ren Ng Perspective Projection

Parameterized by • fovy : vertical angular field of view • aspect : width / height of field of view • near : depth of near clipping plane • far : depth of far clipping plane Derived quantities • top = near * tan (fovy) • bottom = – top • right = top * aspect • left = – right

CS184/284A Ren Ng Transforms Recap Transforms Recap

Coordinate Systems • Object coordinates • Apply modeling transforms… • World (scene) coordinates • Apply viewing transform… • Camera (eye) coordinates • Apply perspective transform + homog. divide… • Normalized device coordinates • Apply 2D screen transform… • Screen coordinates CS184/284A Ren Ng Transforms Recap

Modeling transforms

Object coords World coords

CS184/284A Ren Ng Transforms Recap

Viewing transform

World coords Camera coords

CS184/284A Ren Ng Transforms Recap

(1, 1, 1)

Perspective projection and homogeneous (–1, –1, –1) divide Camera coords NDC

CS184/284A Ren Ng Transforms Recap

(w, h)

(1, 1, 1)

Screen transform

(–1, –1, –1)

(0, 0) NDC Screen coords

CS184/284A Ren Ng Transforms Recap

(w, h)

Rasterization

(0, 0) Screen coords

CS184/284A Ren Ng Things to Remember

Transform uses • Basic transforms: rotate, scale, translate, … • Modeling, viewing, projection, perspective • Change in coordinate system • Hierarchical scene descriptions by push/pop Implementing transforms • Linear transforms = matrices • Transform composition = matrix multiplication • Homogeneous coordinates for translation, projection

CS184/284A Ren Ng Acknowledgments

Thanks to Pat Hanrahan, Kayvon Fatahalian, Mark Pauly for presentation resources.

CS184/284A Ren Ng