CS 543: Computer Graphics Projection Robert W. Lindeman Associate Professor Interactive Media & Game Development Department of Computer Science Worcester Poltechnic Institute gogo@wpi.edu with lots of help from Prof. Emmanuel Agu :-)
3D Viewing and View Volume o Recall: 3D viewing set up R.W. Lindeman - WPI Dept. of Computer Science 2
Projection Transformation o View volume can have different shapes n Parallel, perspective, isometric o Different tpes of projection n Parallel orthographic), perspective, etc. o Important to control n Projection tpe: perspective or orthographic, etc. n Field of view and image aspect ratio n Near and far clipping planes R.W. Lindeman - WPI Dept. of Computer Science 3
Perspective Projection o Similar to real world o Characterized b object foreshortening n Objects appear larger if the are closer to camera o Need to define n Center of projection COP) n Projection view) plane camera projection plane o Projection n Connecting the object to the center of projection R.W. Lindeman - WPI Dept. of Computer Science 4
Wh is it Called Projection? View plane" R.W. Lindeman - WPI Dept. of Computer Science 5
Orthographic Parallel) Projection o No foreshortening effect n Distance from camera does not matter o The center of projection is at infinit o Projection calculation n Just choose equal z coordinates R.W. Lindeman - WPI Dept. of Computer Science 6
Field of View o Determine how much of the world is taken into the picture o Larger field of view = smaller objectprojection size field of view view angle) center of projection z θ z x R.W. Lindeman - WPI Dept. of Computer Science 7
Near and Far Clipping Planes o Onl objects between near and far planes are drawn o Near plane + far plane + field of view = View Frustum Near plane Far plane z x R.W. Lindeman - WPI Dept. of Computer Science 8
View Frustum o 3D counterpart of 2D-world clip window o Objects outside the frustum are clipped Near plane Far plane z x View Frustum R.W. Lindeman - WPI Dept. of Computer Science 9
Projection Transformation o In OpenGL n Set the matrix mode to GL_PROJECTION n For perspective projection, use gluperspective fov, aspect, near, far ); or glfrustum left, right, bottom, top, near, far ); n For orthographic projection, use glortho left, right, bottom, top, near, far ); R.W. Lindeman - WPI Dept. of Computer Science 10
gluperspective fov, aspect, near, far ) o Aspect ratio is used to calculate the window width fov w z θ z ee x h Aspect = w / h near far R.W. Lindeman - WPI Dept. of Computer Science 11
glfrustum left, right, bottom, top, near, far ) o Can use this function in place of gluperspective ) left top z x bottom right near far R.W. Lindeman - WPI Dept. of Computer Science 12
glortho left, right, bottom, top, near, far ) o For orthographic projection left top z x bottom right near far R.W. Lindeman - WPI Dept. of Computer Science 13
Example: Projection Transformation void displa ) { glclear GL_COLOR_BUFFER_BIT ); glmatrixmode GL_PROJECTION ); glloadidentit ); gluperspective FovY, Aspect, Near, Far ); glmatrixmode GL_MODELVIEW ); glloadidentit ); glulookat 0, 0, 1, 0, 0, 0, 0, 1, 0 ); mdispla ); // our displa routine } R.W. Lindeman - WPI Dept. of Computer Science 14
Projection Transformation o Projection n Map the object from 3D space to 2D screen z z x x Perspective: gluperspective ) Parallel: glortho ) R.W. Lindeman - WPI Dept. of Computer Science 15
Parallel Projection The Math) o After transforming the object to ee space, parallel projection is relativel eas: we could just set all Z to the same value n X p = x n Y p = n Z p = -d X p, Y p ) z x o We actuall want to remember Z wh? x,,z) R.W. Lindeman - WPI Dept. of Computer Science 16
Parallel Projection o OpenGL maps projects) everthing in the visible volume into a canonical view volume CVV) x max, max, far) 1, 1, -1) x min, min, near) glortho xmin, xmax, min, max, near, far ) -1, -1, 1) Canonical View Volume Projection: Need to build 4x4 matrix to do mapping from actual view volume to CVV R.W. Lindeman - WPI Dept. of Computer Science 17
Parallel Projection: glortho o Parallel projection can be broken down into two parts n Translation, which centers view volume at origin n Scaling, which reduces cuboid of arbitrar dimensions to canonical cube o Dimension 2, centered at origin R.W. Lindeman - WPI Dept. of Computer Science 18
Parallel Projection: glortho cont.) o Translation sequence moves midpoint of view volume to coincide with origin n e.g., midpoint of x = x max + x min )/2 o Thus, translation factors are -x max +x min )/2, - max + min )/2, -far+near)/2 o So, translation matrix M1: # 1 0 0 x max+ x min)/2& 0 1 0 max+ min)/2 0 0 1 z max+ z min)/2 $ 0 0 0 1 ' R.W. Lindeman - WPI Dept. of Computer Science 19
Parallel Projection: glortho cont.) o Scaling factor is ratio of cube dimension to Ortho view volume dimension o Scaling factors 2/x max -x min ), 2/ max - min ), 2/z max -z min ) o So, scaling matrix M2: # 2 & 0 0 0 x max x min 2 0 0 0 max min 2 0 0 zmax zmin 0 $ 0 0 0 1' R.W. Lindeman - WPI Dept. of Computer Science 20
Parallel Projection: glortho) cont.) o Concatenating M1xM2, we get transform matrix used b glortho # 2 & 0 0 0 x max x min 2 0 0 0 max min 2 0 0 zmax zmin 0 $ 0 0 0 1' X # 1 0 0 x max+ x min)/2& 0 1 0 max+ min)/2 0 0 1 z max+ z min)/2 $ 0 0 0 1 ' $ 2 /x max x min) 0 0 x max+ x min)/x max x min) ' & ) 0 2/ max min) 0 max+ min)/ max min) M2 M1 = & ) & 0 0 2/zmax zmin) zmax+ zmin)/zmax zmin) ) & ) 0 0 0 1 Refer to: Hill, 7.6.2 R.W. Lindeman - WPI Dept. of Computer Science 21
Perspective Projection: Classical o Side view 0,0,0) Projection plane x,,z ) d -z x,,z) Ee center of projection ) z z x Based on similar triangles: -z = d d = * -z R.W. Lindeman - WPI Dept. of Computer Science 22
Perspective Projection: Classical cont.) o So x*, *), the projection of point, x,, z) onto the near plane N, is given as n Similar triangles # x*, *) = N P x,n P & $ P z P z ' o Numerical example Q: Where on the viewplane does P = 1, 0.5, -1.5) lie for a near plane at N = 1? x*, *) = 1 x 1/1.5, 1 x 0.5/1.5) = 0.666, 0.333) R.W. Lindeman - WPI Dept. of Computer Science 23
Pseudo Depth Checking o Classical perspective projection drops z coordinates o But we need z to find closest object depth testing) o Keeping actual distance of P from ee is cumbersome and slow distance = P 2 x + P 2 2 + P z ) o Introduce pseudodepth: all we need is a measure of which objects are further if two points project to the same x, ) # x*, *,z *) = N P & x,n P, ap z + b P z P z P z ' o Choose a, b so that pseudodepth varies from 1 to 1 canonical cube) R.W. Lindeman - WPI Dept. of Computer Science 24 $
Pseudo Depth Checking cont.) o Solving: z* = ap z + b P z o For two conditions, z* = -1 when P z = -N and z* = 1 when P z = -F, we can set up two simultaneous equations o Solving for a and b, we get a = F + N) F N b = 2FN F N R.W. Lindeman - WPI Dept. of Computer Science 25
Homogenous Coordinates o Would like to express projection as 4x4 transform matrix o Previousl, homogeneous coordinates for the point P = P x, P, P z ) was P x, P, P z, 1) o Introduce arbitrar scaling factor, w, so that P = wp x, wp, wp z, w) Note: w is non-zero) o For example, the point P = 2, 4, 6) can be expressed as n 2, 4, 6, 1) n or 4, 8, 12, 2) where w=2 n or 6, 12, 18, 3) where w = 3 o So, to convert from homogeneous back to ordinar coordinates, divide all four terms b last component and discard 4 th term R.W. Lindeman - WPI Dept. of Computer Science 26
Perspective Projection o Same for x, so we have x = x * d / -z = * d / -z z = -d o Put in a matrix form # 1 0 0 0& # x& 0 1 0 0 0 0 1 0 z 0 0 1 $ d ) 1 ' $ 1' # x' & ' = z' $ w' # d x ) & z # d & $ z ' d $ 1 ' OpenGL assumes d = 1, i.e., the image plane is at z = -1 R.W. Lindeman - WPI Dept. of Computer Science 27
Perspective Projection cont.) o We are not done et! o Need to modif the projection matrix to include a and b x 1 0 0 0 x = 0 1 0 0 z 0 0 a b z w 0 0 1/-d) 0 1 z x o We have alread solved a and b Z = 1 z = -1 R.W. Lindeman - WPI Dept. of Computer Science 28
Perspective Projection cont.) o Not done et! OpenGL also normalizes the x and ranges of the view frustum to [-1, 1] translate and scale) o So, as in ortho, to arrive at final projection matrix n We translate b o xmax + xmin)/2 in x o -max + min)/2 in n And scale b o 2/xmax xmin) in x o 2/max min) in R.W. Lindeman - WPI Dept. of Computer Science 29
Perspective Projection cont.) o Final projection matrix glfrustum xmin, xmax, min, max, N, F ) n N = near plane, F = far plane # 2N x max x min 0 $ x max+ x min 0 0 x max x min 2N max+ min 0 max min max min F + N) 0 0 F N 0 0 1 0 2FN F N & ' R.W. Lindeman - WPI Dept. of Computer Science 30
Perspective Projection cont.) o After perspective projection, viewing frustum is also projected into a canonical view volume like in parallel projection) 1, 1, -1) z x -1, -1, 1) Canonical View Volume R.W. Lindeman - WPI Dept. of Computer Science 31