4BA6 - Topic 4 Dr. Steven Collins Chap. 5 3D Viewing and Projections
References Computer graphics: principles & practice, Fole, vandam, Feiner, Hughes, S-LEN 5.644 M23*;-6 (has a good appendix on linear algebra) Advanced Animation and Rendering Techniques, Watt and Watt, S-LEN 5.8 N26;2-5 The OpenGL Programming Guide, Woo, Neider & Davis, S- LEN 5.8 N72;-2 Interactive Computer Graphics, Edward Angel 2
OpenGL Geometr Pipeline x z w original vertex MODELVIEW matrix x z w ee ee ee ee vertex in the ee coordinate space PROJECTION matrix x w proj proj proj 2D projection of vertex onto viewing plane perspective division x dev dev normalised device coordinates (foreshortened) viewport transformation x win win final window coordinates 3
The Camera Sstem To create a view of a scene we need: a description of the scene geometr a camera or view definition Default OpenGL camera is located at the origin looking down the -z axis. The camera definition allows projection of the 3D scene geometr onto a 2D surface for displa. This projection can take a number of forms: orthographic (parallel lines preserved) perspective (foreshortening): -point, 2-point or 3-point skewed orthographic 4
Camera Tpes Before generating an image we must choose our viewer: The pinhole camera model is most widel used: infinite depth of field (everthing is in focus) Advanced rendering sstems model the camera double gauss lens as used in man professional cameras model depth of field and non-linear optics (including lens flare) Photorealistic rendering sstems often emplo a phsical model of the ee for rendering images model the ees response to varing brightness and colour levels model the internal optics of the ee itself (diffraction b lens fibres etc.) 5
Pinhole Camera Model 6
Modeling the Ee s Response Adaptation Glare & Diffraction 7
Camera Sstems A camera model implemented in Princeton Universit (995) 8
Viewing Sstem We are onl concerned with the geometr of viewing at this stage. The camera s position and orientation define a viewvolume or view-frustrum. objects completel or partiall within this volume are potentiall visible on the viewport. objects full outside this volume cannot be seen clipped clipped view frustrum clipping planes 9
Camera Models CTM= Current Transformation Matrix Each vertex in our model must be projected onto the 2D camera viewport plane in order to be displa on the screen. The CTM is emploed to determine the location of each vertex in the camera coordinate sstem: x = MCTM x We then emplo a projection matrix defined b GL_PROJECTION to map this to a 2D viewport coordinate. Finall, this 2D coordinate is mapped to device coordinates using the viewport definition (given b glviewport()).
Camera Modeling in OpenGL camera coordinate sstem viewport coordinate sstem device/screen coordinate sstem glmatrixmode(gl_modelview)... glviewport(,,xres,res) glmatrixmode(gl_projection)...
3D 2D Projection Tpe of projection depends on a number of factors: location and orientation of the viewing plane (viewport) direction of projection (described b a vector) projection tpe: Projection Perspective Parallel -point Orthographic 2-point Axonometric 3-point Oblique 2
Parallel Projections axonometric orthographic oblique 3
Perspective Projections 3-point perspective -point perspective 2-point perspective 4
5 Orthogonal Projections The simplest of all projections, parallel project onto viewplane. Usuall view-plane is axis aligned (often at z=) = = where M MP P x z x
Multiple Projections It is often useful to have multiple projections available at an given time usuall: plan (top) view, front & left or right elevation (side) view Perspective Top Front Right 6
Orthogonal Projections The result is an orthographic projection if the object is axis aligned, otherwise it is an axonometric projection. If the projection plane intersects the principle axes at the same distance from the origin the projection is isometric. x + + z = 7
Parallel Projections in OpenGL glortho(xmin, xmax, min, max, zmin, zmax); Note: we alwas view in -z direction need to transform world in order to view in other arbitrar directions. 8
Perspective Projections Perspective projections are more complex and exhibit foreshortening (parallel appear to converge at points). Parameters: centre of projection (COP) field of view (θ,φ) projection direction up direction 9
2 Perspective Projections Consider a perspective projection with the viewpoint at the origin and a viewing direction oriented along the positive -z axis and the view-plane located at z = -d p d z d z P P = = -z d = = z x d d z z x d d z d z x z x P P P divide b homogenous ordinate to map back to 3D space a similar construction for x p
2 Perspective Projections Details = d z z x d d z d z x z x P P P = z x z x Flip z to transform to a left handed co-ordinate sstem increasing z values mean increasing distance from the viewer. PROJECTION matrix perspective division
Perspective Projection Depending on the application we can use different mechanisms to specif a perspective view. Example: the field of view angles ma be derived if the distance to the viewing plane is known. Example: the viewing direction ma be obtained if a point in the scene is identified that we wish to look at. OpenGL supports this b providing different methods of specifing the perspective view: glulookat, glfrustrum and gluperspective 22
Perspective Projections glfrustrum(xmin, xmax, min, max, zmin, zmax); 23
glfrustrum Note that all points on the line defined b (xmin,min,- zmin) and COP are mapped to the lower left point on the viewport. Also all points on the line defined b (xmax,max,-zmin) and COP are mapped to the upper right corner of the viewport. The viewing direction is alwas parallel to -z It is not necessar to have a smmetric frustrum like: glfrustrum(-.,., -.,., 5., 5.); Non smmetric frustrums introduce obliqueness into the projection. zmin and zmax are specified as positive distances along -z 24
Perspective Projections gluperspective(fov, aspect, near, far); h 2 θ = tan h = 2near tan θ near 2 2 25
gluperspective A utilit function to simplif the specification of perspective views. Onl allows creation of smmetric frustrums. Viewpoint is at the origin and the viewing direction is the -z axis. The field of view angle, fov, must be in the range [..8] aspect allows the creation of a view frustrum that matches the aspect ratio of the viewport to eliminate distortion. 26
Perspective Projections 27
Lens Configurations mm Lens (fov = 22 ) 2mm Lens (fov = 84 ) 35mm Lens (fov = 54 ) 2mm Lens (fov = ) 28
Positioning the Camera The previous projections had limitations: usuall fixed origin and fixed projection direction To obtain arbitrar camera orientations and positions we manipulate the MODELVIEW matrix prior to creation of the models. This positions the camera w.r.t. the model. We wish to position the camera at (, 2, ) w.r.t. the world Two possibilities: transform the world prior to creation of objects using translatef and rotatef: gltranslatef(-, -2, -); use glulookat to position the camera with respect to the world co-ordinate sstem: glulookat(, 2,, ); Both are equivalent. 29
Positioning the Camera glulookat(eex, ee, eez, lookx, look, lookz, upx, up, upz); equivalent to: gltranslatef(-eex, -ee, -eez); glrotatef(theta,.,.,.); glrotatef(phi,.,.,.); 3
Projection window The projection matrix defines the mapping from a 3D world co-ordinate to a 2D viewport co-ordinate. The window extents are defined as a parameter of the projection: glfrustrum(l,r,b,t,n,f) (r,t,-n) (l,b,-n) (w,h,-n) gluperspective(f,a,n,f) (-w,-h,-n) h w f = n tan 2 = h a 3
Projection window We need to associate the 2D window co-ordinate sstem with the viewport co-ordinate sstem in order to determine the correct pixel associated with each vertex. normalised device co-ordinates viewport co-ordinates 32
Window to Viewport Transformation: review An affine planar transformation is used. After projection to the window, all points are transformed to normalised device co-ordinates: [-,]x[,] x n n x = 2 x max = 2 p p max x x min min min min glviewport used to relate the co-ordinate sstems: glviewport(int x, int, int width, int height); 33
Window to Viewport Transformation: review (x,) = location of bottom left of viewport within the window width,height = dimension in pixels of the viewport x w = width + 2 height + 2 ( x + ) x = ( + ) n w n normall we re-create the window after a window resize event to ensure a correct mapping between window and viewport dimensions: static void reshape(int width, int height) { glviewport(,, width, height); glmatrixmode(gl_projection); glloadidentit(); gluperspective(85.,., 5, 5); } 34
Aspect Ratio The aspect ratio defines the relationship between the width and height of an image. Using gluperspective an viewport aspect ratio ma be explicitl provided, otherwise the aspect ratio is a function of the supplied viewport width and height. The aspect ratio of the window (defined b the user) must match the viewport aspect ratio to prevent unwanted affine distortion: aspect ratio =.25 aspect ratio =.5 35
Sample Viewport Application // top left: top view glviewport(, win_height/2, win_width/2, win_height/2); glmatrixmode(gl_projection); glloadidentit(); glortho(-3., 3., -3., 3.,., 5.); glulookat(., 5.,.,.,.,.,.,., -.); glmatrixmode(gl_modelview); glloadidentit(); glcalllist(object); // top right: right view glviewport(win_width/2, win_height/2, win_width/2, win_height/2); glmatrixmode(gl_projection); glloadidentit(); glortho(-3., 3., -3., 3.,., 5.); glulookat(5.,.,.,.,.,.,.,.,.); glmatrixmode(gl_modelview); glloadidentit(); glcalllist(object); // bottom left: front view glviewport(,, win_width/2, win_height/2); glmatrixmode(gl_projection); glloadidentit(); glortho(-3., 3., -3., 3.,., 5.); glulookat(.,., 5.,.,.,.,.,.,.); glmatrixmode(gl_modelview); glloadidentit(); glcalllist(object); // bottom right: rotating perspective view glviewport(win_width/2,, win_width/2, win_height/2); glmatrixmode(gl_projection); glloadidentit(); gluperspective(7.,.,, 5); glulookat(.,., 5.,.,.,.,.,.,.); glmatrixmode(gl_modelview); glloadidentit(); glrotatef(3.,.,.,.); glrotatef(angle,.,.,.); glcalllist(object); 36