Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward...

24
Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Transcript of Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward...

Page 1: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Computer Viewing

CS4395: Computer Graphics 1

Mohan SridharanBased on slides created by Edward Angel

Page 2: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Objectives

• Introduce the mathematics of projection.

• Introduce OpenGL viewing functions.

• Look at alternate viewing APIs.

CS4395: Computer Graphics 2

Page 3: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Computer Viewing

• There are three aspects of the viewing process, all implemented in the pipeline:– Positioning the camera:

• Setting the model-view matrix.

– Selecting a lens:• Setting the projection matrix.

– Clipping:• Setting the view volume.

CS4395: Computer Graphics 3

Page 4: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

The OpenGL Camera

• Initially, object and camera frames are the same:– Default model-view matrix is identity.

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

• OpenGL also specifies default view volume that is a cube with side of length 2 centered at the origin.– Default projection matrix is identity.

CS4395: Computer Graphics 4

Page 5: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Default Projection

• Default projection is orthogonal:

CS4395: Computer Graphics 5

clipped out

z=0

2

Page 6: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Moving the Camera Frame

• To visualize objects with both positive and negative z values:– 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 views are equivalent and determined by the model-view matrix:– Need a translation: glTranslatef(0.0,0.0,-d)– d > 0

CS4395: Computer Graphics 6

Page 7: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Moving Camera back from Origin

CS4395: Computer Graphics 7

frames after translation by –d d > 0

Page 8: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Moving the Camera

• Move the camera to any desired position by a sequence of rotations and translations.

• Example: side view.– Rotate the camera.– Move it away from origin.– Modelview matrix: C = TR

CS4395: Computer Graphics 8

Page 9: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

OpenGL code

• Remember that last transformation specified is first to be applied!

CS4395: Computer Graphics 9

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

Page 10: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

The LookAt Function

• GLU library contains the function (gluLookAt) to form the modelview matrix through a simple interface.

• Need to set up a direction and initialize:– Concatenate with modeling transformations.

• Example: isometric view of cube aligned with axes.

CS4395: Computer Graphics 10

glMatrixMode(GL_MODELVIEW):glLoadIdentity();gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0. 0.0);

Page 11: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Function: gluLookAt()

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

CS4395: Computer Graphics 11

Page 12: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Other Viewing APIs

• The LookAt function is only one possible API for positioning the camera.

• Others include (Sections 5.3.2, 5.3.4):– View reference point, view plane normal, view up

(PHIGS, GKS-3D).– Yaw, pitch, roll.– Elevation, azimuth, twist.– Direction angles.

CS4395: Computer Graphics 12

Page 13: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Projections and Normalization

• 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.

CS4395: Computer Graphics 13

xp xyp y zp = 0

Page 14: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Homogeneous Coordinate Representation

xp = xyp = yzp = 0wp = 1

CS4395: Computer Graphics 14

pp = Mp

M =

1000

0000

0010

0001

• Default orthographic projection:

• In practice, can let M=I and set z term to zero later.

Page 15: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Perspective Projection

• Preserves lines but not affine.

• Irreversible: all points along a projector project to the same point.

• Later: invertible variant of projection transformation that preserves distances (hidden-surface removal).

CS4395: Computer Graphics 15

Page 16: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Simple Perspective Projection

• Center of projection at the origin.• Projection plane z = d, d < 0

CS4395: Computer Graphics 16

Page 17: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Simple Perspective Equations

• Consider top and side views:

CS4395: Computer Graphics 17

xp =

dz

x

/

dz

x

/yp =

dz

y

/zp = d

Page 18: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Simple Perspective Projection

• Representation in 4D for a 3D point:

• Recover 3D point by dividing first three components by the fourth component.

• Points in 3D become lines through the origin in 4D.• Large class of transformations using 4x4 matrices with the last

row altered.

CS4395: Computer Graphics 18

w

wz

wy

wx

p

Page 19: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Perspective Projection Example

consider q = Mp where

CS4395: Computer Graphics 19

M =

0/100

0100

0010

0001

d

dz

z

y

x

1

z

y

x

p = q =

Page 20: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Perspective Division• However w 1, so divide by w to return from homogeneous

coordinates to 3D space.• This perspective division yields:

• In homogeneous coordinates:

• Apply projection matrix after model-view matrix, then perform perspective division.

CS4395: Computer Graphics 20

dzdz

yy

dz

xx ppp ,

/,

/

11

/

/

'p

p

p

z

y

x

ddz

ydz

x

q

Page 21: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

OpenGL Orthogonal Viewing

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

CS4395: Computer Graphics 21

near and far measured from camera

Page 22: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

OpenGL Perspective

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

CS4395: Computer Graphics 22

Page 23: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Using Field of View

• With glFrustum it is often difficult to get the desired view.

• gluPerpective(fovy, aspect, near, far) often provides a better interface.

CS4395: Computer Graphics 23

aspect = w/h

front plane

Page 24: Computer Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.

Other Issues

• Hidden surface removal: Section 5.6.

• Interactive mesh displays: Section 5.7.

CS4395: Computer Graphics 24