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. 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. 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. Types of orthographic view Oblique view (planar face view) Axonometric view suriyong l. 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 Viewing with a computer 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(); glTranslatef(0, 0, -d); // Translate far from origin with distance d glRotatef(90.0, 0.0, 1.0, 0.0); // move camera to x axis (rotate around +y) 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 = Ry(45) .Rx(-35.26). T(-dz) 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 u=vxn 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 Move the camera to the VRP by translation matrix let T (-x, -y, -z) suriyong l. Determination of the view-up vector. Handling rotation To finding v let v = n vup set 1 then (don't care the length of vup factor at first) v n vup and n.vup ; n.v = 0 n.n v is the proportion of vup and v vup vup .n n n.n From v and n we can find u from u = v× n let u', v' and n' are normalized of u, v and n we can create rotation matrix M u x u y M u z 0 vx nx vy ny vz nz 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 u x v V = RT x nx 0 u y vy ny u z vz nz 0 0 xu x yu y zu z xvx yvy zvz xnx yny znz 1 Note: Translation matrix is on the right, first on the left second derivation, 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 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); // define objects here suriyong l. 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 x, y, z 2. Projection plane is perpendicular to –z axis and located at z = d 3. Project that point to projection plane, let’s say xp, yp, zp 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 closer of object, the size is much more reduce suriyong l. Using homogeneous coordinate wx wy p wz w we need w 1 x y p z 1 Perspective projection transformation q Mp 1 0 0 0 0 0 1 0 0 1 0 1/ d 0 x 0 y 0 z 0 1 yp Divided by 4th component Then xp x z/d y z/d zp z d z/d x z/d x p y yp q z/d zp d 1 1 Projection pipeline. suriyong l. = q 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 function for specifying perspective view glFrustum(xmin, xmax, ymin, ymax, near, far); Note: near, far are +ve, measured from COP (at z = 0) glFrustum(left, right, bottom, top, near, far); Specification of a frustum suriyong l. 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 fovy: 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. 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 algorithm 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 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 x= 1 y = 1 z = 1 If clipping volume is defined by 6 plane Called (canonical view volume) Use function call xp x yp y zp 0 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 Oblique Projection Oblique projection Oblique clipping volume tan Oblique projection. (a) Top view. (b) Side view suriyong l. z x xp and thus x p x - z cot Like wise y p y - z cot zp 0 1 P 0 0 0 0 cot 1 cot 0 0 0 0 0 0 0 1 Breaking to 1 P MorthH( , ) 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 cot 1 cot 0 0 0 0 0 0 0 1 Orthogonal projection and shearing matrix Conclusion: oblique is done by shearing and projection For 45 degree 1 P(45,45) MorthH(45,45) 0 0 0 Effect of shear transformation suriyong l. 0 1 0 0 1 1 0 0 0 0 0 1 Last Scaling and transformation 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 90 O Simple projective projection suriyong l. Normalized oblique projection Former perspective projection matrix at z = -1 1 0 M 0 0 With 90O 0 0 1 0 0 1 0 -1 0 0 0 0 Apply N to homogeneous coordinate q = Np x y z w angle of view we get x = z y z Introduce matrix N transformation matrix N x x y y z z w z suriyong l. x y z 1 Devided by w ', thus x x z y y z z z Apply an orthographic projection along the z-axis to N M orth N 0 0 which is a simple perspective-projection matrix, and the projection of the arbitrary point p is x y P M orth Np 0 z After we do the perspective division, we obtain the desired values the x p and y p : x z y yp z xp suriyong l. Set x = z to the planes z= 1 thus x 1 And y = z to y 1 The front of the view volume z = zmax is transformed to the plane z zmax Select zmax zmin zmax zmin 2 zmax .zmin zmax zmin 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. x xmax to skew (shear) the point min 2 The required shear matrix is ymax ymin , 2 , zmin to 0,0,zmin x xmax y ymin H( , ) H cot 1 min .cot 1 max 2 zmax 2 zmax The resulting frustum is described by the planes x xmin x max 2 zmax y ymax ymin 2 zmax z far zmin znear zmax The next step is to scale the sides of this frustum to x z y z suriyong l. Next scaling: 2 zmin x x min max S 0 0 2 zmin ymax ymin Last normalize 1 0 N 0 0 0 1 0 0 0 0 1 0 0 0 set ; and xmax xmin xmax xmin 2 zmin ymax ymin ymax ymin ymax ymin 2 zmax .zmin zmax zmin In term of near and far Result projection matrix 2 zmin x x max min P = NSH zmax zmin zmax zmin zmax zmin zmax zmin 2near right left 2 zmax zmin zmax zmin suriyong l. 2near top bottom right left right left top bottom top bottom far near far near 2 far.near far near 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 suriyong l. Suggest Reading Hearn, Donald and Baker, M. Pauline, Computer Graphics with OpenGL 3rd edition, Pearson, Prentice Hall, Inc., ISBN 0-13-015390-7 suriyong l.