INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Computer Viewing and Projection David Carr Fundamentals of Computer Graphics Spring 24 Based on Slides by E. Angel Projection 1 L Overview Computer Viewing - Introduction to the mathematics of projection - OpenGL viewing functions - Alternate viewing APIs Projection Matrices - Derive the projection matrices used for standard OpenGL projections - Introduce oblique projections - Introduce projection normalization Projection 2 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Computer Viewing Projection 3 L 1
Three Aspects of Computer Viewing Positioning the camera - Setting the model-view matrix Selecting a lens - Setting the projection matrix Clipping - Setting the view volume All are implemented in the pipeline Projection 4 L The OpenGL Camera In OpenGL: - Initially the world and camera frames are the same - Default model-view matrix is an identity The camera is: - Located at origin - Points in the negative z-direction OpenGL also specifies: - Default view volume + Cube with sides of length 2 + Centered at the origin - Default projection matrix is an identity Projection 5 L Default Projection Default projection is orthogonal 2 clipped out z= Projection 6 L 2
Moving the Camera Frame To visualize an object with both positive and negative z values: - Move the camera in the positive z direction + Translate the camera frame - Move the objects in the negative z direction + Translate the world frame Both of these views are: - Equivalent - Determined by the model-view matrix - Want a translation: + gl.gltranslatef(.,.,-d); with d > Projection 7 L Moving Camera back from Origin Default Frames Frames after Translation by d d > Projection 8 L Moving the Camera We can move the camera to any desired position by a sequence of : - Rotations and translations Example: side view - Rotate the camera - Move it away from origin - Model-view matrix C = TR Projection 9 L 3
OpenGL code Remember that last transformation specified is first to be applied gl.glmatrixmode(gl_modelview) gl.glloadidentity(); gl.gltranslatef(.,., -d); gl.glrotatef(9.,., 1.,.); Projection 1 L The LookAt Method The GLU object contains the function gllookat - Simple interface to form the model-view matrix - Specify camera location (eye) - Specify point in view to look at (at) - Specify a vector representing the upward direction (up) Still need to initialize - Can concatenate with modeling transformations Example: isometric view of cube aligned with axes gl.glmatrixmode(gl_modelview): gl.glloadidentity(); glu.glulookat(1.,1.,1.,.,.,.,.,1.,.); Projection 11 L LookAt glu.glulookat(eyex,eyey,eyez, atx,aty,atz, upx,upy,upz) Projection 12 L 4
Other Viewing APIs The LookAt function is only one possible API for positioning the camera Others include - View reference point, view plane normal, view up (PHIGS, GKS-3D) - Yaw, pitch, roll - Elevation, azimuth, twist - Direction angles Projection 13 L Projections and Normalization The default projection in the eye (camera) frame is orthogonal For points within the default view volume x p = x y p = y z p = Most graphics systems use view normalization - All other views are converted to the default view by transformations that determine the projection matrix - Allows use of the same pipeline for all views Projection 14 L Homogeneous Coordinate Representation xp = x yp = y zp = wp = 1 M = È1 Î p p = Mp 1 1 In practice, we can let M = I and set the z term to zero later Projection 15 L 5
Simple Perspective Center of projection at the origin Projection plane z = d, d < Projection 16 L Consider top and side views Perspective Equations x z / d x p = x z / d y p = y z / d z p = d Projection 17 L consider q = Mp where È1 M = Î Homogeneous Coordinate Form Èx y z Î1 q = fi p = È Î 1 x y z z / d 1/ d Projection 18 L 1 6
x p = x z / d Perspective Division However w 1, so we must divide by w to return from homogeneous coordinates This perspective division yields y p = y z / d z p = d The desired perspective equations We will consider the corresponding clipping volume with the OpenGL functions Projection 19 L OpenGL Orthogonal Viewing gl.glortho(xmin,xmax,ymin,ymax,near,far) gl.glortho(left,right,bottom,top,near,far) near and far measured from camera Projection 2 L OpenGL Perspective gl.glfrustum(xmin,xmax,ymin,ymax,near,far) Projection 21 L 7
Using Field of View With glfrustum it is often difficult to get the desired view glu.gluperpective(fovy, aspect, near, far) often provides a better interface front plane aspect = w/h Projection 22 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Projection Matrices Projection 23 L Normalization Rather than derive a different projection matrix for each type of projection Convert all projections to orthogonal projections with the default view volume This strategy allows us to: - Use standard transformations in the pipeline - Make clipping efficient Projection 24 L 8
Pipeline View Model-View Transformation nonsingular Projection Transformation Perspective Division 4D Æ 3D Clipping Against Default Cube Projection 3D Æ 2D Projection 25 L Notes We stay in four-dimensional homogeneous coordinates - For both the model-view and projection transformations - Both these transformations are nonsingular - Default to identity matrices (orthogonal view) Normalization lets us clip against simple cube regardless of type of projection Delay final projection until end - Important for hidden-surface removal to retain depth information as long as possible Projection 26 L Orthogonal Normalization glortho(left,right,bottom,top,near,far) normalization fi find transformation to convert specified clipping volume to default Projection 27 L 9
Orthogonal Matrix Two steps - Move center to origin + T(-(left+right)/2, -(bottom+top)/2,(near+far)/2)) - Scale to have sides of length 2 + S(2/(left-right),2/(top-bottom),2/(near-far)) P = ST = È 2 right - left - right - left right - left 2 top + bottom - top - bottom top - bottom 2 far + near near - far far - near Î 1 Projection 28 L Final Projection Set z = Equivalent to the homogeneous coordinate transformation È1 1 M orth = Î 1 Hence, general orthogonal projection in 4D is P = M orth ST Projection 29 L Oblique Projections The OpenGL projection functions cannot produce general parallel projections such as However: - If we look at a cube it appears that the cube has been sheared Oblique Projection = Shear + Orthogonal Projection Projection 3 L 1
General Shear top view side view Projection 31 L Shear Matrix x-y shear (z values unchanged) È 1 -cotq H(q,f) = 1 -cot f 1 Î 1 Projection matrix General case: P = M orth H(q,f) P = M orth STH(q,f) Projection 32 L Equivalency Projection 33 L 11
Effect on Clipping The projection matrix P = STH transforms the original clipping volume to the default clipping volume object top view z = 1 DOP clipping volume DOP x = -1 x = 1 far plane z = -1 near plane distorted object (projects correctly) Projection 34 L Simple Perspective Consider a simple perspective with the COP at the origin, the near clipping plane at z = -1, and a 9 degree field of view determined by the planes x = ±z, y = ±z Projection 35 L Perspective Matrices Simple projection matrix in homogeneous coordinates M = È1 Î 1 1-1 Note that this matrix is independent of the far clipping plane Projection 36 L 12
Generalization N = È 1 1 a b Î -1 after persepective divison, the point (x, y, z, 1) goes to x = x/z y = y/z z = -(a+b/z) which projects orthogonally to the desired point regardless of a and b Projection 37 L Picking a and b If we pick near + far a = far - near 2near * far b = near - far the near plane is mapped to z = -1 the far plane is mapped to z =1 and the sides are mapped to x = ± 1, y = ± 1 Hence the new clipping volume is the default clipping volume Projection 38 L Normalization Transformation distorted object projects correctly original clipping volume original object new clipping volume Projection 39 L 13
Normalization and Hidden-Surface Removal Although our selection of the form of the perspective matrices may appear somewhat arbitrary, - Chosen so that if z 1 > z 2 in the original clipping volume - Then the for the transformed points z 1 > z 2 Thus, hidden surface removal works if the normalization transformation is applied first However, the formula z = -(a+b/z) implies that - Distances are distorted by the normalization - Can cause numerical problems especially if the near distance is small Projection 4 L OpenGL Perspective gl.glfrustum allows for an non-symmetric viewing frustum (although glu.gluperspective does not) Projection 41 L OpenGL Perspective Matrix The normalization in gl.glfrustum require: - Initial shear to form a right viewing pyramid, followed by - Scaling to get the normalized perspective volume. - The perspective matrix results in needing only a final orthogonal transformation P = NSH our previously defined perspective matrix shear and scale Projection 42 L 14
Why do we do it this way? Allows for a single pipeline for both - Perspective viewing - Orthogonal viewing Keep in 4-dimensional homogeneous coordinates - As long as possible - Retain 3-dimensional information needed for hidden-surface removal and shading Simplify clipping Projection 43 L Questions? Projection 44 L 15