Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed...

52
Viewing Viewing Chapter 5 Chapter 5

Transcript of Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed...

Page 1: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

ViewingViewing

Chapter 5Chapter 5

Page 2: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 2Chapter 5 -- Viewing

Introduction:Introduction: We have completed our discussion of the We have completed our discussion of the

first half of the synthetic camera model first half of the synthetic camera model specifying objects in three dimensionsspecifying objects in three dimensions

We now investigate the multitude of We now investigate the multitude of ways in which we can describe our ways in which we can describe our virtual camera.virtual camera.

First, we look at the types of views we can First, we look at the types of views we can create, and why we need more than one type create, and why we need more than one type of view.of view.

Then we examine how an application viewer Then we examine how an application viewer can create a particular view in within can create a particular view in within OpenGL.OpenGL.

Page 3: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 3Chapter 5 -- Viewing

1. Classical and 1. Classical and Computer ViewingComputer Viewing

Before looking at the interface between Before looking at the interface between computer-graphics systems and application computer-graphics systems and application programmers for 3D viewing, we take a programmers for 3D viewing, we take a slight diversion to consider classical slight diversion to consider classical viewing.viewing.

There are two reasons to do thisThere are two reasons to do this First, many jobs that were formerly done by hand First, many jobs that were formerly done by hand

drawing - such as animation in movies, architectural drawing - such as animation in movies, architectural rendering, .. Are now done routinely with the aid of rendering, .. Are now done routinely with the aid of compute graphics.compute graphics.

Practitioners of these fields need to produce Practitioners of these fields need to produce classical views.classical views.

Page 4: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 4Chapter 5 -- Viewing

Second, the relationship between classical and computer Second, the relationship between classical and computer viewing shows many advantages of, and a few difficulties viewing shows many advantages of, and a few difficulties with, the approach used by most APIs.with, the approach used by most APIs.

When we introduced the synthetic camera When we introduced the synthetic camera model in Chapter 1, we covered some model in Chapter 1, we covered some elements:elements:

objects, viewers, projectors, and a projection planeobjects, viewers, projectors, and a projection plane

The projectors meet at the center of projection The projectors meet at the center of projection (COP)(COP)

this corresponds to the center of the lens in the camera, this corresponds to the center of the lens in the camera, or in the eyeor in the eye

Page 5: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 5Chapter 5 -- Viewing

Both classical and computer graphics Both classical and computer graphics allow the viewer to be an infinite allow the viewer to be an infinite distance from the objectsdistance from the objects

Note, as we move the COP to infinity, the Note, as we move the COP to infinity, the projectors become parallel and the COP projectors become parallel and the COP can be replaced with a direction of can be replaced with a direction of projection (DOP)projection (DOP)

Page 6: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 6Chapter 5 -- Viewing

Although computer graphics systems Although computer graphics systems have two fundamental types of viewing have two fundamental types of viewing

(parallel and perspective), (parallel and perspective), classical graphics appears to permit a classical graphics appears to permit a

host of different views ranging from:host of different views ranging from: multiview orthographic projections, one- two- multiview orthographic projections, one- two-

and three-point perspectivesand three-point perspectives

This seeming discrepancy arises becauseThis seeming discrepancy arises because in classical graphics due to the desire to show in classical graphics due to the desire to show

a specific relationship among an object, the a specific relationship among an object, the viewer, and the projection plane viewer, and the projection plane

as opposed to the computer graphics as opposed to the computer graphics approach of complete independence of all approach of complete independence of all specificationsspecifications

Page 7: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 7Chapter 5 -- Viewing

1.1 Classical Viewing1.1 Classical Viewing When an architect draws an image of When an architect draws an image of

a building,a building, they know which sides they wish to they know which sides they wish to

display, display, and thus where they should place the and thus where they should place the

viewerviewer

Each classical view is determined by Each classical view is determined by a specific relationship between the a specific relationship between the objects and the viewer.objects and the viewer.

Page 8: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 8Chapter 5 -- Viewing

1.2 Orthographic Projections1.2 Orthographic Projections The classical Orthographic projectionThe classical Orthographic projection

Multiview Orthographic projectionsMultiview Orthographic projections

Page 9: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 9Chapter 5 -- Viewing

1.3 Axonometric Projections1.3 Axonometric Projections If we allow the projection plane to be If we allow the projection plane to be

at any angle (not just parallel with a at any angle (not just parallel with a face of the object) we end up with an face of the object) we end up with an axonometric view.axonometric view.

Some special cases:Some special cases:

Page 10: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 10Chapter 5 -- Viewing

1.4 Oblique Projections1.4 Oblique Projections These are the most general parallel These are the most general parallel

viewsviews projectors can make an arbitrary angle projectors can make an arbitrary angle

with the projection plane.with the projection plane.

Page 11: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 11Chapter 5 -- Viewing

1.5 Perspective Viewing1.5 Perspective Viewing All perspective views are All perspective views are

characterized by diminution of size. characterized by diminution of size. (the farther away, the smaller they are)(the farther away, the smaller they are)

Page 12: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 12Chapter 5 -- Viewing

2. Viewing with a 2. Viewing with a ComputerComputer

We can now return to 3D graphics from a We can now return to 3D graphics from a computer perspectivecomputer perspective We have the choice in OpenGL of a We have the choice in OpenGL of a

perspective camera or an orthogonal one.perspective camera or an orthogonal one. In terms of the pipeline architecture, viewing In terms of the pipeline architecture, viewing

consists of two fundamental operations:consists of two fundamental operations: First we must position the cameraFirst we must position the camera The second is the application of the projection The second is the application of the projection

transformationtransformation

But let’s review the default cameraBut let’s review the default camera

Page 13: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 13Chapter 5 -- Viewing

OpenGL starts with the camera OpenGL starts with the camera at the origin, pointing in the negative z at the origin, pointing in the negative z

direction.direction. set up for orthogonal viesset up for orthogonal vies a viewing volume that is a cube a viewing volume that is a cube

centered at the origin, with sides length 2centered at the origin, with sides length 2

clipped out

z=0

Page 14: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 14Chapter 5 -- Viewing

3. Positioning of the 3. Positioning of the CameraCamera

We now examine the API that OpenGL We now examine the API that OpenGL provides for three-dimensional provides for three-dimensional graphics, and show how other APIs graphics, and show how other APIs differdiffer

In this section we deal with positioning In this section we deal with positioning the camera.the camera.

In Section 5.4 we discuss how we In Section 5.4 we discuss how we specify the desired projection.specify the desired projection.

Page 15: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 15Chapter 5 -- Viewing

In OpenGL, the model-view and In OpenGL, the model-view and projection matrices are concatenated projection matrices are concatenated together to form the matrix that together to form the matrix that applies to geometric entities such as applies to geometric entities such as vertices.vertices.

We have seen how to use the model-view We have seen how to use the model-view matrixmatrix

to position objects in space.to position objects in space.

The other is to convert from the reference The other is to convert from the reference frame used for modeling to the frame of frame used for modeling to the frame of the camerathe camera

Page 16: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 16Chapter 5 -- Viewing

3.1 Positioning of the Camera Frame3.1 Positioning of the Camera Frame If we want to visualize object with both positive If we want to visualize object with both positive

and negative z values we can eitherand negative z values we can either Move the camera in the positive z directionMove the camera in the positive z direction

Translate the camera frameTranslate the camera frame Move the objects in the negative z directionMove the objects in the negative z direction

Translate the world frameTranslate the world frame Both of these views are equivalent and are Both of these views are equivalent and are

determined by the model-view matrixdetermined by the model-view matrix Want a translation (Want a translation (glTranslatef(0.0,0.0,-d);glTranslatef(0.0,0.0,-d);)) WithWith d > 0d > 0

Page 17: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 17Chapter 5 -- Viewing

Default Frames: initially the camera Default Frames: initially the camera is at the originis at the origin

Frames after translation of -d (d>0)Frames after translation of -d (d>0)

Page 18: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 18Chapter 5 -- Viewing

At any given time, the state of the model-At any given time, the state of the model-view matrix encapsulates the relation view matrix encapsulates the relation between the camera frame and the world between the camera frame and the world frame.frame.

Although combining the modeling and Although combining the modeling and viewing transformations into a single viewing transformations into a single matrix may initially cause confusion, on matrix may initially cause confusion, on closer examination this approach is a good closer examination this approach is a good one.one.

The obvious next problems are how we The obvious next problems are how we specify the desired position of the camera specify the desired position of the camera and how we implement camera positioning and how we implement camera positioning in OpenGLin OpenGL

Page 19: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 19Chapter 5 -- Viewing

Here, we find it convenient to think in Here, we find it convenient to think in terms of moving the default camera terms of moving the default camera relative to the world frame.relative to the world frame.

We will outline three approaches to this.We will outline three approaches to this. One is given in this section, and two others in One is given in this section, and two others in

5.3.2 (two more are exercises at the end of the 5.3.2 (two more are exercises at the end of the chapter)chapter)

The First Approach:The First Approach: Specify the position indirectly by applying a Specify the position indirectly by applying a

sequence of rotations and translations to the sequence of rotations and translations to the model-view matrixmodel-view matrix

This is a direct application of the instance This is a direct application of the instance transformations we presented in Chapter 4transformations we presented in Chapter 4

Page 20: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 20Chapter 5 -- Viewing

We must be careful for two reasons:We must be careful for two reasons: First, we usually want to define the camera First, we usually want to define the camera

beforebefore we position the objects in the scene. we position the objects in the scene. Second, transformations on the camera may Second, transformations on the camera may

appear to be backward from what we might appear to be backward from what we might expect.expect.

Page 21: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 21Chapter 5 -- Viewing

Here is how to do a side view,Here is how to do a side view, Rotate the cameraRotate the camera Move it away from originMove it away from origin Model-view matrix C = TRModel-view matrix C = TR

Page 22: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 22Chapter 5 -- Viewing

OpenGL CodeOpenGL Code Remember that last transformation Remember that last transformation

specified is first to be appliedspecified is first to be applied

glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW); glLoadIdentity( );glLoadIdentity( ); glTranslatef(0.0, 0.0, -d);glTranslatef(0.0, 0.0, -d); glRotatef(-90.0, 0.0, 1.0, 0.0)glRotatef(-90.0, 0.0, 1.0, 0.0)

Page 23: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 23Chapter 5 -- Viewing

3.2 Two Viewing APIs3.2 Two Viewing APIs We can take a different approach to We can take a different approach to

positioning the camera an approach positioning the camera an approach used by PHIGS, and GKS-3D (two of used by PHIGS, and GKS-3D (two of the original standards in 3D)the original standards in 3D) We describe the camera’s position and We describe the camera’s position and

orientation in the world frameorientation in the world frame It’s desired location is centered at the view-It’s desired location is centered at the view-

reference point (VRP)reference point (VRP) It’s orientation is specified with the view-It’s orientation is specified with the view-

plane normal (VPN) and the view-up vector plane normal (VPN) and the view-up vector (VUP)(VUP)

Page 24: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 24Chapter 5 -- Viewing

2.3 The Look-At Function2.3 The Look-At Function The use of the VRP, VPN, and VUP is The use of the VRP, VPN, and VUP is

but one way to provide an API for but one way to provide an API for specifying the position of a camera.specifying the position of a camera.

In many situations, a more direct method is In many situations, a more direct method is appropriate.appropriate.

gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);upy, upz);

Page 25: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 25Chapter 5 -- Viewing

2.4 Other Viewing APIs2.4 Other Viewing APIs In many applications, neither of the In many applications, neither of the

viewing interfaces that we have viewing interfaces that we have presented is appropriate.presented is appropriate.

Consider a flight simulator:Consider a flight simulator: The pilot worries about roll, pitch, and yawThe pilot worries about roll, pitch, and yaw

Page 26: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 26Chapter 5 -- Viewing

Viewing in many applications is most Viewing in many applications is most naturally specified in polar naturally specified in polar coordinates -- rather than rectilinear coordinates -- rather than rectilinear coordinates. coordinates.

Applications involving objects that rotate Applications involving objects that rotate about other objects fit this category.about other objects fit this category.

Page 27: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 27Chapter 5 -- Viewing

4. Simple Projections4. Simple Projections

With a real camera, once we position it, we With a real camera, once we position it, we still must select a lens.still must select a lens. In computer graphics we select the type of lens In computer graphics we select the type of lens

and the size of the film by selecting the type of and the size of the film by selecting the type of projection and the viewing parameters.projection and the viewing parameters.

Most APIs distinguish between parallel and Most APIs distinguish between parallel and perspective views by providing different perspective views by providing different functions for the two cases.functions for the two cases. In OpenGL we can set the projection matrix with In OpenGL we can set the projection matrix with

a glLoadMatrix function, or we can other a glLoadMatrix function, or we can other functions for the most common viewing functions for the most common viewing conditionsconditions

Page 28: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 28Chapter 5 -- Viewing

4.1 Perspective Projections4.1 Perspective Projections Suppose that we are in the camera Suppose that we are in the camera

frame with the camera located at the frame with the camera located at the origin pointed in the negative z origin pointed in the negative z direction.direction.

Page 29: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 29Chapter 5 -- Viewing

As we saw in Chapter 2, we can place As we saw in Chapter 2, we can place the projection plane in front of the the projection plane in front of the center of projection. center of projection. Center of projection at the originCenter of projection at the origin Projection plane Projection plane zz = = dd, , dd < 0 < 0

Page 30: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 30Chapter 5 -- Viewing

Consider top and side viewsConsider top and side views

xp = x/(z/d) yp = y/(z/d) zp = d

Page 31: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 31Chapter 5 -- Viewing

Although the perspective transformation preserves Although the perspective transformation preserves lines, it is not Affinelines, it is not Affine

It is also irreversible (because all points along a It is also irreversible (because all points along a projector project to the same point)projector project to the same point)

We can, however, modify slightly our use of We can, however, modify slightly our use of homogenous coordinates to handle projections.homogenous coordinates to handle projections.

Page 32: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 32Chapter 5 -- Viewing

4.2 Orthogonal Projections4.2 Orthogonal Projections Orthogonal or orthographic Orthogonal or orthographic

projections are a special case of projections are a special case of parallel projections, in which the parallel projections, in which the projectors are perpendicular to the projectors are perpendicular to the view of the plane.view of the plane.

Page 33: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 33Chapter 5 -- Viewing

4. Projections in OpenGL4. Projections in OpenGL

The projections we just developed did The projections we just developed did not take into account the properties of not take into account the properties of the camera:the camera: the focal length of its lens,the focal length of its lens, the size of the film planethe size of the film plane

View VolumeView Volume

Page 34: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 34Chapter 5 -- Viewing

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

The resulting view volume is a The resulting view volume is a frustum -- which is a truncated frustum -- which is a truncated pyramid.pyramid.

Page 35: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 35Chapter 5 -- Viewing

4.1 Perspectives in OpenGL4.1 Perspectives in OpenGL In OpenGL we have two functions for In OpenGL we have two functions for

specifying perspective views and one specifying perspective views and one for specifying parallel views.for specifying parallel views.

We can specify our camera view by:We can specify our camera view by: glFrustrum(xmin, xmax, ymin, ymax, glFrustrum(xmin, xmax, ymin, ymax,

near, far)near, far)

Page 36: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 36Chapter 5 -- Viewing

Because the projection matrix Because the projection matrix determined by these specifications determined by these specifications multiplies the present matrix, we multiplies the present matrix, we must first select the matrix mode. must first select the matrix mode.

A typical sequence isA typical sequence is glMatrixMode(GL_PROJECTION);glMatrixMode(GL_PROJECTION); glLoadIdentity( );glLoadIdentity( ); glFrustrum(xmin, xmax, ymin, ymax, near, glFrustrum(xmin, xmax, ymin, ymax, near,

far);far);

Page 37: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 37Chapter 5 -- Viewing

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

gluPerspective(fovy, aspect, near, far);gluPerspective(fovy, aspect, near, far);

aspect = w/h

front plane

Page 38: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 38Chapter 5 -- Viewing

4.2 Parallel Viewing in OpenGL4.2 Parallel Viewing in OpenGL The only parallel-viewing function The only parallel-viewing function

provided by OpenGL is the provided by OpenGL is the orthographic viewing functionorthographic viewing function glOrtho(xmin, xmax, ymin, ymax, near, far)glOrtho(xmin, xmax, ymin, ymax, near, far)

near and far measured from camera

Page 39: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 39Chapter 5 -- Viewing

5. Hidden-Surface 5. Hidden-Surface RemovalRemoval

Hidden surface removal algorithms can Hidden surface removal algorithms can be divided into two broad classes:be divided into two broad classes: Object-space algorithmsObject-space algorithms

attempt to order the surfaces of the objects in attempt to order the surfaces of the objects in the scene such that drawing surfaces in a the scene such that drawing surfaces in a particular order provides the correct image.particular order provides the correct image.

Image-space algorithmsImage-space algorithms work as part of the projection process and seek work as part of the projection process and seek

to determine the relationship among object to determine the relationship among object points on each projectorpoints on each projector

z-buffer fits into this category.z-buffer fits into this category.

Page 40: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 40Chapter 5 -- Viewing

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

proportional to the number of polygons.proportional to the number of polygons. It can be implemented with a small It can be implemented with a small

number of additional calculations over number of additional calculations over what we have to do anyway.what we have to do anyway.

Typically you use these functions:Typically you use these functions: glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |

GLUT_DEPTH);GLUT_DEPTH); glEnable(GL_DEPTH_TEST);glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT);glClear(GL_DEPTH_BUFFER_BIT);

Page 41: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 41Chapter 5 -- Viewing

6.1 Culling6.1 Culling For a convex object, such as the cube, we For a convex object, such as the cube, we

could simply remove all the faces pointing could simply remove all the faces pointing away from the viewer, and render only the away from the viewer, and render only the ones facing the viewer. (we will look at this ones facing the viewer. (we will look at this mroe in Chapter 8)mroe in Chapter 8)

In OpenGL we turn on culling by simply In OpenGL we turn on culling by simply enabling itenabling it

glEnable(GL_CULL);glEnable(GL_CULL);

However, culling works only if we have a However, culling works only if we have a convex object.convex object.

Often we can use culling in addition to the z-buffer Often we can use culling in addition to the z-buffer algoritmalgoritm

Page 42: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 42Chapter 5 -- Viewing

7. Walking Through a 7. Walking Through a SceneScene

Let us modify the version of our color-Let us modify the version of our color-cube program from Chapter 4 cube program from Chapter 4 Old Version:Old Version:

the cube rotated about the origin.the cube rotated about the origin. Orthographic projectionOrthographic projection

In this versionIn this version perspective projectionperspective projection allow the camera to move.allow the camera to move.

Page 43: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 43Chapter 5 -- Viewing

void keys(unsigned char key, int x, int y)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 == ‘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 == ‘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;

if(key == ‘Z’) viewer[2] += 1.0;if(key == ‘Z’) viewer[2] += 1.0;

}}

Page 44: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 44Chapter 5 -- Viewing

void display(void)void display(void)

{{

glClear(GL_COLOR_BUFFER_BIT | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);GL_DEPTH_BUFFER_BIT);

glLoadIdentity( );glLoadIdentity( );

gluLookAt(viewer[0], viewer[1], viewer[2], gluLookAt(viewer[0], viewer[1], viewer[2],

0.0, 0.0, 0.0, 0.0, 1.0 , 0.0);0.0, 0.0, 0.0, 0.0, 1.0 , 0.0);

glRotatef(theta[0], 1.0, 0.0, 0.0);glRotatef(theta[0], 1.0, 0.0, 0.0);

glRotatef(theta[1], 0.0, 1.0, 0.0);glRotatef(theta[1], 0.0, 1.0, 0.0);

glRotatef(theta[2], 0.0, 0.0, 1.0);glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube( );colorcube( );

glFlush( );glFlush( );

glutSwapBuffers( );glutSwapBuffers( );

}}

Page 45: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 45Chapter 5 -- Viewing

void myReshape(int w, int h)void myReshape(int w, int h)

{{

glViewport(0,0,w,h);glViewport(0,0,w,h);

glLoadMatrix(GL_PROJECTION);glLoadMatrix(GL_PROJECTION);

glLoadIdentity( );glLoadIdentity( );

if(w<=h)if(w<=h)

glFrustrum(-2.0, 2.0, -2.0 * (Glfloat)h/(Glfloat)w,glFrustrum(-2.0, 2.0, -2.0 * (Glfloat)h/(Glfloat)w,

2.0*(Glfloat(h)/(Glfloat)w, 2.0, 20.0);2.0*(Glfloat(h)/(Glfloat)w, 2.0, 20.0);

elseelse

glFrustrum(-2.0, 2.0, -2.0 * (Glfloat)w/(Glfloat)h,glFrustrum(-2.0, 2.0, -2.0 * (Glfloat)w/(Glfloat)h,

2.0*(Glfloat)w/(Glfloat)h, 2.0, 20.0);2.0*(Glfloat)w/(Glfloat)h, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW);

}}

Page 46: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 46Chapter 5 -- Viewing

8. Parallel-Projection 8. Parallel-Projection MatricesMatrices

Page 47: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 47Chapter 5 -- Viewing

9. Perspective-Projection 9. Perspective-Projection MatricesMatrices

Page 48: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 48Chapter 5 -- Viewing

10. Projections and 10. Projections and ShadowsShadows

The creation of simple shadows is an The creation of simple shadows is an interesting application of projection interesting application of projection matrices.matrices.

This section covers how to re-project This section covers how to re-project the polygon casting the shadow onto the polygon casting the shadow onto the groundthe ground this re-projection is called a shadow this re-projection is called a shadow

polygon.polygon.

Page 49: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 49Chapter 5 -- Viewing

For a simple environment, this For a simple environment, this technique works well, however, when technique works well, however, when objects cast shadows on other objects cast shadows on other objects, this method becomes objects, this method becomes impractical.impractical.

In Chapter 9 we address a more In Chapter 9 we address a more general shadow-creation method that general shadow-creation method that requires more work.requires more work.

Page 50: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 50Chapter 5 -- Viewing

11. Summary11. Summary

We have come a long way.We have come a long way. We can now write complete, nontrivial, We can now write complete, nontrivial,

three dimensional applications.three dimensional applications. Probably the most instructive activity that Probably the most instructive activity that

you can do now is to write such an you can do now is to write such an application.application.

In Chapter 6 we consider the In Chapter 6 we consider the interaction of light with the materials interaction of light with the materials that characterize our objects.that characterize our objects.

Page 51: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 51Chapter 5 -- Viewing

11. Suggested Readings11. Suggested Readings Foley (90), Watt (93) and Hern&Baker (94) Foley (90), Watt (93) and Hern&Baker (94)

derive canonical projection transformationsderive canonical projection transformations All follow the PHIGS orientation, so the API is All follow the PHIGS orientation, so the API is

slightly different from the one used here.slightly different from the one used here. Most differ in whether they use column or Most differ in whether they use column or

row matrices, in where the COP is located, row matrices, in where the COP is located, and in whether the projection is in the and in whether the projection is in the positive or negative z direction.positive or negative z direction.

See theSee the OpenGL Programmer’s Guide OpenGL Programmer’s Guide (97) (97) for further discussion of the use of the for further discussion of the use of the model-view and projection matrices model-view and projection matrices

Page 52: Viewing Chapter 5. CS 480/680 2Chapter 5 -- Viewing Introduction: Introduction: We have completed our discussion of the first half of the synthetic camera.

CS 480/680 52Chapter 5 -- Viewing

Exercises -- Due next Exercises -- Due next classclass