Viewing Chapter 5 CS 480680 Chapter 5 Viewing

  • Slides: 52
Download presentation
Viewing Chapter 5 CS 480/680 Chapter 5 -- Viewing

Viewing Chapter 5 CS 480/680 Chapter 5 -- Viewing

n Introduction: n We have completed our discussion of the first half of the

n Introduction: n We have completed our discussion of the first half of the synthetic camera model n specifying objects in three dimensions n We now investigate the multitude of ways in which we can describe our virtual camera. n First, we look at the types of views we can create, and why we need more than one type of view. n Then we examine how an application viewer can create a particular view in within Open. GL. CS 480/680 Chapter 5 -- Viewing 2

1. Classical and Computer Viewing n Before looking at the interface between computergraphics systems

1. Classical and Computer Viewing n Before looking at the interface between computergraphics systems and application programmers for 3 D viewing, we take a slight diversion to consider classical viewing. n There are two reasons to do this n CS 480/680 First, many jobs that were formerly done by hand drawing such as animation in movies, architectural rendering, . . Are now done routinely with the aid of compute graphics. n Practitioners of these fields need to produce classical views. Chapter 5 -- Viewing 3

n n Second, the relationship between classical and computer viewing shows many advantages of,

n n Second, the relationship between classical and computer viewing shows many advantages of, and a few difficulties with, the approach used by most APIs. When we introduced the synthetic camera model in Chapter 1, we covered some elements: n objects, viewers, projectors, and a projection plane n The projectors meet at the center of projection (COP) n CS 480/680 this corresponds to the center of the lens in the camera, or Chapter 5 -- Viewing in the eye 4

n Both classical and computer graphics allow the viewer to be an infinite distance

n Both classical and computer graphics allow the viewer to be an infinite distance from the objects n CS 480/680 Note, as we move the COP to infinity, the projectors become parallel and the COP can be replaced with a direction of projection (DOP) Chapter 5 -- Viewing 5

n Although computer graphics systems have two fundamental types of viewing n (parallel and

n Although computer graphics systems have two fundamental types of viewing n (parallel and perspective), n classical graphics appears to permit a host of different views ranging from: n multiview orthographic projections, one- two- and threepoint perspectives n This seeming discrepancy arises because n n CS 480/680 in classical graphics due to the desire to show a specific relationship among an object, the viewer, and the projection plane as opposed to the computer graphics approach of complete independence of all specifications Chapter 5 -- Viewing 6

n 1. 1 Classical Viewing n When an architect draws an image of a

n 1. 1 Classical Viewing n When an architect draws an image of a building, n they know which sides they wish to display, n and thus where they should place the viewer n CS 480/680 Each classical view is determined by a specific relationship between the objects and the viewer. Chapter 5 -- Viewing 7

n 1. 2 Orthographic Projections n The classical Orthographic projection n Multiview Orthographic projections

n 1. 2 Orthographic Projections n The classical Orthographic projection n Multiview Orthographic projections CS 480/680 Chapter 5 -- Viewing 8

n 1. 3 Axonometric Projections n If we allow the projection plane to be

n 1. 3 Axonometric Projections n If we allow the projection plane to be at any angle (not just parallel with a face of the object) we end up with an axonometric view. n Some special cases: CS 480/680 Chapter 5 -- Viewing 9

n 1. 4 Oblique Projections n These are the most general parallel views n

n 1. 4 Oblique Projections n These are the most general parallel views n projectors can make an arbitrary angle with the projection plane. CS 480/680 Chapter 5 -- Viewing 10

n 1. 5 Perspective Viewing n All perspective views are characterized by diminution of

n 1. 5 Perspective Viewing n All perspective views are characterized by diminution of size. n (the farther away, the smaller they are) CS 480/680 Chapter 5 -- Viewing 11

2. Viewing with a Computer n We can now return to 3 D graphics

2. Viewing with a Computer n We can now return to 3 D graphics from a computer perspective n We have the choice in Open. GL of a perspective camera or an orthogonal one. n In terms of the pipeline architecture, viewing consists of two fundamental operations: n n First we must position the camera The second is the application of the projection transformation n But let’s review the default camera CS 480/680 Chapter 5 -- Viewing 12

n Open. GL starts with the camera n at the origin, pointing in the

n Open. GL starts with the camera n at the origin, pointing in the negative z direction. n set up for orthogonal vies n a viewing volume that is a cube n centered at the origin, with sides length 2 clipped out z=0 CS 480/680 Chapter 5 -- Viewing 13

3. Positioning of the Camera n We now examine the API that Open. GL

3. Positioning of the Camera n We now examine the API that Open. GL provides for three-dimensional graphics, and show other APIs differ In this section we deal with positioning the camera. n In Section 5. 4 we discuss how we specify the desired projection. n CS 480/680 Chapter 5 -- Viewing 14

n In Open. GL, the model-view and projection matrices are concatenated together to form

n In Open. GL, the model-view and projection matrices are concatenated together to form the matrix that applies to geometric entities such as vertices. n We have seen how to use the model-view matrix n to position objects in space. n The other is to convert from the reference frame used for modeling to the frame of the camera CS 480/680 Chapter 5 -- Viewing 15

n 3. 1 Positioning of the Camera Frame n If we want to visualize

n 3. 1 Positioning of the Camera Frame n If we want to visualize object with both positive and negative z values we can either n Move the camera in the positive z direction n Translate the camera frame n Move the objects in the negative z direction n Translate the world frame n Both of these views are equivalent and are determined by the model-view matrix n Want a translation (gl. Translatef(0. 0, -d); ) n With d > 0 CS 480/680 Chapter 5 -- Viewing 16

n Default Frames: initially the camera is at the origin n Frames after translation

n Default Frames: initially the camera is at the origin n Frames after translation of -d (d>0) CS 480/680 Chapter 5 -- Viewing 17

n At any given time, the state of the model-view matrix encapsulates the relation

n At any given time, the state of the model-view matrix encapsulates the relation between the camera frame and the world frame. n Although combining the modeling and viewing transformations into a single matrix may initially cause confusion, on closer examination this approach is a good one. n The obvious next problems are how we specify the desired position of the camera and how we implement camera positioning in Open. GL CS 480/680 Chapter 5 -- Viewing 18

Here, we find it convenient to think in terms of moving the default camera

Here, we find it convenient to think in terms of moving the default camera relative to the world frame. n We will outline three approaches to this. n n n One is given in this section, and two others in 5. 3. 2 (two more are exercises at the end of the chapter) The First Approach: n Specify the position indirectly by applying a sequence of rotations and translations to the modelview matrix n This is a direct application of the instance transformations we presented in Chapter 4 CS 480/680 Chapter 5 -- Viewing 19

n We must be careful for two reasons: n n CS 480/680 First, we

n We must be careful for two reasons: n n CS 480/680 First, we usually want to define the camera before we position the objects in the scene. Second, transformations on the camera may appear to be backward from what we might expect. Chapter 5 -- Viewing 20

n CS 480/680 Here is how to do a side view, n Rotate the

n CS 480/680 Here is how to do a side view, n Rotate the camera n Move it away from origin n Model-view matrix C = TR Chapter 5 -- Viewing 21

n Open. GL Code n Remember that last transformation specified is first to be

n Open. GL Code n Remember that last transformation specified is first to be applied n n CS 480/680 gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity( ); gl. Translatef(0. 0, -d); gl. Rotatef(-90. 0, 1. 0, 0. 0) Chapter 5 -- Viewing 22

n 3. 2 Two Viewing APIs n We can take a different approach to

n 3. 2 Two Viewing APIs n We can take a different approach to positioning the camera an approach used by PHIGS, and GKS-3 D (two of the original standards in 3 D) n We describe the camera’s position and orientation in the world frame n n CS 480/680 It’s desired location is centered at the view-reference point (VRP) It’s orientation is specified with the view-plane normal (VPN) and the view-up vector (VUP) Chapter 5 -- Viewing 23

n 2. 3 The Look-At Function n The use of the VRP, VPN, and

n 2. 3 The Look-At Function n The use of the VRP, VPN, and VUP is but one way to provide an API for specifying the position of a camera. n In many situations, a more direct method is appropriate. n CS 480/680 glu. Look. At(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); Chapter 5 -- Viewing 24

n 2. 4 Other Viewing APIs n In many applications, neither of the viewing

n 2. 4 Other Viewing APIs n In many applications, neither of the viewing interfaces that we have presented is appropriate. n Consider a flight simulator: n CS 480/680 The pilot worries about roll, pitch, and yaw Chapter 5 -- Viewing 25

n Viewing in many applications is most naturally specified in polar coordinates -- rather

n Viewing in many applications is most naturally specified in polar coordinates -- rather than rectilinear coordinates. n Applications involving objects that rotate about other objects fit this category. CS 480/680 Chapter 5 -- Viewing 26

4. Simple Projections n With a real camera, once we position it, we still

4. Simple Projections n With a real camera, once we position it, we still must select a lens. n In computer graphics we select the type of lens and the size of the film by selecting the type of projection and the viewing parameters. n Most APIs distinguish between parallel and perspective views by providing different functions for the two cases. n In Open. GL we can set the projection matrix with a gl. Load. Matrix function, or we can other functions for the most common viewing conditions CS 480/680 Chapter 5 -- Viewing 27

n 4. 1 Perspective Projections n CS 480/680 Suppose that we are in the

n 4. 1 Perspective Projections n CS 480/680 Suppose that we are in the camera frame with the camera located at the origin pointed in the negative z direction. Chapter 5 -- Viewing 28

n As we saw in Chapter 2, we can place the projection plane in

n As we saw in Chapter 2, we can place the projection plane in front of the center of projection. n Center of projection at the origin n Projection plane z = d, d < 0 CS 480/680 Chapter 5 -- Viewing 29

n Consider top and side views xp = x/(z/d) CS 480/680 yp = y/(z/d)

n Consider top and side views xp = x/(z/d) CS 480/680 yp = y/(z/d) Chapter 5 -- Viewing zp = d 30

n Although the perspective transformation preserves lines, it is not Affine n It is

n Although the perspective transformation preserves lines, it is not Affine n It is also irreversible (because all points along a projector project to the same point) n We can, however, modify slightly our use of homogenous coordinates to handle projections. CS 480/680 Chapter 5 -- Viewing 31

n 4. 2 Orthogonal Projections n CS 480/680 Orthogonal or orthographic projections are a

n 4. 2 Orthogonal Projections n CS 480/680 Orthogonal or orthographic projections are a special case of parallel projections, in which the projectors are perpendicular to the view of the plane. Chapter 5 -- Viewing 32

4. Projections in Open. GL n The projections we just developed did not take

4. Projections in Open. GL n The projections we just developed did not take into account the properties of the camera: n the focal length of its lens, n the size of the film plane n View Volume CS 480/680 Chapter 5 -- Viewing 33

Most graphics APIs define clipping parameters through the specification of a projection. n The

Most graphics APIs define clipping parameters through the specification of a projection. n The resulting view volume is a frustum -- which is a truncated pyramid. n CS 480/680 Chapter 5 -- Viewing 34

n 4. 1 Perspectives in Open. GL In Open. GL we have two functions

n 4. 1 Perspectives in Open. GL In Open. GL we have two functions for specifying perspective views and one for specifying parallel views. n We can specify our camera view by: n n gl. Frustrum(xmin, xmax, ymin, ymax, near, far) CS 480/680 Chapter 5 -- Viewing 35

n Because the projection matrix determined by these specifications multiplies the present matrix, we

n Because the projection matrix determined by these specifications multiplies the present matrix, we must first select the matrix mode. n A typical sequence is n n n CS 480/680 gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity( ); gl. Frustrum(xmin, xmax, ymin, ymax, near, far); Chapter 5 -- Viewing 36

n In many applications, it is natural to specify the angle or field of

n In many applications, it is natural to specify the angle or field of view n glu. Perspective(fovy, aspect, near, far); front plane aspect = w/h CS 480/680 Chapter 5 -- Viewing 37

n 4. 2 Parallel Viewing in Open. GL n The only parallel-viewing function provided

n 4. 2 Parallel Viewing in Open. GL n The only parallel-viewing function provided by Open. GL is the orthographic viewing function n gl. Ortho(xmin, near CS 480/680 xmax, ymin, ymax, near, far) and far measured from camera Chapter 5 -- Viewing 38

5. Hidden-Surface Removal n Hidden surface removal algorithms can be divided into two broad

5. Hidden-Surface Removal n Hidden surface removal algorithms can be divided into two broad classes: n Object-space algorithms n attempt to order the surfaces of the objects in the scene such that drawing surfaces in a particular order provides the correct image. n Image-space algorithms n n CS 480/680 work as part of the projection process and seek to determine the relationship among object points on each projector z-buffer fits into this category. Chapter 5 -- Viewing 39

n The major advantage of z-buffer is n that its worst case complexity is

n The major advantage of z-buffer is n that its worst case complexity is proportional to the number of polygons. n It can be implemented with a small number of additional calculations over what we have to do anyway. n Typically you use these functions: n n n CS 480/680 glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); gl. Enable(GL_DEPTH_TEST); gl. Clear(GL_DEPTH_BUFFER_BIT); Chapter 5 -- Viewing 40

n 6. 1 Culling n For a convex object, such as the cube, we

n 6. 1 Culling n For a convex object, such as the cube, we could simply remove all the faces pointing away from the viewer, and render only the ones facing the viewer. (we will look at this mroe in Chapter 8) n In Open. GL we turn on culling by simply enabling it n n However, culling works only if we have a convex object. n CS 480/680 gl. Enable(GL_CULL); Often we can use culling in addition to the z-buffer algoritm Chapter 5 -- Viewing 41

7. Walking Through a Scene n Let us modify the version of our color-cube

7. Walking Through a Scene n Let us modify the version of our color-cube program from Chapter 4 n Old Version: n n the cube rotated about the origin. Orthographic projection n In this version n n CS 480/680 perspective projection allow the camera to move. Chapter 5 -- Viewing 42

void keys(unsigned char key, int x, int y) { if(key == ‘x’) viewer[0] -=

void keys(unsigned char key, int x, int y) { if(key == ‘x’) viewer[0] -= 1. 0; if(key == ‘X’) 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; } CS 480/680 Chapter 5 -- Viewing 43

void display(void) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Load. Identity( ); glu. Look. At(viewer[0],

void display(void) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Load. Identity( ); glu. Look. At(viewer[0], viewer[1], viewer[2], 0. 0, 1. 0 , 0. 0); gl. Rotatef(theta[0], 1. 0, 0. 0); gl. Rotatef(theta[1], 0. 0, 1. 0, 0. 0); gl. Rotatef(theta[2], 0. 0, 1. 0); colorcube( ); gl. Flush( ); glut. Swap. Buffers( ); } CS 480/680 Chapter 5 -- Viewing 44

void my. Reshape(int w, int h) { gl. Viewport(0, 0, w, h); gl. Load.

void my. Reshape(int w, int h) { gl. Viewport(0, 0, w, h); gl. Load. Matrix(GL_PROJECTION); gl. Load. Identity( ); if(w<=h) gl. Frustrum(-2. 0, -2. 0 * (Glfloat)h/(Glfloat)w, 2. 0*(Glfloat(h)/(Glfloat)w, 2. 0, 20. 0); else gl. Frustrum(-2. 0, -2. 0 * (Glfloat)w/(Glfloat)h, 2. 0*(Glfloat)w/(Glfloat)h, 2. 0, 20. 0); gl. Matrix. Mode(GL_MODELVIEW); } CS 480/680 Chapter 5 -- Viewing 45

8. Parallel-Projection Matrices CS 480/680 Chapter 5 -- Viewing 46

8. Parallel-Projection Matrices CS 480/680 Chapter 5 -- Viewing 46

9. Perspective-Projection Matrices CS 480/680 Chapter 5 -- Viewing 47

9. Perspective-Projection Matrices CS 480/680 Chapter 5 -- Viewing 47

10. Projections and Shadows n The creation of simple shadows is an interesting application

10. Projections and Shadows n The creation of simple shadows is an interesting application of projection matrices. n This section covers how to re-project the polygon casting the shadow onto the ground n this re-projection is called a shadow polygon. CS 480/680 Chapter 5 -- Viewing 48

n For a simple environment, this technique works well, however, when objects cast shadows

n For a simple environment, this technique works well, however, when objects cast shadows on other objects, this method becomes impractical. n In Chapter 9 we address a more general shadow -creation method that requires more work. CS 480/680 Chapter 5 -- Viewing 49

11. Summary n We have come a long way. n We can now write

11. Summary n We have come a long way. n We can now write complete, nontrivial, three dimensional applications. n Probably the most instructive activity that you can do now is to write such an application. n In Chapter 6 we consider the interaction of light with the materials that characterize our objects. CS 480/680 Chapter 5 -- Viewing 50

11. Suggested Readings n Foley (90), Watt (93) and Hern&Baker (94) derive canonical projection

11. Suggested Readings n Foley (90), Watt (93) and Hern&Baker (94) derive canonical projection transformations n All follow the PHIGS orientation, so the API is slightly different from the one used here. n Most differ in whether they use column or row matrices, in where the COP is located, and in whether the projection is in the positive or negative z direction. n See the Open. GL Programmer’s Guide (97) for further discussion of the use of the model-view and projection matrices CS 480/680 Chapter 5 -- Viewing 51

Exercises -- Due next class CS 480/680 Chapter 5 -- Viewing 52

Exercises -- Due next class CS 480/680 Chapter 5 -- Viewing 52