Document 7363895

Download Report

Transcript Document 7363895

Last Time

• Canonical view pipeline Local Coordinate Space World Coordinate Space View Space 3D Screen Space Projection Display Space 10/08/02 (c) 2002 University of Wisconsin, CS 559

• General Orthographic • Perspective viewing – Simple case – Completely general case

Today

10/08/02 (c) 2002 University of Wisconsin, CS 559

Simple Orthographic Projection

• Specify the region of space that we wish to render as a

view volume

• Assume that world coordinate axes line up with the image plane coordinate axes – Viewer is looking in the –

z

direction, with

x

to the right and

y

up • The view volume has: – a

near plane

at

z=n

– a

far plane

at

z=f , (f < n)

– a

left plane

at

x=l

– a

right plane

at

x=r, (r>l) z

– a

top plane

at

y=t

– and a

bottom plane

at

y=b, (b

(c) 2002 University of Wisconsin, CS 559 10/08/02

Rendering the Volume

• To project, map the view volume onto the canonical view volume – After that, we know how to map the view volume to the window • The mapping looks similar to the one for canonical→window:     

x y canonical canonical z canonical

1                  2 2  

r

0 0 0 0 0 0  

l

  2 

t

t

0 0 0 0 0 0  

b

  2 2 

n

n

0 0 0 0 0 0  

f f

 0 0 0 1             1   0 0 0

t r

 

n

  

f b l

0 1 0 0 0 1 0    

t r

 

n

  

b l f

  1 0      

n t r

              1

b

1

f l view y view z view

        2 2 2     

x

canonical

M

view

 

canonical

x

view

10/08/02 (c) 2002 University of Wisconsin, CS 559

General Orthographic Projection

• We could look at the world from any direction, not just along

–z

• The image could rotated in any way about the viewing direction:

x

need not be right, and

y

need not be up • How can we specify the view under these circumstances?

10/08/02 (c) 2002 University of Wisconsin, CS 559

Specifying a View

• The location of the eye in space – A point in space for the center of projection,

(e x ,e y ,e z )

• The direction in which we are looking: gaze direction – Specified as a vector

: (g x ,g y ,g z )

– This vector will be normal to the image plane • A direction that we want to appear

up

in the image –

(up x ,up y ,up z ),

This vector does not have to be perpendicular to

n

• We also need the size of the view volume –

l,r,t,b,n,f

– Specified

with respect to the eye and image plane

, not the world (c) 2002 University of Wisconsin, CS 559 10/08/02

Getting there…

• We wish to end up in the “simple” situation, so we need a coordinate system with: – A vector toward the viewer – One pointing right in the image plane – One pointing up in the image plane – The origin at the eye • We must: – Define such a coordinate system,

view space

– Transform points from the world space into view space – Apply our simple projection from before (c) 2002 University of Wisconsin, CS 559 10/08/02

View Space

• Given our camera definition: – Which point is the origin of view space?

– Which direction is the normal to the view plane,

w

?

– How do we find the right vector,

u

?

– How do we find the up vector,

v

?

• Given these points, how do we do the transformation?

10/08/02 (c) 2002 University of Wisconsin, CS 559

View Space

• The origin is at the eye:

(e x ,e y ,e z )

• The normal vector is the normalized viewing direction:

w

 

g

– It is actually more general to think of it as the vector perpendicular to the image plane, as you must do in the homework • We know which way up should be, and we know we have a right handed system, so

u=up×w,

normalized:

u

• We have two vectors in a right handed system, so to get the third:

v=w×u

(c) 2002 University of Wisconsin, CS 559 10/08/02

World to View

• We must translate so the origin is at

(e x ,e y ,e z )

• To complete the transformation we need to do a rotation • After this rotation: – The direction

u

in world space should be the direction (1,0,0) in view space – The vector

v

should be (0,1,0) – The vector

w

should be (0,0,1) • The matrix that does that is:     

u v w x

0

x x u y v y w y

0

u z v z w z

0 0 0 0 1      (c) 2002 University of Wisconsin, CS 559 10/08/02

All Together

• We apply a translation and then a rotation, so the result is:

M

world

 

view

     

u v w x

0

x x u y v y w y

0

u z v z w z

0 0  0   0 1    1   0   0 0 0 1 0 0 0 0 1 0 

e x

e y

e z

1           

u v w x

0

x x

• And to go all the way from world to screen:

M

world

 

canonical

x

canonical

M

world

M

view

 

canonical

 

canonical

x

world

M

world

 

view u y v y w y

0

u z v z w z

0   

u v w

1   

e e e

     (c) 2002 University of Wisconsin, CS 559 10/08/02

OpenGL and Transformations

• OpenGL internally stores several matrices that control viewing of the scene – The MODELVIEW matrix is intended to capture all the transformations up to the view space – The PROJECTION matrix captures the view to screen conversion • You also specify the mapping from the canonical view volume into window space – Directly through function calls to set up the window • Matrix calls multiply some matrix M onto the current matrix C, resulting in CM – Set view transformation first, then set transformations from local to world space – last one set is first one applied 10/08/02 (c) 2002 University of Wisconsin, CS 559

OpenGL Camera

• The default OpenGL image plane has u aligned with the x axis, v aligned with y, and n aligned with z – Means the default camera looks along the negative z axis – Makes it easy to do 2D drawing (no need for any view transformation) • glOrtho(…) sets the view->canonical matrix – Modifies the PROJECTION matrix • gluLookAt(…) sets the world->view matrix – Takes an image center point, a point along the viewing direction and an up vector – Multiplies a world->view matrix

onto the current MODELVIEW matrix

– You could do this yourself, using glMultMatrix(…) with the matrix from the previous slides (c) 2002 University of Wisconsin, CS 559 10/08/02

Left vs Right Handed View Space

• You can define u as right, v as up, and n as toward the viewer: a right handed system

u

v=w

– Advantage: Standard mathematical way of doing things • You can also define u as right, v as up and n as into the scene: a left handed system

v

u=w

– Advantage: Bigger n values mean points are further away • OpenGL is right handed • Many older systems, notably the Renderman standard developed by Pixar, are left handed (c) 2002 University of Wisconsin, CS 559 10/08/02

Perspective Projection

• Abstract camera model - box with a small hole in it • Pinhole cameras work in practice - camera obscura, etc 10/08/02 (c) 2002 University of Wisconsin, CS 559

Distant Objects Are Smaller

10/08/02 (c) 2002 University of Wisconsin, CS 559

Parallel lines meet

common to draw film plane

in front

of the focal point 10/08/02 (c) 2002 University of Wisconsin, CS 559

Vanishing points

• Each set of parallel lines (=direction) meets at a different point: The vanishing point for this direction – Classic artistic perspective is 3 point persepctive • Sets of parallel lines on the same plane lead to collinear vanishing points: the horizon for that plane • Good way to spot faked images (c) 2002 University of Wisconsin, CS 559 10/08/02

Basic Perspective Projection

• Assume you have transformed to view space, with

x

to the right,

y

up, and

z

back toward the viewer • Assume the origin of view space is at the center of projection (the eye) • Define a focal distance,

d

, and put the image plane there (note

d

is negative) 10/08/02 (c) 2002 University of Wisconsin, CS 559

Basic Perspective Projection

• If you know

P(x v ,y v ,z v )

and

d,

what is

P(x s ,y s )?

– Where does a point in view space end up on the screen?

P(x v ,y v ,z v ) y v P(x s ,y s ) d -z v x v

(c) 2002 University of Wisconsin, CS 559 10/08/02

Basic Case

• Similar triangles gives:

x s d

x v z v y v P(x s ,y s ) y s d

y v z v P(x v ,y v ,z v )

10/08/02

d

View Plane (c) 2002 University of Wisconsin, CS 559

-z v

Simple Perspective Transformation

• Using homogeneous coordinates we can write:    

x s y d s

          

z v x v y v z v d

     

P

s

   1   0   0 0 0 1 0 0 0 0 1 1

d

0 0 0 0      

P

v

(c) 2002 University of Wisconsin, CS 559 10/08/02

Perspective View Volume

• Recall the orthographic view volume, defined by a near, far, left, right, top and bottom plane • The perspective view volume is also defined by near, far, left, right, top and bottom planes – the

clip planes

– Near and far planes are parallel to the image plane:

z v =n, z v =f

– Other planes all pass through the center of projection (the origin of view space) – The left and right planes intersect the image plane in vertical lines – The top and bottom planes intersect in horizontal lines (c) 2002 University of Wisconsin, CS 559 10/08/02

x v

10/08/02

n

Near Clip Plane

l r

Clipping Planes

View Volume Left Clip Plane

f

Far Clip Plane

-z v

Right Clip Plane (c) 2002 University of Wisconsin, CS 559

Where is the Image Plane?

• Notice that it doesn’t really matter where the image plane is located, once you define the view volume – You can move it forward and backward along the

z

axis and still get the same image, only scaled • But we need to know where it is to define the clipping planes – Assume the left/right/top/bottom planes are defined according to where they cut the near clip plane • Or, define the left/right and top/bottom clip planes by the

field of view

(c) 2002 University of Wisconsin, CS 559 10/08/02

x v

10/08/02 FOV

Clipping Planes

Near Clip Plane View Volume Left Clip Plane

f

Far Clip Plane

-z v

Right Clip Plane (c) 2002 University of Wisconsin, CS 559

OpenGL

• gluPerspective(…) – Field of view in the

y

direction (vertical field-of-view) – Aspect ratio (should match window aspect ratio) – Near and far clipping planes – Defines a symmetric view volume • glFrustum(…) – Give the near and far clip plane, and places where the other clip planes cross the near plane – Defines the general case – Used for stereo viewing, mostly (c) 2002 University of Wisconsin, CS 559 10/08/02

Perspective Projection Matrices

• We want a matrix that will take points in our perspective view volume and transform them into the orthographic view volume – This matrix will go in our pipeline just before the orthographic projection matrix

(r,t,n) (r,t,n) (l,b,n) (l,b,n)

(c) 2002 University of Wisconsin, CS 559 10/08/02

Mapping Lines

• We want to map all the lines through the center of projection to parallel lines – Points on lines through the center of projection map to the same point on the image – Points on parallel lines map orthographically to the same point on the image – If we convert the perspective case to the orthographic case, we can use all our existing methods • The intersection points of lines with the near clip plane should not change • The matrix that does this, not surprisingly, looks like the matrix for our simple perspective case (c) 2002 University of Wisconsin, CS 559 10/08/02

General Perspective

M

P

  1   0   0 0 0 1 0 0 0 

n

 0

f

n

1

n

 0 0 0

f

          

n

0 0 0 0

n

0 0 0 0

n

 1

f

0 0 

nf

0      • This matrix leaves points with

z=n

unchanged • It is just like the simple projection matrix, but it does some extra things to

z

to map the depth properly • We can multiply a homogenous matrix by any number without changing the final point, so the two matrices above have the same effect (c) 2002 University of Wisconsin, CS 559 10/08/02

Complete Perspective Projection

• After applying the perspective matrix, we still have to map the orthographic view volume to the canonical view volume:

M

view

 

screen

M

O

M

P

          

r

0 0 0 2 

l

 

t

0  2

b

 0 0 

n

 0 0 2 0

f

     

n t

  

r

r

 

n t

   

b f l b

f l

     1          

n

  0   0 0 0

n

0 0 

n

0 0  1

f

 0 0 

nf

0      (c) 2002 University of Wisconsin, CS 559 10/08/02

OpenGL Perspective Projection

• For OpenGL you give the distance to the near and far clipping planes • The total perspective projection matrix resulting from a glFrustum call is:

M

OpenGL

          

r

2 

n l

0 0 0  

t

2  0 0 0

n b

  

n n

 

t t

 

r r

    

l l b b

   

f

  1

f

  

n

2

f

 0 0 0

n f

           (c) 2002 University of Wisconsin, CS 559 10/08/02

Near/Far and Depth Resolution

• It may seem sensible to specify a very near clipping plane and a very far clipping plane – Sure to contain entire scene • But, a bad idea: – OpenGL only has a finite number of bits to store screen depth – Too large a range reduces resolution in depth - wrong thing may be considered “in front” – See Shirley for a more complete explanation • Always place the near plane as far from the viewer as possible, and the far plane as close as possible (c) 2002 University of Wisconsin, CS 559 10/08/02