Viewing How to see the object Type of object view How to set the view for CG The shadow creating suriyong l. Type of Views Examine how an app viewer can create a particular view within OpenGL Use model-view matrix to switch from world frame to camera frame: camera at origin Preferred projection type : parallel, perspective suriyong l. ® OpenGL x y z w original vertex Geometry Pipeline MODELVIEW PROJECTION matrix matrix xeye y eye zeye weye vertex in the eye coordinate space perspective division x proj \VEC4 y proj w proj 2d projection of vertex onto viewing plane 3 viewport transformation xdev y dev 1 normalized device coordinates (foreshortened) xwin y win final window coordinates Principle of view Classical view and computer view are Similar COP at infinity Viewing COP: Center Of Projection ex. eye , camera lens, for CG: the camera frame DOP: Direct OP - view at infinite distance suriyong l. COP condition Finite distance view perspective view Infinite distance view Parallel view Limiting case of perspective view Treat as separate case API support both view User easily switch between view Use planar geometric projection viewing method: Object project on planar surface (plane and projection lines) 2 types of view for CG but classical permit more than one viewer one-, two- etc. suriyong l. 3D 2D Projection Type of projection depends on a number of factors: location and orientation of the viewing plane (viewport) direction of projection (described by a vector) projection type: Projection Perspective Parallel 1-point Orthographic 2-point Axonometric 3-point Oblique 6 Classical view Underlying notion of principal face Object is composed of a number of planar faces front, back, top, bottom, etc. Classical views suriyong l. Orthographic projections An Orthogonal view Projectors are perpendicular to projection plane Multiview Orthographic projection Projection plane is parallel to one of the principle faces, usually 3 faces Orthographic projection suriyong l. Temple and three multiview orthographic projections Orthographic properties Preserve both distances and angles: No distortion in shape Projectors perpendicular to plane Projection plane is parallel to one principal of object face Applied in drawings suriyong l. Axonometric projection Plane rotational orthogonal projection Plane is not perpendicular to 1 face Special case Isometric : plane is places symmetrically with 3 simple faces Dimetric: plane is places symmetrically with 2 simple faces Trimetric No faces are places symmetrically with plane suriyong l. Axonometric projections. (a) Construction of trimetric-view projection. (b) Top view. (c) Side view. Axonometric views suriyong l. Viewing System We are only concerned with the geometry of viewing at this stage. The camera’s position and orientation define a viewvolume or view-frustrum. view frustrum objects completely or partially within this volume are potentially visible on the viewport. objects fully outside this volume cannot be seen clipped 12 clipped clipping planes Camera Models Each vertex in our model must be projected onto the 2D camera viewport plane in order to be display on the screen. The CTM is employed to determine the location of each vertex in the camera coordinate system: 𝑥 ′ = 𝑀𝐶𝑇𝑀 𝑥 We then employ a projection matrix defined by GL_PROJECTION to map this to a 2D viewport coordinate. Finally, this 2D coordinate is mapped to device coordinates using the viewport definition (given by glViewport()). 13 Camera Modeling in OpenGL ® camera coordinate system viewport coordinate system glMatrixMode(GL_MODELVIEW) ... device/screen coordinate system glViewport(0,0,xres,yres) glMatrixMode(GL_PROJECTION) ... 14 Parallel Projections orthographic axonometric oblique 15 Perspective Projections 3-point perspective 1-point perspective 2-point perspective 16 Orthogonal Projections The simplest of all projections, parallel project onto viewplane. Usually view-plane is axis aligned (often at z=0) x x 1 y y 0 P MP where M z 0 0 1 1 0 17 0 1 0 0 0 0 0 0 0 0 0 1 Multiple Projections It is often useful to have multiple projections available at any given time usually: plan (top) view, front & left or right elevation (side) view Perspective Top Front Right 18 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 y z 1 19 Parallel Projections in OpenGL® glOrtho(xmin, xmax, ymin, ymax, zmin, zmax); Note: we always view in -z direction need to transform world in order to view in other arbitrary directions. 20 Perspective Projections Perspective projections are more complex and exhibit foreshortening (parallel appear to converge at points). Parameters: centre of projection (COP) field of view (q, f) projection direction up direction 21 Oblique View Projector make an arbitrary to projecting plane Able to see more than 1 face at the same time Most difficult to draw by hand Lens parallel to plane Unnatural Oblique view (a) Construction. (b) Top view. (c) Side view. suriyong l. Perspective View Diminution of size Unable to measure Real-looking image For classical perspective 1-, 2- or 3- point perspective Perspective view Classical perspective views: The (a) three-, (b) two-, and (c)suriyong one-point l. perspectives The Camera System To create a view of a scene we need: a description of the scene geometry 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 geometry onto a 2D surface for display. This projection can take a number of forms: orthographic (parallel lines preserved) perspective (foreshortening): 1-point, 2-point or 3-point skewed orthographic 24 Camera Types Before generating an image we must choose our viewer: The pinhole camera model is most widely used: infinite depth of field (everything is in focus) Advanced rendering systems model the camera double gauss lens as used in many professional cameras model depth of field and non-linear optics (including lens flare) Photorealistic rendering systems often employ a physical model of the eye for rendering images model the eyes response to varying brightness and colour levels model the internal optics of the eye itself (diffraction by lens fibres etc.) 25 Pinhole Camera Model 26 Modeling the Eye’s Response Adaptation Glare & Diffraction 27 Camera Systems A camera model implemented in Princeton University (1995) 28 Computer view principle OpenGL View Perspective camera – 1 point of view Orthogonal camera Initial camera position. Imaging using the default camera. suriyong l. Positioning of the camera Convert reference of model frame to camera frame First at point P (both frame at vertex) Last at point Q Camera frame C Initial camera position. Movement of the camera and world frames. (a) Initial suriyong (b) l. Configuration after change in the model view configuration. matrix Suppose to see object at +x direction glMatrixMode(GL_MODELVIEW); //Initialize matrix_mode to Model_view glLoadIdentity(); glRotatef(90.0, 0.0, 1.0, 0.0); // move camera to x axis (rotate around +y) glTranslatef(0, 0, -d); // Translate far from origin with distance d suriyong l. Create Isometric view Object moving Transform object to isometric view type Camera moving Move the camera to the isometric view type suriyong l. Object moving // Assume that we have 2 unit cube glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -d); // move camera backward glRotatef(35.26, 1.0, 0.0, 0.0); // Rotate around x axis (35.26o) // Point (-1, 1, 1) move to (0,0, √2) glRotatef(45.0, 0.0, 1.0, 0.0); // Rotate around y axis (45o) glOrtho(…); // view clipping The (-1, 1, 1) is rotate to the new point (0, 0, √2) and translate to -d positive z-axis view x-axis view suriyong l. Create an Isometric view Symmetrically rotate object to see 3 faces together Rotate about the y-axis (45o) and x-axis (-35.26o) consequently Move away from WCS with distance d M = T(-dz).Ry(45) .Rx(-35.26) suriyong l. Camera moving Two Viewing APIs (Normalization transformation process) Start at world frame, camera at origin look to –z direction, define the projection point on projection plane as VRP (View Reference Point) Specify VRP – View Reference Point with user’s function set_view_reference_point(x, y, z); Specify camera orientation: fix the camera orientation - user’s function VPN (View Plane Normal vector - n) set_view_plane_normal(nx, ny, nz); VUP (View UP vector) : up direction set_view_up(vup_x, vup_y, vup_z); suriyong l. Camera frame. Specify v from VUP Projection Create u from v, n cross product 𝑢 =𝑣×𝑛 Get new orthogonal coordinate system u-v-n called viewing-coordinate system matrix that does change frame called view-orientation matrix Let p is VRP x y p z 1 View plane normal (n) nx n n y nz 0 VUP vupx vup y vup vupz 0 Target: Fine the isometric matrix which composed of translation and rotation V = RT // V: the isometric matrix Steps: Move the camera to the VRP by translation matrix let T (-x, -y, -z) suriyong l. Determination of the view-up vector. Handing rotation Find 𝑣 as factor of 𝑛 and 𝑣𝑢𝑝 𝑣 = 𝛼𝑛 + 𝛽𝑣𝑢𝑝 Let 𝛽 = 1 (don’t care the length of 𝑣𝑢𝑝 at first) 𝑣 = 𝛼𝑛 + 𝑣𝑢𝑝 and 𝛼=− 𝑣 is the proportion of 𝑣𝑢𝑝 and 𝑛. 𝑣𝑢𝑝 ; 𝑛. 𝑣 = 0 𝑛. 𝑛 𝑣𝑢𝑝 . 𝑛 𝑣 = 𝑣𝑢𝑝 − 𝑛 𝑛. 𝑛 Find 𝑢 from 𝑣 and 𝑛 : 𝑢 = 𝑣 × 𝑛 Modify vectors to their normalize form Let 𝑢′ , 𝑣 ′ and 𝑛′ are normalized form of 𝑢, 𝑣 and 𝑛 Create rotation matrix M from 𝑢′ , 𝑣 ′ and 𝑛′ 𝑢𝑥′ 𝑣𝑥′ 𝑛′𝑥 0 𝑢𝑦′ 𝑣𝑦′ 𝑛′𝑦 0 𝑀= ′ 𝑢𝑧 𝑣𝑧′ 𝑛′𝑧 0 0 0 0 1 suriyong l. We need matrix in the form of inverse thus, R = M-1 = MT ; R is the rotation matrix Finally, multiply by T then 𝑢𝑥′ 𝑣𝑥′ 𝑉 = 𝑅𝑇 = ′ 𝑛𝑥 0 𝑢𝑦′ 𝑣𝑦′ 𝑛𝑦′ 0 𝑢𝑧′ 𝑣𝑧′ 𝑛𝑧′ 0 −𝑥𝑢𝑥′ − 𝑦𝑢𝑦′ − 𝑧𝑢𝑧′ −𝑥𝑣𝑥′ − 𝑦𝑣𝑦′ − 𝑧𝑣𝑧′ −𝑥𝑛𝑥′ − 𝑦𝑛𝑦′ − 𝑧𝑛𝑧′ 1 Note: Translation matrix is on the right: Object moves to the left, Camera moves to the right. Camera was specified in the world frame both have similar forms suriyong l. The Look-At function Isometric viewing function Use model of VRP, VPN and VUP gluLookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); eyex, eyey, eyez : location of VRP atx,aty, atz : target object (VPN = [atx-eyex, aty – eyey]) upx, upy, upz : (VUP = [upx-eyex, upy-eyey]) Look-at positioning. suriyong l. OpenGL application sequence void display() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eyex,eyey,eyez, atx,aty,atz, upx,upy,upz); . . . // define objects here } suriyong l. Positioning the Camera The previous projections had limitations: usually fixed origin and fixed projection direction To obtain arbitrary camera orientations and positions we manipulate the MODELVIEW matrix prior to creation of the models. This positions the camera w.r.t. the model. Ex. position the camera at (10, 2, 10) w.r.t. the world Two possibilities: transform the world prior to creation of objects using translatef and rotatef: glTranslatef(-10, -2, -10); use gluLookAt to position the camera with respect to the world coordinate system: gluLookAt(10, 2, 10, … ); Both are equivalent. 41 Positioning the Camera gluLookAt(eyex, eyey, eyez, lookx, looky, lookz, upx, upy, upz); θ ϕ equivalent to: glTranslatef(-eyex, -eyey, -eyez); glRotatef(theta, 1.0, 0.0, 0.0); glRotatef(phi, 0.0, 1.0, 0.0); 42 Other Viewing APIs Roll, pitch and yaw Elevation and azimuth suriyong l. Perspective Projection Condition: COP at the origin Camera point to –ve Z axis We will consider the first case in detail Two possibility of camera for perspective projection (a) view plane parallel to lens and normal to z axis (b) view plane not parallel, but lens orthogonal to z axis suriyong l. Calculation step 1. Let point in space is located at 𝑥, 𝑦, 𝑧 2. Projection plane is perpendicular to –z axis and located at z = d 3. Project that point to projection plane, let’s say 𝑥𝑝 , 𝑦𝑝 , 𝑧𝑝 x z/d y yp z/d zp d xp Summary Three views of perspective projection. (a) Three-dimensional view. (b) Top view. (c) Side view. 1. Non uniform shortening 2. The farer of object, the size is much more reduce suriyong l. Using homogeneous coordinate 𝑤. 𝑥 𝑤. 𝑦 𝑝= ; we need 𝑤 = 1 ⇒ 𝑝 = 𝑤. 𝑧 𝑤 𝑥 𝑦 𝑧 1 ; devided by 𝑤 Thus Perspective projection transformation 𝑥 1 0 0 0 𝑥 𝑦 0 1 0 0 𝑦 𝑞 =𝑀×𝑝= × = 𝑧 0 0 1 0 𝑧 𝑧/𝑑 0 0 1/𝑑 0 1 If divided by 4th component 𝑥 𝑥𝑝 𝑧 𝑑 𝑦 𝑦𝑝 𝑞′ = = 𝑧 𝑝 𝑧 𝑑 1 𝑑 1 Projection pipeline. suriyong l. Orthogonal Projection Special case of perspective projection Projectors are perpendicular to view plane Rather than cop at infinity, let z = 0 then Transformation matrix form Orthogonal projection x p 1 0 y p 0 1 0 0 zp 1 0 0 suriyong l. 0 0 0 0 0 x 0 y 0 z 1 1 xp x yp y zp 0 Projection in OpenGL More property for camera: angle of view Only objects that fit in within angle of view of the camera appear in the image Definition of a view volume Front and back clipping planes suriyong l. Perspective in OpenGL 2 functions for specifying perspective view glFrustum(xmin, xmax, ymin, ymax, near, far); gluPerspective(fovy, aspect, near, far); Note: near, far are +ve, measured from COP (at z = 0) Specification of a frustum suriyong l. Perspective Projections glFrustrum(xmin, xmax, ymin, ymax, zmin, zmax); 50 glFrustrum notes (xmin,ymin,-zmin) and COP are mapped to the lower left point of viewport. (xmax,ymax,-zmin) and COP are mapped to the upper right corner of viewport. Viewing direction is always parallel to -z Symmetric frustrum is unnecessary: glFrustrum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0); Asymmetric frustrums introduce obliqueness into the projection. zmin and zmax are specified as positive distances along -z 51 We must select the matrix mode glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(xmin, xmax, ymin, ymax, near, far); Note: no need for xmin, xmax or ymin, ymax are symmetry to z axis Many application specify the angle of view projection plane need not to be square suriyong l. gluPerspective(fovy, aspect, near, far); // For specifying angle of view fov: angle between the top and bottom aspect: the ratio of w divided by h of projection plane near, far : plane specified as glFrustum Specification using the field of view. suriyong l. Perspective Projections gluPerspective(fov, aspect, near, far); ℎ/2 𝜃 = tan ; 𝑛𝑒𝑎𝑟 2 ℎ ; 2. 𝑛𝑒𝑎𝑟 𝜃 ℎ = 2. 𝑛𝑒𝑎𝑟. tan ; 2 𝜃 = 2 tan−1 54 gluPerspective A utility function to simplify the specification of perspective views. Only allows creation of symmetric 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 [0..180] apect allows the creation of a view frustrum that matches the aspect ratio of the viewport to eliminate distortion. 55 Perspective Projections 56 Lens Configurations 10mm Lens (fov = 122°) 20mm Lens (fov = 84°) 35mm Lens (fov = 54°) 200mm Lens (fov = 10°) 57 Parallel view in OpenGL Use function glOrtho glOrtho(xmin, xmax, ymin, ymax near, far) Or glOrtho(left, right, buttom, top, near, far) Orthographic viewing. suriyong l. Hidden surface removal Many algorithms Object-space algorithm: object’s surface has order Image space algorithm: (z-buffer algorithm) check the depth if surfaces are overlap The z-buffer algorithm. suriyong l. Function use for hidden surface removal Initializing the depth buffer and enable the hidden surface removal glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glEnable(GL_DEPTH_TEST); Clearing buffer for new rendering glClear(GL_DEPTH_BUFFER_BIT); suriyong l. Culling (selection) Removing all the surfaces pointing away from viewer (render only the one facing the viewer) By enable culling glEnable(GL_CULL_FACE); Work with convex object an z buffer If n cubes are passed trough pipeline, 6n faces are passed trough pipeline If culling, half of the surface is eliminated suriyong l. Walk trough a scene Use key to relocation the camera void keys(unsigned char key, int x, int y) { if(key == ‘x’) viewer[0] -= 1.0; // viewer[] is predefined array if(key == ‘X’) // to hold camera position viewer[0] += 1.0; if(key == ‘y’) viewer [1] -= 1.0; if(key == ‘Y’) viewer[1] += 1.0; if(key == ‘z’) viewer[2] -= 1.0; if(key == ‘Z’) viewer[2] += 1.0; glutPostRedisplay(); } suriyong l. void display(void) { glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // use gluLookAt to define camera view gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glFlush(); glutSwapBuffers(); } suriyong l. //Use “glFrustum” to specify the camera lens void myReshape (int w, int h) { glViewport (0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w<= h) glFrustum (-2.0, 2.0, -2.0*(GLfloat) h/ (GLfloat) w, 2.0*(GLfloat) h/ (GLfloat) w, 2.0, 20.0); else glFrustum(-2.0, 2.0, -2.0*(GLfloat) w/ (GLfloat) h, 2.0*(GLfloat) w/ (GLfloat) h, 2.0, 20.0); glMatrixMode(GL_MODELVIEW); } suriyong l. Projection normalization Convert all projections into orthogonal projection Distort the objects to the same as the desire projection of the original object Using homogeneous coordinate matrix by concatenate this matrix as shown Normalization transformation Predistortion of objects. (a) Perspective view. (b) Orthographic projection of distorted object. suriyong l. Orthogonal-Projection matrices Parallel viewing All point project in to the same point on projection plane If clipping volume is defined by 6 plane Called (canonical view volume) Use function call xp x yp y zp 0 x= 1 y = 1 z = 1 glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); Near plane is at z = 1, back of camera Far plane is at z = -1 front of camera suriyong l. Let set glOrtho(xmin, xmax, ymin, ymax, Near, Far ); Canonical view: Left side: xmin Right side xmax Top : ymax Bottom: ymin Front clipping plane at z = -near Back clipping plane at z = -far suriyong l. Mapping a view volume to the canonical view volume Effect: Former defined vertices are convert to canonical view Projection matrix is determined by type of view and the view volume is defined by glOrtho Affine transformations for normalization xmax xmin y ymin z z 2 2 2 P ST S , , , max , max min .T 2 2 2 xmax xmin ymax ymin zmax zmin 2 2 0 0 0 0 1 0 0 xmax xmin x x xmax xmin max min 2 2 2 ymax ymin 0 0 0 0 1 0 0 ymax ymin ymax ymin 2 z z 2 0 0 0 0 0 1 max min 0 0 2 z z max min 0 0 0 1 0 0 0 1 0 0 . 2 right left 0 0 0 0 0 2 top bottom 0 0 0 2 far near 0 right left right left top bottom top bottom far near far near 1 <- In term of near and far suriyong l. 0 0 2 far near 0 xmax xmin xmax xmin ymax ymin ymax ymin z z max min zmax zmin 1 Normalized Oblique Projection Oblique projection Oblique clipping volume tanq Oblique projection. (a) Top view. (b) Side view suriyong l. z x xp and thus x p x - z cotq Like wise y p y - z cot f zp 0 1 0 𝑃= 0 0 Breaking to 1 0 𝑃 = 𝑀𝑜𝑟𝑡ℎ 𝐻 𝜃, 𝜙 = 0 0 0 − cot 𝜃 1 − cot 𝜙 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 0 − cot 𝜃 − cot 𝜙 0 0 0 0 0 1 It is orthogonal projection matrix and shearing matrix • Oblique is done by shearing and projection • For 45 degree 1 0 𝑃 45,45 = 𝑀𝑜𝑟𝑡ℎ 𝐻 45,45 = 0 0 0 1 0 0 −1 −1 0 0 0 0 0 1 Effect of shear transformation suriyong l. Scale and translate before projection 2 x x min max 0 ST 0 0 0 0 2 ymax ymin 0 0 2 zmax zmin 0 0 Total process of oblique canonical projection matrix P MorthSTH suriyong l. xmax xmin xmax xmin ymax ymin ymax ymin z zmin max zmax zmin 1 Perspective transformation Like previous transformation First : transform to canonical view (normalization) Suppose the angle of view is 90O Simple perspective projection from 𝑧𝑚𝑎𝑥 , 𝑧𝑚𝑖𝑛 𝑡𝑜 𝑧 = ±1 suriyong l. Perspective projection Originally perspective 1 0 𝑀= 0 0 projection matrix at z = -1 0 0 0 1 0 0 0 1 0 0 −1 0 Apply N to homogeneous coordinate With 90O angle of view we get ±𝑥 = ±𝑧 ±𝑦 = ±𝑧 Introduce matrix N transformation matrix 1 0 𝑁= 0 0 0 1 0 0 0 0 𝛼 −1 0 0 𝛽 0 suriyong l. 𝑞 = 𝑁𝑝 𝑥′ 1 𝑦′ 0 = 0 𝑧′ 0 𝑤′ 0 1 0 0 0 0 𝛼 −1 𝑥′ = 𝑥 𝑦′ = 𝑦 𝑧 ′ = 𝛼𝑧 + 𝛽 𝑤 ′ = −𝑧 0 0 𝛽 0 𝑥 𝑦 𝑧 1 Devided by w’thus 𝑥 𝑧 𝑦 𝑦" = − 𝑧 𝑥" = − 𝑧" = − 𝛼 + 𝛽 𝑧 Apply an orthographic projection along the z axis to N 1 0 0 0 0 1 0 0 𝑀𝑜𝑟𝑡ℎ 𝑁 = 0 0 0 0 0 0 −1 0 which is a simple perspective-projection matrix, and the projection of the arbitrary point p is 𝑥 𝑦 𝑃′ = 𝑀𝑜𝑟𝑡ℎ 𝑁𝑝 = 0 −𝑧 After we do the perspective division, we obtain the desired values the 𝑥𝑝 and 𝑦𝑝 : 𝑥 𝑥𝑝 = − 𝑧 𝑦 𝑦𝑝 = − 𝑧 suriyong l. Set 𝑥", 𝑦" = ±𝑧 to the planes 𝑧 = ±1 thus 𝑥" = ±1, 𝑦" = ±1 The front of the view volume 𝑧 = 𝑧𝑚𝑎𝑥 is transformed to 𝑧“ = − 𝛼 + 𝛽 𝑧𝑚𝑎𝑥 Solution 𝛼= 𝑧𝑚𝑎𝑥 + 𝑧𝑚𝑖𝑛 , 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 𝛽=− 2𝑧𝑚𝑎𝑥 . 𝑧𝑚𝑖𝑛 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 1 0 0 1 0 0 𝑧𝑚𝑎𝑥 + 𝑧𝑚𝑖𝑛 𝑁= 0 0 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 0 0 −1 0 0 − 2𝑧𝑚𝑎𝑥 . 𝑧𝑚𝑖𝑛 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 0 Then z in the boundary of ±1 Perspective normalization of view volume N: perspective normalization matrix frustum => parallel pipe and orthographic projection suriyong l. OpenGL Perspective transformation Not limit on symmetric perspective Process is the same as convert from oblique parallel to orthogonal view Convert frustum to symmetric frustum: shear Scale the side of this frustum suriyong l. OpenGL perspective. to skew (shear) the point 𝑥𝑚𝑖𝑛 +𝑥𝑚𝑎𝑥 2 , 𝑦𝑚𝑎𝑥 +𝑦𝑚𝑖𝑛 2 , 𝑧𝑚𝑖𝑛 to 0,0, 𝑧𝑚𝑖𝑛 The required shearing matrix is 𝐻 𝜃, 𝜙 = 𝐻 cot −1 𝑥𝑚𝑖𝑛 + 𝑥𝑚𝑎𝑥 𝑦𝑚𝑎𝑥 + 𝑦𝑚𝑖𝑛 , cot −1 2𝑧𝑚𝑎𝑥 2𝑧𝑚𝑎𝑥 The result frustum is described by the planes 𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛 2𝑧𝑚𝑎𝑥 𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛 𝑦=± 2𝑧𝑚𝑎𝑥 𝑧𝑓𝑎𝑟 = 𝑧𝑚𝑖𝑛 𝑧𝑛𝑒𝑎𝑟 = 𝑧𝑚𝑎𝑥 𝑥=± The next step is to scale the sides of this frustum to 𝑥 = ±𝑧 𝑦 = ±𝑧 suriyong l. Next scaling: 2𝑧𝑚𝑖𝑛 𝑧𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛 𝑆= 0 0 0 0 2𝑧𝑚𝑖𝑛 𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛 0 0 0 0 0 0 0 0 −1 0 Last normalize 1 0 𝑁= 0 0 𝑧max + 𝑧min 0 0 0 𝑠𝑒𝑡 𝛼 = 𝑧max − 𝑧𝑚𝑖𝑛 1 0 0 ; 2𝑧max . 𝑧min 0 𝛼 𝛽 𝑎𝑛𝑑 𝛽 = − 0 −1 0 𝑧max − 𝑧𝑚𝑖𝑛 Result is projection matrix 2𝑧𝑚𝑖𝑛 𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛 𝑃 = 𝑁𝑆𝐻 = 0 0 2𝑧𝑚𝑖𝑛 𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛 0 0 0 0 𝑥𝑚𝑎𝑥 + 𝑥𝑚𝑖𝑛 𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛 𝑦𝑚𝑎𝑥 + 𝑦𝑚𝑖𝑛 𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛 𝑧𝑚𝑎𝑥 + 𝑧𝑚𝑖𝑛 − 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 −1 0 0 2𝑛𝑒𝑎𝑟 𝑟𝑖𝑔ℎ𝑡 − 𝑙𝑒𝑓𝑡 0 0 2𝑛𝑒𝑎𝑟 𝑡𝑜𝑝 − 𝑏𝑜𝑡𝑡𝑜𝑚 0 0 0 0 = 2𝑧𝑚𝑎𝑥 𝑧𝑚𝑖𝑛 𝑧𝑚𝑎𝑥 − 𝑧𝑚𝑖𝑛 0 suriyong l. 𝑟𝑖𝑔ℎ𝑡 + 𝑙𝑒𝑓𝑡 𝑟𝑖𝑔ℎ𝑡 − 𝑙𝑒𝑓𝑡 𝑡𝑜𝑝 + 𝑏𝑜𝑡𝑡𝑜𝑚 𝑡𝑜𝑝 − 𝑏𝑜𝑡𝑡𝑜𝑚 𝑓𝑎𝑟 + 𝑛𝑒𝑎𝑟 − 𝑓𝑎𝑟 − 𝑛𝑒𝑎𝑟 −1 0 0 2 𝑓𝑎𝑟. 𝑛𝑒𝑎𝑟 𝑓𝑎𝑟 − 𝑛𝑒𝑎𝑟 0 The Viewport The projection matrix defines the mapping from a 3D world co-ordinate to a 2D viewport co-ordinate. The viewport extents are defined as a parameter of the projection: (r,t,-n) glFrustrum(l,r,b,t,n,f) (l,b,-n) (w,h,-n) gluPerspective(f,a,n,f) h n tan (-w,-h,-n) 79 w ha f 2 The Viewport We need to associate the 2D viewport co-ordinate system with the window co-ordinate system in order to determine the correct pixel associated with each vertex. normalised device co-ordinates window co-ordinates 80 Viewport to Window Transformation An affine planar transformation is used. After projection to the view plane, all points are transformed to normalized device co-ordinates: [-1…+1, -1…+1] x p xmin 1 xn 2 x x max min y y yn 2 p min 1 ymax ymin glViewport used to relate the co-ordinate systems: glViewport(int x, int y, int width, int height); 81 Viewport to Window Transformation = location of bottom left of viewport within the window width,height = dimension in pixels of the viewport (x,y) width height xw xn 1 x y y 1 y w n 2 2 normally we re-create the window after a window resize event to ensure a correct mapping between viewport and window dimensions: static void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(85.0, 1.0, 5, 50); } 82 Aspect Ratio The aspect ratio defines the relationship between the width and height of an image. Using gluPerspective an viewport aspect ratio may be explicitly provided, otherwise the aspect ratio is a function of the supplied viewport width and height. The aspect ratio of the window (defined by the user) must match the viewport aspect ratio to prevent unwanted affine distortion: aspect ratio = 1.25 aspect ratio = 0.5 83 Sample Viewport Application // top left: top view glViewport(0, win_height/2, win_width/2, win_height/2); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0); gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(object); // top right: right view glViewport(win_width/2, win_height/2, win_width/2, win_height/2); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0); gluLookAt(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(object); // bottom left: front view glViewport(0, 0, win_width/2, win_height/2); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glCallList(object); // bottom right: rotating perspective view glViewport(win_width/2, 0, win_width/2, win_height/2); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70.0, 1.0, 1, 50); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(30.0, 1.0, 0.0, 0.0); glRotatef(Angle, 0.0, 1.0, 0.0); glCallList(object); 84 Shadow projection Not a geometric object Making visual looked realistic Require one more light sources A point is in shadow if it is not illuminated by any light source There are no shadow if light source is at the center of projection suriyong l. Assume the shadow fall on the ground -> y=0 Move the light source to the origin Shadow from a single polygon Shadow polygon projection (a) from a light source, and (b) with source moved to the origin. suriyong l. • First light source at (xl , yl , zl) • Translate to the origin matrix T = (-xl , -yl , -zl) • Next perspective projection matrix M 1 0 0 0 0 1 0 1 yl 0 0 1 0 0 0 0 0 • Last translate back to ( xl, yl, zl ) T = (xl , yl , zl) The result equations are: x p xl x xl ( y yl ) / yl yp 0 z p zl z zl ( y yl ) / yl suriyong l. void display(void) { GLfloat xl =0, yl=20, zl=0; // light position GLfloat m[16]; for (int i=0;i<16;i++) m[i]=0.0; // set perspective projection matrix m[0]=m[5] = m[10]=1.0; m[7]=-1.0/light[1]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(1, 1, 1,0.0,0.0,0.0,0.0,1.0,0.0); glTranslatef(0.0, 3.0, 0.0); glColorPointer(3,GL_FLOAT, 0, colors); // draw cube glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); glPushMatrix(); // save state glTranslatef(xl, yl, zl); // translate light back glMultMatrixf(m); // perspective projection glTranslatef(-xl,-yl, -zl); // translate light glColor3f(shadow_color); glColorPointer(3,GL_FLOAT, 0, bcolors); // draw shadow glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); glPopMatrix(); glutSwapBuffers(); // restore state } suriyong l. Exercises 1. 2. 3. 4. 5. 6. 7. Not all projections are planar geometric projections. Give an example of a projection in which the projection surface is not a plane, and another in which the projectors are not lines. Consider an airplane whose position is specified by the roll, pitch, and yaw, and by the distance from an object. Find a model-view matrix in terms of these parameters. Consider a satellite rotating around the earth. Its position above the earth is specified in polar coordinates. Find a model-view matrix that keeps the viewer looking at the earth. Such a matrix could be used to show the earth as it rotates. Show how to compute u and v directions from the VPN, VRP, and VUP using only cross products. Can we obtain an isometric of the cube by a single rotation about a suitably chosen axis? Explain your answer. Derive the perspective-projection matrix when the COP can be at any point and the projection plane can be at any orientation. Show that perspective projection preserves lines. suriyong l. 8. 9. 10. 11. 12. 13. 14. 15. Any attempt to take the projection of a point in the same plane as the COP will lead to a division by zero. What is the projection of a line segment that has endpoints on either side of the projection plane? Define one or more APIs to specify oblique projections. You do not need write the functions; just decide which parameters the user must specify. Derive an oblique-projection matrix from specification of front and back clipping planes, and top-right and bottom-left intersections of the sides of the clipping volume with the front clipping plane. Our approach of normalizing all projections seems to imply that we could predistorr all objects and support only orthographic projections. Explain any problems we would face if we took this approach to building a graphics system. How do the OpenGL projection matrices change if the COP is not at the origin? Assume that the COP is at (0,0, d) and the projection plane is z = 0. We can create an interesting class of three-dimensional objects by extending twodimensional objects into the third dimension by extrusion. For example, a circle becomes a cylinder, a line becomes a quaririlateral, and a quadrilateral in the plane becomes a parallelepiped. Use this technique to convert the two-dimensional maze from Exercise 2.8 to a three-dimensional maze. Extend the maze program of Exercise 5.13 to allow the user to walk through the maze. A click on the middle mouse button should move the user forward; a click on the right or left button should turn the user 90 degrees to the right or left, respectively. If we were to use orthogonal projections to draw the coordinate axes, the x- and v-axes would lie in the plane of the paper, but the z-axis would point out of the page. Instead, we can draw the x- and y-axes as meeting at a 90-degree angle, with the z-axis going off at — 135 degrees from the x-axis. Find the matrix that projects the original orthogonalcoordinate axes to this view. suriyong l. 16. 17. 18. 19. 20. Write a program to display a rotating cube in a box with three light sources. Each light source should project the cube onto one of the three visible sides of the box. Find the projection of a point onto the plane ax + by + cz + d = 0 from a light source located at infinity in the direction (dx,dy, dz). Using one of the three-dimensional interfaces discussed in Chapter 4, write a program to move the camera through a scene composed of simple objects. In animation, often we can save work by working with two-dimensional patterns that are mapped onto flat polygons that arc always parallel to the camera. Write a program that will keep a simple polygon facing the camera as the camera moves. Stereo images arc produced by creating two images with the viewer in two slightly different positions. Consider a viewer who is at the origin but whose eyes are separated by ∆x units. What are the appropriate viewing specifications to create the two images? suriyong l. References Angel, Edward, Interactive Computer Graphics A Top-Down Approach Using OpenGL 3rd Edition, Addison Wesley, ISBN 0-321-19044-0 Hearn, Donald and Baker, M. Pauline, Computer Graphics with OpenGL 3rd edition, Pearson, Prentice Hall, Inc., ISBN 0-13-015390-7 Steven Collins, Viewing system and OpenGL, lecture slide 4BA6 - Topic 4, internet material. suriyong l.