CSCI 480 Computer Graphics Lecture 5 Viewing and Projection January 25, 2012 Jernej Barbic University of Southern California Shear Transformation Camera Positioning Simple Parallel Projections Simple Perspective Projections [Angel, Ch. 5] http://www-bcf.usc.edu/~jbarbic/cs480-s12/ 1
Reminder: Affine Transformations Given a point [x y z], form homogeneous coordinates [x y z 1]. The transformed point is [x y z ]. 2
Transformation Matrices in OpenGL Transformation matrices in OpenGL are vectors of 16 values (column-major matrices) In glloadmatrixf(glfloat *m); m = {m 1, m 2,..., m 16 } represents Some books transpose all matrices! 3
Shear Transformations x-shear scales x proportional to y Leaves y and z values fixed 4
Specification via Shear Angle cot(θ) = (x -x) / y x = x + y cot(θ) y = y z = z y (x,y) x -x θ (x,y ) y θ = shear angle θ x 5
Specification via Ratios For example, shear in both x and z direction Leave y fixed Slope α for x-shear, γ for z-shear Solve Yields 6
Composing Transformations Let p = A q, and q = B s. Then p = (A B) s. s q p B A AB matrix multiplication 7
Composing Transformations Fact: Every affine transformation is a composition of rotations, scalings, and translations So, how do we compose these to form an x-shear? Exercise! 8
Outline Shear Transformation Camera Positioning Simple Parallel Projections Simple Perspective Projections 9
Transform Camera = Transform Scene Camera position is identified with a frame Either move and rotate the objects Or move and rotate the camera Initially, camera at origin, pointing in negative z-direction 10
The Look-At Function Convenient way to position camera glulookat(ex, ey, ez, fx, fy, fz, ux, uy, uz); e = eye point f = focus point u = up vector u e f u e f view plane 11
OpenGL code void display() { glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); glmatrixmode (GL_MODELVIEW); glloadidentity(); glulookat (e x, e y, e z, f x, f y, f z, u x, u y, u z ); gltranslatef(x, y, z);... renderbunny(); glutswapbuffers(); } 12
Implementing the Look-At Function Plan: 1. Transform world frame to camera frame Compose a rotation R with translation T W = T R 2. Invert W to obtain viewing transformation V V = W -1 = (T R) -1 = R -1 T -1 Derive R, then T, then R -1 T -1 13
World Frame to Camera Frame I Camera points in negative z direction n = (f e) / f e is unit normal to view plane Therefore, R maps [0 0-1] T to [n x n y n z ] T u n e f view plane 14
World Frame to Camera Frame II R maps [0,1,0] T to projection of u onto view plane This projection v equals: α = (u n) / n = u n v 0 = u α n v = v 0 / v 0 u V 0 e α n f view plane 15
World Frame to Camera Frame III Set w to be orthogonal to n and v w = n x v (w, v, -n) is right-handed v n w e f view plane 16
Summary of Rotation glulookat(e x, e y, e z, f x, f y, f z, u x, u y, u z ); n = (f e) / f e v = (u (u n) n) / u (u n) n w = n x v Rotation must map: (1,0,0) to w (0,1,0) to v (0,0,-1) to n 17
World Frame to Camera Frame IV Translation of origin to e = [e x e y e z 1] T 18
Camera Frame to Rendering Frame V = W -1 = (T R) -1 = R -1 T -1 R is rotation, so R -1 = R T T is translation, so T -1 negates displacement 19
Putting it Together Calculate V = R -1 T -1 This is different from book [Angel, Ch. 5.3.2] There, u, v, n are right-handed (here: u, v, -n) 20
Other Viewing Functions Roll (about z), pitch (about x), yaw (about y) Assignment 2 poses a related problem 21
Outline Shear Transformation Camera Positioning Simple Parallel Projections Simple Perspective Projections 22
Projection Matrices Recall geometric pipeline Projection takes 3D to 2D Projections are not invertible Projections also described by 4x4 matrix Homogenous coordinates crucial Parallel and perspective projections 23
Parallel Projection Project 3D object to 2D via parallel lines The lines are not necessarily orthogonal to projection plane source: Wikipedia 24
Parallel Projection Problem: objects far away do not appear smaller Can lead to impossible objects : Penrose stairs source: Wikipedia 25
Orthographic Projection A special kind of parallel projection: projectors perpendicular to projection plane Simple, but not realistic Used in blueprints (multiview projections) 26
Orthographic Projection Matrix Project onto z = 0 x p = x, y p = y, z p = 0 In homogenous coordinates 27
Perspective Perspective characterized by foreshortening More distant objects appear smaller Parallel lines appear to converge Rudimentary perspective in cave drawings: Lascaux, France source: Wikipedia 28
Discovery of Perspective Foundation in geometry (Euclid) Mural from Pompeii, Italy 29
Middle Ages Art in the service of religion Perspective abandoned or forgotten Ottonian manuscript, ca. 1000 30
Renaissance Rediscovery, systematic study of perspective Filippo Brunelleschi Florence, 1415 31
Projection (Viewing) in OpenGL Remember: camera is pointing in the negative z direction 32
Orthographic Viewing in OpenGL glortho(xmin, xmax, ymin, ymax, near, far) z min = near, z max = far 33
Perspective Viewing in OpenGL Two interfaces: glfrustum and gluperspective glfrustum(xmin, xmax, ymin, ymax, near, far); z min = near, z max = far 34
Field of View Interface gluperspective(fovy, aspectratio, near, far); near and far as before aspectratio = w / h Fovy specifies field of view as height (y) angle 35
OpenGL code void reshape(int x, int y) { glviewport(0, 0, x, y); glmatrixmode(gl_projection); glloadidentity(); } gluperspective(60.0, 1.0 * x / y, 0.01, 10.0); 36
Perspective Viewing Mathematically d d = focal length y/z = y p /d so y p = y/(z/d) = y d / z Note that y p is non-linear in the depth z! 37
Exploiting the 4 th Dimension Perspective projection is not affine: has no solution for M Idea: exploit homogeneous coordinates for arbitrary w 0 38
Perspective Projection Matrix Use multiple of point Solve with 39
Projection Algorithm Input: 3D point (x,y,z) to project 1. Form [x y z 1] T 2. Multiply M with [x y z 1] T ; obtaining [X Y Z W] T 3. Perform perspective division: X / W, Y / W, Z / W Output: (X / W, Y / W, Z / W) (last coordinate will be d) 40
Perspective Division Normalize [x y z w] T to [(x/w) (y/w) (z/w) 1] T Perform perspective division after projection Projection in OpenGL is more complex (includes clipping) 41