1 Chapter 5 Viewing. 2 OpenGL Perspective Matrix parallel perspective axonometric multiview...

Post on 13-Jan-2016

254 views 4 download

transcript

1

Chapter 5

Viewing

2

OpenGL Perspective Matrix

parallel perspective

axonometric multivieworthographic

oblique

isometric dimetric trimetric

2 point1 point 3 point

planar geometric projections

3

Perspective Projection

4

Parallel Projection

5

Classical Viewing Viewing requires three basic elements

One or more objects A viewer with a projection surface Projectors that go from the object(s) to the projection surface

Classical views are based on the relationship among these elements The viewer picks up the object and orients it how she would like

to see it

Each object is assumed to constructed from flat principal faces Buildings, polyhedra, manufactured objects

6

Planar Geometric Projections Standard projections project onto a plane Projectors are lines that either

converge at a center of projection are parallel

Such projections preserve lines but not necessarily angles

7

Classical Projections

8

Orthographic Projection Projectors are orthogonal to projection surface

9

Multiview Orthographic Projection

Projection plane parallel to principal face Usually form front, top, side views

isometric (not multivieworthographic view)

in CAD and architecture, we often display three multiviews plus isometric

top

front

side

10

Advantages and Disadvantages Preserves both distances and angles

Shapes preserved Can be used for measurements

Building plansManuals

Cannot see what object really looks like because many surfaces hidden from view Often we add the isometric

11

Axonometric ProjectionsAllow projection plane to move relative to object

classify by how many angles ofa corner of a projected cube are the same

none: trimetrictwo: dimetricthree: isometric

1

32

12

Types of Axonometric Projections

13

Construction of Axonometric Projections

Projectors are still orthogonal to the projection plane, but Projection plane can have any orientation w.r.t. the object

14

Advantages and Disadvantages Lines are scaled (foreshortened) but can find scaling factors Lines preserved but angles are not

Projection of a circle in a plane not parallel to the projection plane is an ellipse

Can see three principal faces of a box-like object Some optical illusions possible

Parallel lines appear to diverge

Does not look real because far objects are scaled the same as near objects

Used in CAD applications

15

Oblique ProjectionArbitrary relationship between projectors and

projection plane

16

Advantages and Disadvantages Can pick the angles to emphasize a particular face In physical world, cannot create with simple

camera; possible with bellows camera or special lens (architectural)

17

Perspective Projection Projectors coverge at center of projection

18

Vanishing Points Parallel lines (not parallel to the projection plan)

on the object converge at a single point in the projection (the vanishing point)

Drawing simple perspectives by hand uses these vanishing point(s)

vanishing point

19

Three-Point Perspective No principal face parallel to projection plane Three vanishing points for cube

20

Two-Point Perspective On principal direction parallel to projection plane Two vanishing points for cube

21

One-Point Perspective One principal face parallel to projection plane One vanishing point for cube

22

Advantages and Disadvantes Objects further from viewer are projected smaller than t

he same sized objects closer to the viewer (diminuition) Looks realistic

Equal distances along a line are not projected into equal distances (nonuniform foreshortening)

Angles preserved only in planes parallel to the projection plane

More difficult to construct by hand than parallel projections (but not more difficult by computer)

23

Computer Viewing There are three aspects of the viewing process,

all of which are implemented in the pipeline, Positioning the camera

Setting the model-view matrix Selecting a lens

Setting the projection matrix Clipping

Setting the view volume

24

The OpenGL Camera In OpenGL, initially the world and camera

frames are the same Default model-view matrix is an identity

The camera is located at origin and points in the negative z direction

OpenGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin Default projection matrix is an identity

25

Default ProjectionDefault projection is orthogonal

clipped out

z=0

26

Moving the Camera Frame If we want to visualize object with both positive and negativ

e z values we can either Move the camera in the positive z direction

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

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

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

27

Moving Camera Back from Origin

default frames

frames after translation by –d d > 0

28

Moving the Camera (Object) We can move the camera to any desired

position by a sequence of rotations and translations

Example: positive x (side view of an object) Rotate the camera Move it away from origin Model-view matrix C = TR

29

OpenGL Code Remember that last transformation

specified is first to be applied

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

We are changing the modelview matrix C. A vertex specified at qthrough glVertex, after the change is still at q in the world frame, but is at Cq in the camera frame known internally to OpenGL.

30

Manually Generating an Isometric View – 1/3

Rotating about the y axis by 45o

Rotating about the x axis by 35.26o

Translating along the z direction by d

z

y

22

2 z

y

22 2

31

Manually Generating an Isometric View – 2/3

1000

3/33/33/3

06/63/66/6

02/202/2

1000

03/33/33/3

06/63/66/6

02/202/2

1000

100

0010

0001

1000

02/202/2

0010

02/202/2

1000

03/63/30

03/33/60

0001

1000

100

0010

0001

dd

dRTRM yx

32

Manually Generating an Isometric View – 3/3

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0.0, 0.0, -d);

glRotatef(35.26, 1.0, 0.0, 0.0);

glRotate(45.0, 0.0, 1.0, 0.0);

33

The LookAt Function – 1/2 The GLU library contains the function glLookAt to fr

om the required modelview matrix through a simple interface

Note the need for setting an up direction Still need to initialize

Can concatenate with modeling transformations Example: isometric view of cube aligned with axes

glMatrixMode(GL_MODELVIEW):glLoadIdentity();gluLookAt(-1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

34

The LookAt Function – 2/2glLookAt(eyex, eyey, eyez, atx, aty, atz,

upx, upy, upz)

35

Other Viewing APIs – 1/2 The LookAt function is only one possible

API for positioning the camera Others include

View reference point, view plane normal, view up (PHIGS, GKS-3D)

Yaw, pitch, roll Elevation, azimuth, twist Direction angles

36

Other Viewing APIs – 2/2

37

Simple Perspective Center of projection at the origin Projection plane z = d, d < 0

38

Perspective EquationsConsider top and side views

xp =dz

x

/yp =

dz

y

/zp = d

39

Homogeneous Coordinate Form

M =

0/100

0100

0010

0001

d

consider q = Mp where

1

z

y

x

dz

z

y

x

/

q = p =

40

Perspective Division However w 1, so we must divide by w to

return from homogeneous coordinates This perspective division yields

the desired perspective equations We will consider the corresponding clipping

volume with the OpenGL functions

xp =dz

x

/yp =

dz

y

/zp = d

41

Orthogonal Projections The default projection in the eye (camera) frame is

orthogonal For points within the default view volume

Most graphics systems use view normalization All other views are converted to the default view by

transformations that determine the projection matrix Allows use of the same pipeline for all views

xp = xyp = yzp = 0

42

Homogeneous Coordinate Representation

xp = xyp = yzp = 0wp = 1

pp = Mp

M =

1000

0000

0010

0001

In practice, we can let M = I and set the z term to zero later

43

Projections in OpenGL

44

OpenGL PerspectiveglFrustum(xmin,xmax,ymin,ymax,near,fa

r)

45

Using Field of View With glFrustum it is often difficult to get the d

esired view gluPerpective(fovy, aspect, near, fa

r) often provides a better interface

aspect = w/h

front plane

46

OpenGL Orthogonal ViewingglOrtho(xmin,xmax,ymin,ymax,near,far)

glOrtho(left,right,bottom,top,near,far)

near and far measured from camera

47

Hidden-Surface Removal

48

Z-Buffer Algorithm Keep track of the smallest depth or z value f

or each pixel Z value is initialized to the farthest distance Worst-case time complexity is O(n), where

n is the number of polygons glutInitDisplayMode(GLUT_DEPTH);

glEnable(GL_DEPTH_TEST);glClear(GL_DEPTH_BUFFER_BIT);

49

Culling Removing all the faces pointing away from

the viewer. For example, rendering n cubes with culling can filter 3n polygons

glEnable(GL_CULL);

50

Walking Through a Scene – 1/3void 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;

glutPostRedisplay();

}

51

Walking Through a Scene – 2/3void display(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

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,1.0);

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

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

colorcube();

glFlush();

glutSwapBuffers();

}

52

Walking Through a Scene – 3/3void 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);

}

53

Parallel-Projection Matrices

Perspective ViewOrthographic projection of distorted object

54

Normalization Rather than derive a different projection

matrix for each type of projection, we can convert all projections to orthogonal projections with the default view volume

This strategy allows us to use standard transformations in the pipeline and makes for efficient clipping

55

Pipeline View

modelviewtransformation

projectiontransformation

perspective division

clipping projection

nonsingular

4D 3D

against default cube 3D 2D

56

Notes We stay in four-dimensional homogeneous coordinate

s through both the modelview and projection transformations Both these transformations are nonsingular Default to identity matrices (orthogonal view)

Normalization lets us clip against simple cube regardless of type of projection

Delay final projection until end Important for hidden-surface removal to retain depth infor

mation as long as possible

57

Orthogonal NormalizationglOrtho(left,right,bottom,top,near,fa

r)normalization find transformation to convertspecified clipping volume to default

Canonical view volume

-1-1 -11 1 1

58

Orthogonal Matrix Two steps

Move center to origin

T(-(left+right)/2, -(bottom+top)/2,(near+far)/2)) Scale to have sides of length 2

S(2/(left-right),2/(top-bottom),2/(near-far))

1000

200

02

0

002

nearfar

nearfar

farnear

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

P = ST =

59

Final Projection Set z =0 Equivalent to the homogeneous coordinate

transformation

Hence, general orthogonal projection in 4D is

1000

0000

0010

0001

Morth =

P = MorthST

60

Oblique Projections The OpenGL projection functions cannot produce

general parallel projections such as

However if we look at the example of the cube it appears that the cube has been sheared

Oblique Projection = Shear + Orthogonal Projection

61

General Shear

top viewside view

cot

cot

tan

zyy

zxx

xx

z

p

p

p

62

Shear Matrixxy shear (z values unchanged)

Projection matrix

General case:

1000

0100

0φcot10

0θcot01

H(,) =

P = Morth H(,)

P = Morth STH(,)

63

Equivalency

64

Effect on Clipping The projection matrix P = STH transforms

the original clipping volume to the default clipping volume

top view

DOP DOP

near plane

far plane

object

clippingvolume

z = -1

z = 1

x = -1x = 1

distorted object(projects correctly)

65

Simple PerspectiveConsider a simple perspective with the COP at the

origin, the near clipping plane at z = -1, and a 90 degree field of view determined by the planes

x = z, y = z

66

Perspective MatricesSimple projection matrix in homogeneous

coordinates (z=-1, and the center of projection is at the origin)

Note that this matrix is independent of the far clipping plane

0100

0100

0010

0001

M =

67

Generalization

0100

βα00

0010

0001

Let N =

after persepective divison, the point (x, y, z, 1) goes to

x’’ = -x/zy’’ = -y/zz’’ = -(+/z)

If we apply an orthographic projection along the z-axis to N, we get:

0100

0000

0010

0001

MorthN=

And the projection of an arbitrary point pis, p’=MorthNp = , after perspective division:

z

y

x

0

xp = -x/zyp = -y/z

We get the same result regardless of and

68

Picking and If we pick

minmax

minmax

zz

zz

minmax

minmax

zz

zz2

the zmin plane is mapped to z = -1the zmax plane is mapped to z =1and the sides are mapped to x = 1, y = 1

Hence the new clipping volume is the default clipping volume

69

Normalization Transformation

original clipping

volumeoriginal object new clipping

volume

distorted objectprojects correctly

70

Normalization and Hidden-Surface Removal

Although our selection of the form of the perspective matrices may appear somewhat arbitrary, it was chosen so that if z1 > z2 in the original clipping volume then the for the transformed points z1’ > z2’

Thus we hidden surface removal works if we first apply the normalization transformation

However, the formula z’’ = -(+/z) implies that the distances are distorted by the normalization which can cause numerical problems especially if the near distance is small

71

OpenGL Perspective glFrustum allows for an unsymmetric vie

wing frustum (although gluPerspective does not)

72

OpenGL Perspective Matrix The normalization in glFrustum requires

an initial shear to form a right viewing pyramid, followed by a scaling to get the normalized perspective volume. Finally, the perspective matrix results in needing only a final orthogonal transformation

P = NSH

our previously defined perspective matrix

shear and scale

73

Normalization DetailsShear the point of ((xmin+xmax)/2,(ymin+ymax)/2, zmax) to (0, 0, zmax)

)

2(cot),

2(cot),(

max

maxmin1

max

maxmin1

z

yy

z

xxHH

So the resulting frustum is described by the planes:

max

min

max

minmax

max

minmax

2

2

zz

zz

zz

yyy

zz

xxx

Next, scale the volume so that x=z, y= z, so the scaling matrix is:S(2zmax/(xmax-xmin),2zmax/(ymax-ymin),1)Finally, we make use of the previous N.

x

z z=zmax

z=zmin

74

Why do we do it this way? Normalization allows for a single pipeline

for both perspective and orthogonal viewing

We keep in four dimensional homogeneous coordinates as long as possible to retain three-dimensional information needed for hidden-surface removal and shading

We simplify clipping

75

Projections and Shadows

Idea: put the projectionplane behind the object!For example, in this figure,assume (xl, yl, zl) is a lightsource, then we can set the projection plane on theplane of y=0

76

Implementing Shadows – 1/2Move the light source to the originby a translation:T(-xl, -yl, -zl), then wecould use a simple perspective projectionthrough the origin, the projection matrix is

Finally we translate everything back.

001

0

0100

0010

0001

ly

M

77

Implementing Shadows – 2/2GLfloat m[16];

/* shadow projection matrix */

For(i=0;i<15;i++) m[i]=0.0;

m[0]=m[5]=m[10]=1.0;

m[7]=-1.0/yl;

glColor3fv(polygon_color);

glBegin(GL_POLYGON);

. /* draw polygon normally */

.

glEnd();

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glTranslatef(xl, yl, zl);

glMultMatrixf(m);

glTranslate(-xl, -yl, -zl);

glColor3fv(shadow_color);

glBegin(GL_POLYGON);

. /* draw the polygon again */

.

glEnd();

glPopMatrix(); /* restore state */This works if objects cannot cast shadows on other objects!

78

Summary and Notes Classical Viewing Parallel Projection versus Perspective

Projection OpenGL Projections Projection Normalization Shadows