2/26/04© university of wisconsin, cs559 spring 2004 last time general orthographic viewing...

35
2/26/04 © University of Wisconsin, CS559 S pring 2004 Last Time General Orthographic Viewing Specifying cameras in world coordinates Building worldview transformation matrices OpenGL transformations Basic perspective projection Properties of perspective projection The “eye at origin looking along –z” special case General Perspective Specifying perspective view volumes (frustum)

Upload: dorcas-obrien

Post on 23-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Last Time

• General Orthographic Viewing– Specifying cameras in world coordinates

– Building worldview transformation matrices

• OpenGL transformations

• Basic perspective projection– Properties of perspective projection

– The “eye at origin looking along –z” special case

• General Perspective– Specifying perspective view volumes (frustum)

Page 2: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Today

• More general perspective viewing

• Clipping

Page 3: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Basic Perspective Projection

• Eye at origin, looking along -z

xv

yv

-zvd

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

Page 4: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 5: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Clipping Planes

xv

-zv

Near Clip Plane

Far Clip PlaneView

Volume

Left ClipPlane

Right ClipPlane

fn l

r

Page 6: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 7: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Clipping Planes

xv

-zv

Near Clip Plane

Far Clip PlaneView

Volume

Left ClipPlane

Right ClipPlane

fFOV

Assumes a symmetric view volume

Page 8: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

OpenGL

• gluPerspective(…)– Field of view in the y direction, FOV, (vertical field-of-view)

– Aspect ratio, a, should match window aspect ratio

– Near and far clipping planes, n and f

– 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

Page 9: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

gluPerspective to glFrustum

• As noted previously, glu functions don’t add basic functionality, they are just more convenient– So how does gluPerspective convert to glFrustum?

– Symmetric, so only need t and l

y

z

FOV / 2

n

t ?

Page 10: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

gluPerspective to glFrustum

• As noted previously, glu functions don’t add basic functionality, they are just more convenient– So how does gluPerspective convert to glFrustum?

– Symmetric, so only need t and l

y

z

FOV / 2

n

t

tal

FOVnt

n

tFOV

2tan

2tan

Page 11: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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)

Page 12: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 13: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 14: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 15: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 16: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

Page 17: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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 canonical space before perspective divide– Before dividing out the homogeneous coordinate

Page 18: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Clipping

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

• Many algorithms for clipping lines exist– You need one for the 2nd project

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

– Weiler

Page 19: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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 region

• 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

Page 20: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Sutherland-Hodgman Clip

• 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?)

Page 21: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Sutherland-Hodgman Clip

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

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

– We are going to rewrite the polygon one vertex at a time – the rewritten polygon will be the polygon clipped to the line/plane

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

– Continue processing vertices as follows…

p0

p4

p3

p2

p1

p0 p4p3p2p1

Clipperpoints

inpoints

out

Page 22: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Sutherland-Hodgman (3)

Inside Outside

s

p

Output p

Inside Outside

sp

Output i

Inside Outside

s

p

No output

Inside Outside

sp

Output i and p

i

i

Page 23: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Sutherland-Hodgman (4)

• Look at the next vertex in the list, p, and the edge from the last vertex, s, to p. If the…– polygon edge crosses the clip line/plane going from out to in: emit

crossing point, i, next vertex, p

– polygon edge crosses clip line/plane going from in to out: emit crossing, i

– polygon edge goes from out to out: emit nothing

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

Page 24: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Inside-Outside Testing

• Lines/planes store a vector pointing toward the outside of the clip region – the outward pointing normal– Could re-define for inward

pointing

• Dot products give inside/outside information

• Note that x (a vector) is any point on the clip line/plane

Outside Inside

n

s

f

i

x

0

0

0

x)(fn

x)(in

x)(sn

Page 25: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Finding Intersection Pts

• Use the parametric form for the edge between two points, x1 and x2:

• For planes of the form x=a:

• Similar forms for y=a, z=a• Solution for general plane can also be found

10 )()( 121 ttt xxxx

))()(

)(),(

)(

)(,( 1

12

1211

12

121 xa

xx

zzzxa

xx

yyyai

x

Page 26: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Inside/Outside in Screen Space

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

• Inside/Outside reduces to comparisons before perspective divide

sss

sss

sss

wzw

wyw

wxw

Page 27: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Hardware Sutherland-Hodgman

• 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

Clip Top

Vertices in Clip Right

Clip Bottom

Clip Left

Clipped vertices out Clip Far

Clip Near

Page 28: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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

– OpenGL supports optional back-face culling (and front-face culling too)

• 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

Page 29: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Clipping In General

• Apart from clipping to the view volume, clipping is a basic operation in many other algorithms– Special purpose rendering might use different clipping (Project 2)

– Breaking space up into chunks

– 2D drawing and windowing

– Modeling

• May require more complex geometry than rectangular boxes

Page 30: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

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 is 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)

Page 31: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Clipping Lines

• Lines can also be clipped by Sutherland-Hodgman– Slower than necessary, unless you already have hardware

• Better algorithms exist– Cohen-Sutherland

– Liang-Barsky

– Nicholl-Lee-Nicholl (we won’t cover this one – only good for 2D)

Page 32: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Cohen-Sutherland (1)

• Works basically the same as Sutherland-Hodgman– Was developed earlier

• Clip line against each edge of clip region in turn– If both endpoints outside, discard line and stop

– If both endpoints in, continue to next edge (or finish)

– If one in, one out, chop line at crossing pt and continue

• Works in both 2D and 3D for convex clipping regions

Page 33: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Cohen-Sutherland (2)

1 23

4

1 2

3

4

3

4

1 2

3

4

1 2

Page 34: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Cohen-Sutherland (3)

• Some cases lead to premature acceptance or rejection– If both endpoints are inside all edges

– If both endpoints are outside one edge

• General rule of clipping – if a fast test can cover many cases, do it first

Page 35: 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view

2/26/04 © University of Wisconsin, CS559 Spring 2004

Cohen-Sutherland - Details

• Only need to clip line against edges where one endpoint is out

• Use outcode to record endpoint in/out wrt each edge. One bit per edge, 1 if out, 0 if in.

• Trivial reject: – outcode(x1) & outcode(x2)!=0

• Trivial accept:– outcode(x1) | outcode(x2)==0

• Which edges to clip against?– outcode(x1) ^ outcode(x2)

1 2

3

4

0010

0101