34
02/26/02 (c) 2002 University of Wiscon sin, CS 559 Last Time Canonical view pipeline Orthographic projection There was an error in the matrix for taking a simple orthographic volume and transforming it into the canonical view space The slides now online are correct In Shirley’s chapter on Transformation, note notation errors in the discussion of homogeneous coordinates Local Coordin ate Space World Coordina te Space View Space 3D Screen Space Displ ay Space Projection

02/26/02 (c) 2002 University of Wisconsin, CS 559 Last Time Canonical view pipeline Orthographic projection –There was an error in the matrix for taking

Embed Size (px)

Citation preview

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

Last Time

• Canonical view pipeline

• Orthographic projection– There was an error in the matrix for taking a simple orthographic

volume and transforming it into the canonical view space

– The slides now online are correct

• In Shirley’s chapter on Transformation, note notation errors in the discussion of homogeneous coordinates

Local Coordinate

Space

World Coordinate

Space

View Space

3D Screen Space

Display Space

Projection

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

Today

• Perspective viewing– Simple case

– Completely general case

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

Perspective Projection

• Abstract camera model - box with a small hole in it

• Pinhole cameras work in practice - camera obscura, etc

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

Distant Objects Are Smaller

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

Parallel lines meetcommon to draw film planein front of the focal point

02/26/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

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

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

• Define a focal distance, d, and put the image plane there (note d is negative)– This doesn’t quite fit into our viewing model, but we’ll come back

to that

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

Basic Perspective Projection

• If you know P(xv,yv,zv) and d, what is P(xs,ys)?– Where does a point in view space end up on the screen?

xv

yv

-zvd

P(xv,yv,zv)P(xs,ys)

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

Basic Case

• Similar triangles gives:

v

vs

z

x

d

x

v

vs

z

y

d

y

yv

-zv

P(xv,yv,zv)P(xs,ys)

View Plane

d

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

Simple Perspective Transformation

• Using homogeneous coordinates we can write:

dzz

y

x

d

y

x

v

v

v

v

s

s

vs

d

PP

0100

0100

0010

0001

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

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: zv=n, zv=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

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

Clipping Planes

xv

-zv

Near Clip Plane

Far Clip PlaneView

Volume

Left ClipPlane

Right ClipPlane

fn l

r

02/26/02 (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

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

Clipping Planes

xv

-zv

Near Clip Plane

Far Clip PlaneView

Volume

Left ClipPlane

Right ClipPlane

fFOV

02/26/02 (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

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

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

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

(r,t,n)

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

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

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

General Perspective

• 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

0100

00

000

000

0100

00

0010

0001

nffn

n

n

n

fnfnPM

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

Complete Perspective Projection

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

0100

00

000

000

1000

200

02

0

002

nffn

n

n

fn

fn

fn

bt

bt

bt

lr

lr

lr

POscreenview MMM

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

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:

0100

200

02

0

002

fn

nf

fn

fnbt

bt

bt

nlr

lr

lr

n

OpenGLM

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

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

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

Clipping

• Parts of the geometry to be rendered may lie outside the view volume– View volume maps to memory addresses– Out-of-view geometry generates invalid addresses– Geometry outside the view volume also behaves very strangely

under perspective projection• Triangles can be split into two pieces, for instance

• Clipping removes parts of the geometry that are outside the view

• Best done in screen space before perspective divide– Before dividing out the homogeneous coordinate

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

Clipping

• Points are trivial to clip - just check which side of the clip planes they are on

• Many algorithms for clipping lines exist– Next lecture

• Two main algorithms for clipping polygons exist– Sutherland-Hodgman (today)

– Weiler (next lecture)

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

Clipping Points

• A point is inside the view volume if it is on the “inside” of all the clipping planes– The normals to the clip planes are considered to point inward, toward the

visible stuff

• Now we see why clipping is done in canonical view space

• For instance, to check against the left plane:– X coordinate in 3D must be > -1

– In homogeneous screen space, same as: xscreen> -wscreen

• In general, a point, p, is “inside” a plane if:– You represent the plane as nxx+nyy+nzz+d=0, with (nx,ny,nz) pointing

inward

– And nxpx+nypy+nzpz+d>0

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

Polygon-Rectangle Clipping (2D)

• Task: Clip a polygon to a rectangle

• Easy cases:

• Hard Cases:

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

Sutherland-Hodgman Clip (1)

• Clip the polygon against each edge of the clip region in turn– Clip polygon each time to line containing edge

– Only works for convex clip regions (Why?)

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

Sutherland-Hodgman Clip (2)

• To clip a polygon to a line:– Consider the polygon as a list of vertices

– One side of the line is inside the clip region, the other outside

– Think of the process as rewriting the polygon, one vertex at a time

– Check start vertex: if “inside”, emit it, otherwise ignore it

– Process vertex list as follows…

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

Sutherland-Hodgman (3)

• Look at the next vertex in the list:– polygon edge crosses clip edge going from out to in: emit crossing

point, next vertex

– polygon edge crosses clip edge going from in to out: emit crossing

– polygon edge goes from out to out: emit nothing

– polygon edge goes from in to in: emit next vertex

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

Sutherland-Hodgman (4)

Inside Outside

s

p

Output p

Inside Outside

sp

Output i

Inside Outside

s

p

No output

Inside Outside

sp

Output i,p

i

i

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

Inside-Outside Testing

• Edges store a vector pointing toward the outside of the clip region

• Dot products give inside/outside information

Outside Insiden

s

f

i

x

0

0

0

x)(fn

x)(in

x)(sn

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

Sutherland-Hodgman (5)

• In 3D, clip against planes instead of lines– Six planes to clip against

– Inside/Outside test still works

• Suitable for hardware implementation– Only need the clip edge, the endpoints of the current edge, and the

last output point

– Polygon edges are output as they are found, and passed right on to the next clip region edge

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

Inside/Outside in Screen Space

• In screen space, clip planes are xs=±1, ys=±1, zs=0, zs=1

• Inside/Outside reduces to comparisons before perspective divide

ss

sss

sss

wz

wyw

wxw

0

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

Additional Clipping Planes

• Useful for doing things like cut-away views– Use a clip plane to cut off part of the object

– Only works if piece to be left behind if convex

• OpenGL allows you to do it

• Also one way to use OpenGL to identify objects in a region of space (uses the selection mechanism)

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

Other Ways to Reject

• If a polygonal object is closed, then no back-facing face is visible– Front-facing faces must occlude all back-facing ones

– Reject back-facing polygons in view space• Transform face normal and check

• Bounding volumes enclosing many polygons can be checked against the view volume– Done in software in world or view space

• Visibility can reject whole chunks of geometry without even looking at them