5
Computing Visibility Computing Visibility Lecture 13 Comp 236 Spring 2005 BSP Trees Ray Casting Depth Buffering Quiz 03/02/05 Lecture 13 2 Power of Plane Equations We’ve gotten a lot of mileage out of one simple equation. Basis for 3D outcode-clipping Basis for plane-at-a-time clipping Basis for viewpoint back-face culling [ ] 0 1 z y x d n n n z y x = 03/02/05 Lecture 13 3 One More Trick with Planes We can develop a visibility algorithm based on evaluating plane equations. Consider the central argument of the viewpoint culling algorithm. If the eye point is within the negative half-space of a face, the face is invisible. If the eye point is within the positive half-space of a face, the face is visible. Does this algorithm work for polygons in general configurations? 03/02/05 Lecture 13 4 Backface Culling for General Visibility Well almost... it would work if there were no overlapping faces. However, notice how the overlapping facets partition each other. Suppose we build a tree of these partitions. + - 1 + - 2 + - 3

Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

Embed Size (px)

Citation preview

Page 1: Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

Computing VisibilityComputing Visibility

Lecture 13Comp 236

Spring 2005

BSP TreesRay CastingDepth BufferingQuiz

03/02/05 Lecture 13 2

Power of Plane EquationsWe’ve gotten a lot of mileage out of one simple equation.

Basis for 3D outcode-clipping Basis for plane-at-a-time clipping Basis for viewpoint back-face culling

[ ] 0

1zyx

dnnn zyx =

03/02/05 Lecture 13 3

One More Trick with PlanesWe can develop a visibility algorithm based on evaluating plane equations.

Consider the central argument of the viewpoint culling algorithm.If the eye point is within the negative half-space of a face, the face is invisible.If the eye point is within the positive half-space of a face, the face is visible.

Does this algorithm work for polygons in general configurations?

03/02/05 Lecture 13 4

Backface Culling for General Visibility

Well almost... it would work if there were no overlapping faces.However, notice how the overlapping facets partition each other.Suppose we build a tree of these partitions.

+ -1+ -

2

+ -3

Page 2: Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

03/02/05 Lecture 13 5

I think that I shall never see…A Binary Space Partition (BSP) tree is a simple

spatial data structure to construct:1. Select a partitioning plane/face. 2. Partition the remaining planes/faces

according to the side of the partitioning plane that they fall on (+ or -).

3. Repeat with each of the two new sets.

Partitioning facets:

Partitioning requires testing all facets in the active set to find if they 1) lie entirely on the positive side of the partition plane, 2) lie entirely on the negative side, or 3) if they cross it. In the 3rd case of a crossing face we clip the offending face into two halves (using our plane-at-a-time clipping algorithm).

03/02/05 Lecture 13 6

Computing Visibility with BSP treesStarting at the root of the tree.

1. Classify viewpoint as being in the positive or negative halfspace of our plane

2. Call this routine with the “other” child(the one in the other half-space of our eye) (if it exists)

3. Draw the current partitioning plane 4. Call this routine with the child in

the same half-space as the eye (if it exists)

Intuitively, at each partition, we first drawthe stuff further away than the currentplane, then we draw the current plane, and then we draw the closer stuff. BSP traversal is called a "hidden surface elimination" algorithm, but it doesn’t really "eliminate" anything; it simply orders the drawing of primitive in a back-to-front order.

1

3

24

12

red

+green

-blue+blue

-green

4

3

5

03/02/05 Lecture 13 7

Computing Visibility with BSP treesStarting at the root of the tree.

1. Classify viewpoint as being in the positive or negative halfspace of our plane

2. Call this routine with the “other” child(the one in the other half-space of our eye) (if it exists)

3. Draw the current partitioning plane 4. Call this routine with the child in

the same half-space as the eye (if it exists)

Intuitively, at each partition, we first drawthe stuff further away than the currentplane, then we draw the current plane, and then we draw the closer stuff. BSP traversal is called a "hidden surface elimination" algorithm, but it doesn’t really "eliminate" anything; it simply orders the drawing of primitive in a back-to-front order.

red

+green

-blue+blue

-green

1

2

3

4

5

12

3

4

5

03/02/05 Lecture 13 8

BSP Tree ExampleComputing visibility or depth-sortingwith BSP trees is both simple and fast.

It resolves visibility at the primitive level.

Visibility computation is independent of screen size

Requires considerable preprocessing ofthe scene primitives

Primitives must be easy to subdividealong planes

Supports Constructive Solid Geometry(CSG) modeling

Page 3: Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

03/02/05 Lecture 13 9

Pixel-Level VisibilityThus far, we’ve considered visibility at the level of primitives. Now we will turn our attention to a class of algorithms that consider visibility at each pixel.

03/02/05 Lecture 13 10

Ray CastingAlgorithm: Cast a ray from the viewpoint through each pixel to find the closest

surface

Rendering Loop: foreach pixel in image

compute ray for pixelset depth = ZMAXforeach primitive in scene

if (ray intersects primitive) thenif (distance < depth) then

pixel = object colordepth = distance to object

endifendif

endforendfor

03/02/05 Lecture 13 11

Ray Casting• Advantages

– Conceptually simple– Can support CSG– Can take advantage of spatial coherence in scene– Can be extended to handle global illumination effects (ex:

shadows and reflectance)

• Disadvantages– Renderer must have access to entire retained model– Hard to map to special-purpose hardware– Visibility determination is coupled to sampling

• Subject to aliasing• Visibility computation is a function of resolution

03/02/05 Lecture 13 12

Depth BufferingAlgorithm:

Render each primitive onto the screen by computing the depth at each pixel in the rasterization loop. Conditionally write the pixel if it is closer than the previous depth value

Rendering Loop:set depth of all pixels to ZMAXforeach primitive in scene

foreach pixel in primitivecompute z at pixelif (z < depth) then

pixel = object colordepthpixel = z

endifendfor

endfor

Page 4: Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

03/02/05 Lecture 13 13

Depth Buffering• Advantages

– Primitives can be processed immediately (Hence: Immediate mode graphics APIs)– Primitives can be processed in any order (Exception: primitives at same depth &

transparent primitives)– Well suited to H/W implementation simple control of low-level (per pixel) operations– Spatial coherence

• Incremental evaluation of loops• Good memory access pattern

• Disadvantages– Visibility determination is coupled to sampling (Subject to aliasing)– Requires a Raster-sized arrray to store depth– Read-Modify-Write (Hard to make fast)– Excessive over-drawing

03/02/05 Lecture 13 14

What Gets Stored in a Depth-Buffer?Recall the projection matrix was augmented to include a mapping for z values.

There are two important considerations. 1. The mapping is from 3D (Affine) to 3D (Projective) 2. The mapping is linear (in general, planes map to planes)

=

′′′

−⋅⋅−

−+

−+−

−⋅

−+−

−⋅

1zyx

010000

0000

wzwywxw

nearfarnearfar2

nearfarnearfar

bottomtop)bottomtop(

bottomtopnear2

leftright)leftright(

leftrightnear2

03/02/05 Lecture 13 15

Computing "Z"We get the following expression for zy from our projection matrix

This mapping of z values is non-linear:

)nearfar(znearfar2)nearfar(zz

−⋅⋅⋅−+⋅=′

1

-1

03/02/05 Lecture 13 16

Linear yet Non-Linear?What does it mean for the projection mapping to preserve planes and lines, yet, have a

non-linear mapping of z values. How can this be?

Consider these examples:

As the parameter t is varied from 0 to 1, what geometric object is described?

Linearity of the space is preserved, but linearity of the parameterization is not.

So, suppose we have function of that maps one of these forms to one of the others. Allow this function to even change the actual values of x0, y0, dx, and dy, as long as this linear combination form is preserved.

Does this mapping preserve lines? What is the advantage of doing this? Why do we care?

+

+

=

+

=

+

=

dydx

1tt2

yx

yx

dydx

tyx

yx

dydx

tyx

yx

0

02

0

0

0

0

Page 5: Computing Visibility - The University of North Carolina at ... · Computing Visibility Lecture 13 Comp 236 Spring 2005 ... the face is invisible. If the eye point is within the positive

03/02/05 Lecture 13 17

Interpolating ZPreserving the linearity of the space allows us to use our plane equation method for interpolating values of z in the interior of the triangle.

Thus, we can used our handy formula for interpolating parameters within a triangle.

=

⋅z

z

z

2

1

0

010212

010212

010212

CBA

zzz

CCCBBBAAA

area21

03/02/05 Lecture 13 18

Monotonic Z ValuesWe need to be careful when reading the values out of a z-buffer and interpolating them. Even though, our interpolated values of z lie on a plane, uniform differences in depth-buffer values do no correspond to a uniform differences in space.However, our z-comparisons will still work because this parameter mapping, while not linear, is monotonic.

Note that when the z values are uniformly quantized the number of discrete discernable depthsis greater closer to the near plane than near the far plane. Is this good?

1

-1

03/02/05 Lecture 13 19

Next Time

Modeling Smooth SurfacesModeling Smooth Surfaces