CS 4731/543: Computer Graphics Lecture 5 (Part I): Projection Emmanuel Agu
3D Viewing and View Volume Recall: 3D viewing set up
Projection Transformation View volume can have different shapes (different looks) Different tpes of projection: parallel, perspective, orthographic, etc Important to control Projection tpe: perspective or orthographic, etc. Field of view and image aspect ratio Near and far clipping planes
Perspective Projection Similar to real world Characteried b object foreshortening Objects appear larger if the are closer to camera Need: Projection center Projection plane Projection: Connecting the object to the projection center camera projection plane
Projection? Projectors Object in 3 space Projected image COP VRP
Orthographic Projection No foreshortening effect distance from camera does not matter The projection center is at infinite Projection calculation just drop coordinates
Field of View Determine how much of the world is taken into the picture Larger field of view = smaller object projection sie field of view (view angle) center of projection θ
Near and Far Clipping Planes Onl objects between near and far planes are drawn Near plane + far plane + field of view = Viewing Frustum Near plane Far plane
Viewing Frustrum 3D counterpart of 2D world clip window Objects outside the frustum are clipped Near plane Far plane Viewing Frustum
Projection Transformation In OpenGL: Set the matri mode to GL_PROJECTION Perspective projection: use gluperspective(fov, aspect, near, far) or glfrustum(left, right, bottom, top, near, far) Orthographic: glortho(left, right, bottom, top, near, far)
gluperspective(fov, aspect, near, far) Aspect ratio is used to calculate the window width fov ee w h Aspect = w / h near far
glfrustum(left, right, bottom, top, near, far) Can use this function in place of gluperspective() left top bottom right near far
glortho(left, right, bottom, top, near, far) For orthographic projection left top bottom right near far
Eample: Projection Transformation void displa() { glclear(gl_color_buffer_bit); glmatrimode(gl_projection); glloadidentit(); gluperspective(fov, aspect, near, far); glmatrimode(gl_modelview); glloadidentit(); glulookat(,,1,,,,,1,); displa_all(); // our displa routine }
Demo Nate Robbins demo on projection
Projection Transformation Projection map the object from 3D space to 2D screen Perspective: gluperspective() Parallel: glortho()
Parallel Projection After transforming the object to the ee space, parallel projection is relativel eas we could just drop the Z Xp = Yp = Zp = -d (Xp, Yp) We actuall want to keep Z wh? (,,)
Parallel Projection OpenGL maps (projects) everthing in the visible volume into a canonical view volume (ma, ma, far) (1, 1, -1) (min, min, near) (-1, -1, 1) Canonical View Volume glortho(min, ma, min, ma,near, far) Projection: Need to build 44 matri to do mapping from actual view volume to CVV
Parallel Projection: glortho Parallel projection can be broken down into two parts Translation which centers view volume at origin Scaling which reduces cuboid of arbitrar dimensions to canonical cube (dimension 2, centered at origin)
Parallel Projection: glortho Translation sequence moves midpoint of view volume to coincide with origin: E.g. midpoint of = (ma + min)/2 Thus translation factors: -(ma+min)/2, -(ma+min)/2, -(far+near)/2 And translation matri M1: 1 1 1 ( ( ( ma ma ma + + + 1 min) min) min) / / / 2 2 2
Parallel Projection: glortho Scaling factor is ratio of cube dimension to Ortho view volume dimension Scaling factors: 2/(ma-min), 2/(ma-min), 2/(ma-min) Scaling Matri M2: 2 ma min 2 ma min 2 ma min 1
Parallel Projection: glortho + + + = 1 min) ma min) /( ma ( min) ma 2 /( min) ma min) /( ma ( min) ma 2 /( min) ma min) /( ma ( min) ma 2 /( 1 2 M M Refer: Hill, 7.6.2 Concatenating M1M2, we get transform matri used b glortho 1 min ma 2 min ma 2 min ma 2 X + + + 1 2 / min) ma ( 1 2 / min) ma ( 1 2 / min) ma ( 1
Perspective Projection: Classical Side view: Projection plane (,, ) (,,) Based on similar triangle: (,,) d - Ee (projection center) - = d = d -
Perspective Projection: Classical So (*,*) the projection of point, (,,) unto the near plane N is given as: Numerical eample: P ( *, *) = N, N P P Q. Where on the viewplane does P = (1,.5, -1.5) lie for a near plane at N = 1? P (*, *) = (1 1/1.5, 1.5/1.5) = (.666,.333)
Pseudodepth Classical perspective projection projects (,) coordinates, drops coordinates But we need to find closest object (depth testing) Keeping actual distance of P from ee is cumbersome and slow ( ) 2 2 2 P + P P dis tan ce = + Introduce pseudodepth: all we need is measure of which objects are further if two points project to same (,) P ( *, *, *) = N, N, P P P Choose a, b so that pseudodepth varies from 1 to 1 (canonical cube) P ap + b
Pseudodepth Solving: * = ap + b P For two conditions, * = -1 when P = -N and * = 1 when P = -F, we can set up two simultaneuous equations Solving: a = ( F + N) F N FN b = 2 F N
Homogenous Coordinates Would like to epress projection as 44 transform matri Previousl, homogeneous coordinates of the point P = (P,P,P) was (P,P,P,1) Introduce arbitrar scaling factor, w, so that P = (wp, wp, wp, w) (Note: w is non-ero) For eample, the point P = (2,4,6) can be epressed as (2,4,6,1) or (4,8,12,2) where w=2 or (6,12,18,3) where w = 3 So, to convert from homogeneous back to ordinar coordinates, divide all four terms b last component and discard 4 th term
Perspective Projection Same for. So we have: = d / - = d / - = -d Put in a matri form: OpenGL assumes d = 1, i.e. the image plane is at = -1 ( ) ( ) = 1 / ' ' ' 1 1 1 1 1 1 d d d d d
Perspective Projection We are not done et. Need to modif the projection matri to include a and b 1 = 1 a b w (1/-d) 1 We have alread solved a and b Z = 1 = -1
Perspective Projection Not done et. OpenGL also normalies the and ranges of the viewing frustum to [-1, 1] (translate and scale) So, as in ortho to arrive at final projection matri we translate b (ma + min)/2 in -(ma + min)/2 in Scale b: 2/(ma min) in 2/(ma min) in
Perspective Projection Final Projection Matri: 2N ma min 2N ma min ma+ min ma min ma+ min ma min ( F + N) F N 1 2FN F N glfrustum(min, ma, min, ma, N, F) N = near plane, F = far plane
Perspective Projection After perspective projection, viewing frustum is also projected into a canonical view volume (like in parallel projection) (1, 1, -1) (-1, -1, 1) Canonical View Volume
References Hill, chapter 7