CSCI 420 Computer Graphics Lecture 6 Viewing and Projection Jernej Barbic University o Southern Caliornia Shear Transormation Camera Positioning Simple Parallel Projections Simple Perspective Projections [Angel, Ch. 4] Reminder: Aine Transormations Given a point [x y z], orm homogeneous coordinates [x y z 1]. The transormed point is [x y z ]. 1 2 Transormation Matrices in OpenGL Transormation matrices in OpenGL are vectors o 16 values (column-major matrices) Shear Transormations x-shear scales x proportional to y Leaves y and z values ixed m = {m 1, m 2,..., m 16 } represents Some books transpose all matrices! 3 4 Speciication via Shear Angle Speciication via Ratios cot(q) = (x -x) / y x = x + y cot(q) y = y z = z y (x,y) (x,y ) x -x q y q = shear angle For example, shear in both x and z direction Leave y ixed Slope a or x-shear, g or z-shear Solve Yields q x 5 6 1
Composing Transormations Let p = A q, and q = B s. Then p = (A B) s. s q p B A Composing Transormations Fact: Every aine transormation is a composition o rotations, scalings, and translations So, how do we compose these to orm an x-shear? Exercise! AB matrix multiplication 7 8 Outline Shear Transormation Camera Positioning Simple Parallel Projections Simple Perspective Projections Transorm Camera = Transorm Scene Camera position is identiied with a rame Either move and rotate the objects Or move and rotate the camera Initially, camera at origin, pointing in negative z-direction 9 10 The Look-At Function Convenient way to position camera OpenGLMatrix::LookAt(ex, ey, ez, x, y, z, ux, uy, uz); // core proile glulookat(ex, ey, ez, x, y, z, ux, uy, uz); // compatibility proile e = eye point = ocus point u = up vector e u e view plane u 11 OpenGL code void display() { glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); openglmatrix->setmatrixmode(openglmatrix::modelview); openglmatrix->loadidentity(); openglmatrix->lookat(e x, e y, e z, x, y, z, u x, u y, u z ); openglmatrix->translate(x, y, z); // transorm the object... loat m[16]; // column-major openglmatrix->getmatrix(m); // ill m with the matrix entries gluniormmatrix4v(h_modelviewmatrix, 1, GL_FALSE, m); renderbunny(); glutswapbuers(); } 12 2
Implementing the Look-At Function Plan: 1. Transorm world rame to camera rame Compose a rotation R with translation T W = T R 2. Invert W to obtain viewing transormation V V = W -1 = (T R) -1 = R -1 T -1 Derive R, then T, then R -1 T -1 World Frame to Camera Frame I Camera points in negative z direction n = ( e) / e is unit normal to view plane Thereore, R maps [0 0-1] T to [n x n y n z ] T u n e view plane 13 14 World Frame to Camera Frame II R maps [0,1,0] T to projection o u onto view plane This projection v equals: a = (u n) / n = u n v 0 = u a n v = v 0 / v 0 u 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 0 e a n w v e n view plane view plane 15 16 Summary o Rotation glulookat(e x, e y, e z, x, y, z, u x, u y, u z ); n = ( e) / 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 World Frame to Camera Frame IV Translation o origin to e = [e x e y e z 1] T 17 18 3
World Frame to Camera Frame V = W -1 = (T R) -1 = R -1 T -1 R is rotation, so R -1 = R T Putting it Together Calculate V = R -1 T -1 T is translation, so T -1 negates displacement This is dierent rom book [Angel, Ch. 5.3.2] There, u, v, n are right-handed (here: u, v, -n) 19 20 Other Viewing Functions Roll (about z), pitch (about x), yaw (about y) Outline Shear Transormation Camera Positioning Simple Parallel Projections Simple Perspective Projections Assignment 2 poses a related problem 21 22 Projection Matrices Recall geometric pipeline Projection takes 3D to 2D Projections are not invertible Projections are described by a 4x4 matrix Homogenous coordinates crucial Parallel and perspective projections Parallel Projection Project 3D object to 2D via parallel lines The lines are not necessarily orthogonal to projection plane 23 source: Wikipedia 24 4
Parallel Projection Orthographic Projection Problem: objects ar away do not appear smaller Can lead to impossible objects : A special kind o parallel projection: projectors perpendicular to projection plane Simple, but not realistic Used in blueprints (multiview projections) Penrose stairs source: Wikipedia 25 26 Orthographic Projection Matrix Perspective Project onto z = 0 xp = x, yp = y, zp = 0 In homogenous coordinates Perspective characterized by oreshortening More distant objects appear smaller Parallel lines appear to converge Rudimentary perspective in cave drawings: Lascaux, France source: Wikipedia 27 28 Discovery o Perspective Middle Ages Foundation in geometry (Euclid) Art in the service o religion Perspective abandoned or orgotten Mural rom Pompeii, Italy Ottonian manuscript, ca. 1000 29 30 5
Renaissance Rediscovery, systematic study o perspective Filippo Brunelleschi Florence, 1415 Projection (Viewing) in OpenGL Remember: camera is pointing in the negative z direction 31 32 Orthographic Viewing in OpenGL (compatibility proile) glortho(xmin, xmax, ymin, ymax, near, ar) Orthographic Viewing in OpenGL (core proile) OpenGLMatrix::Ortho(xmin, xmax, ymin, ymax, near, ar) z min = near, z max = ar z min = near, z max = ar 33 34 Perspective Viewing in OpenGL Two interaces: glfrustum and gluperspective {OpenGLMatrix::, gl}frustum( xmin, xmax, ymin, ymax, near, ar); Field o View Interace {OpenGLMatrix::, glu}perspective( ovy, aspectratio, near, ar); near and ar as beore aspectratio = w / h Fovy speciies ield o view as height (y) angle z min = near, z max = ar 35 36 6
OpenGL code OpenGL code void reshape(int x, int y) { glviewport(0, 0, x, y); openglmatrix->matrixmode(openglmatrix::projection); openglmatrix->loadidentity(); openglmatrix->perspective(60.0, 1.0 * x / y, 0.01, 10.0); openglmatrix->matrixmode(openglmatrix::modelview); } 37 void displayfunc() { openglmatrix->setmatrixmode(openglmatrix::projection); loat p[16]; // column-major openglmatrix->getmatrix(p); const GLboolean isrowmajor = alse; gluniormmatrix4v(h_projectionmatrix, 1, isrowmajor, p); renderbunny(); glutswapbuers(); } 38 Perspective Viewing Mathematically Exploiting the 4 th Dimension Perspective projection is not aine: d has no solution or M d = ocal 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! Idea: exploit homogeneous coordinates or arbitrary w ¹ 0 39 40 Perspective Projection Matrix Use multiple o point 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 Solve with 3. Perorm perspective division: X / W, Y / W, Z / W Output: (X / W, Y / W, Z / W) (last coordinate will be d) 41 42 7
Perspective Division Normalize [x y z w] T to [(x/w) (y/w) (z/w) 1] T Perorm perspective division ater projection Projection in OpenGL is more complex (includes clipping) 43 8