Fall 2014 CSCI 420: Computer Graphics 3.1 Viewing and Projection Hao Li http://cs420.hao-li.com 1
Recall: Affine Transformations [ x y z ] > • Given a point [ x y z 1] > • form homogeneous coordinates [ x 0 y 0 z 0 ] > • The transformed point is 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 proportional to x y • Leaves and values fixed y z 4
Specification via Shear Angle cot( θ ) = ( x 0 − x ) /y x 0 = x + y cot( θ ) y 0 = y z 0 = z [ x 0 , y 0 ] x 0 − x y [ x, y ] θ θ = shear angle y θ x 5
Specification via Ratios • For example, shear in both and direction z x • Leave fixed y • Slope for -shear, for -shear γ z x α • Solve � • Yields 6
Composing Transformations • Let , and p = Aq q = Bs p = ( AB ) s • Then s q p B A AB matrix multiplication 7
Composing Transformations • 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); u • e = eye point e f • f = focus point u • u = up vector f e view plane 11
OpenGL code � void display() { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); � gluLookAt (ex, ey, ez, fx, fy, fz, ux, uy, uz); � glTranslatef(x, y, z); ... renderBunny(); � glutSwapBuffers(); } 12
Implementing the Look-At Function 1. Transform world frame to camera frame - Compose a rotation with translation R T - W = TR 2. Invert to obtain viewing transformation W V V = W − 1 = ( TR ) − 1 = R − 1 T − 1 - R − 1 T − 1 - Derive , then , then R T 13
World Frame to Camera Frame I • Camera points in negative direction z n = ( f � e ) / k f � e k • is unit normal to view plane [ n x n y n z ] > [0 0 − 1] > • Therefore, maps to R u f e n view plane 14
World Frame to Camera Frame II [0 1 0] > • maps to projection of u onto view plane R • This projection equals: v α = u > n / k n k = u > n - - v 0 = u − α n v = v 0 / k v 0 k - v 0 u α f e n view plane 15
World Frame to Camera Frame III • Set to be orthogonal to and , w v n • , w = n × v [ w v − n ] > • is right-handed v f w e n 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 ) / k f � e k v = ( u � ( u > n ) n ) / k u � ( u > n ) n k • , • . w = n × 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 e > = [ e x e y e z 1] > • Translation of origin to 18
Camera Frame to Rendering Frame V = W − 1 = ( TR ) − 1 = R − 1 T − 1 • , • is rotation, so R � 1 = R > R � � T − 1 • is translation, so negates displacement T 19
Putting it Together V = R − 1 T − 1 • Calculate � � • This is different from book [Angel, Ch. 5.3.2] • There, are right-handed (here: ) u , v , n 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 are described by a 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 z p = 0 • , , y p = y x p = x • 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); • and as before far near w/h • aspectRatio = • Fovy specifies field of view as height ( ) angle y 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); � glMatrixMode(GL_MODELVIEW); } 36
Perspective Viewing Mathematically � � � • = focal length d y p = y/ ( z/d ) = yd/z • so y/z = y p /d • Note that is non-linear in the depth ! y p z 37
Exploiting the 4 th Dimension Perspective projection is not affine: � has no solution for M � Idea: exploit homogeneous coordinates � w 6 = 0 for arbitrary 38
Perspective Projection Matrix • Use multiple of point � � • Solve with 39
Projection Algorithm [ x y z ] > • Input : 3D point to project [ x y z 1] > • Form [ x y z 1] > [ X Y Z W ] > • Multiply with ; obtaining M • Perform perspective division: X/W Y/W Z/W , , [ X/W, Y/W, Z/W ] > • Output : • (last coordinate will be ) d 40
Perspective Division [ X Y Z W ] > [ X/W, Y/W, Z/W, 1] > • Normalize to • Perform perspective division after projection � � • Projection in OpenGL is more complex (includes clipping) 41
http://cs420.hao-li.com Thanks! 42
Recommend
More recommend