11.11.2014 Views

Clipping and Hidden Surfaces Clipping Basic Line Clipping Cohen ...

Clipping and Hidden Surfaces Clipping Basic Line Clipping Cohen ...

Clipping and Hidden Surfaces Clipping Basic Line Clipping Cohen ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Clipping</strong> <strong>and</strong><br />

<strong>Hidden</strong> <strong>Surfaces</strong><br />

CS-184: Computer Graphics<br />

Prof. James O’Brien<br />

<strong>Clipping</strong><br />

• Don’t want to draw stuff that is not in screen area<br />

– Waste of time<br />

– Bogus memory access<br />

– Looks ugly<br />

• <strong>Clipping</strong> removes unwanted stuff<br />

• Can also clip for other reasons<br />

– Cut away views<br />

1<br />

2<br />

<strong>Basic</strong> <strong>Line</strong> <strong>Clipping</strong><br />

<strong>Cohen</strong>-Sutherl<strong>and</strong> <strong>Line</strong> Clip<br />

• Clip line against each edge of clip region in turn<br />

– If both endpoints outside, discard line <strong>and</strong> stop<br />

– If both endpoints in, continue to next edge (or finish)<br />

– If one in, one out, chop line at crossing pt <strong>and</strong><br />

continue<br />

• Works in both 2D <strong>and</strong> 3D for convex clipping<br />

regions<br />

3<br />

4<br />

<strong>Cohen</strong>-Sutherl<strong>and</strong> <strong>Line</strong> Clip<br />

<strong>Cohen</strong>-Sutherl<strong>and</strong> <strong>Line</strong> Clip<br />

1 2<br />

3<br />

1 2<br />

3<br />

• Trivial reject <strong>and</strong> accept (speed things up…)<br />

4<br />

4<br />

1 2<br />

1 2<br />

3<br />

3<br />

Trivial Keep<br />

Trivial Reject<br />

NOT Trivial Reject<br />

4<br />

4<br />

5<br />

6<br />

1


<strong>Cohen</strong>-Sutherl<strong>and</strong> <strong>Line</strong> Clip<br />

• Out codes<br />

1 2<br />

0010<br />

3<br />

4<br />

0101<br />

Liang-Barsky <strong>Clipping</strong><br />

• Parametric clipping - view line in parametric<br />

form <strong>and</strong> reason about the parameter values<br />

• More efficient, as not computing the coordinate<br />

values at irrelevant vertices<br />

• Works for rectilinear clip regions in 2D or 3D<br />

• <strong>Clipping</strong> conditions on parameter: <strong>Line</strong> is inside<br />

clip region for values of t such that (for 2D):<br />

x<br />

y<br />

min<br />

min<br />

≤x1<br />

+ t∆x<br />

≤x<br />

≤ y + t∆y<br />

≤ y<br />

1<br />

max<br />

max<br />

∆x<br />

= x2<br />

−x1<br />

∆y<br />

= y −y<br />

2<br />

1<br />

7<br />

8<br />

Liang-Barsky <strong>Clipping</strong><br />

Liang-Barsky <strong>Clipping</strong><br />

• Infinite line intersects clip region edges at:<br />

qk<br />

t<br />

k<br />

=<br />

p<br />

k<br />

where<br />

p 1<br />

= −∆x<br />

q 1<br />

= x 1<br />

− x min<br />

p 2 = ∆x q 2 = x max − x 1<br />

p 3<br />

= −∆y q 3<br />

= y 1<br />

− y min<br />

p 4 = ∆y q 4 = y max − y 1<br />

• When p k 0, line goes from inside to outside – leaving<br />

• When p k =0, line is parallel to an edge (clipping is easy)<br />

• If there is a segment of the line inside the clip region,<br />

sequence of infinite line intersections must go: enter,<br />

enter, leave, leave<br />

Note: Left edge is 1, right edge is 2, top edge is 3, bottom is 4<br />

9<br />

10<br />

Liang-Barsky <strong>Clipping</strong><br />

Liang-Barsky <strong>Clipping</strong><br />

• Compute entering t values, which are q k /p k for each p k 0<br />

• Parameter value for small t end of line is:t small = max(0, entering<br />

t’s)<br />

• parameter value for large t end of line is: t large =min(1, leaving t’s)<br />

Enter<br />

• if t small


Sutherl<strong>and</strong>-Hodgman Polygon Clip<br />

Sutherl<strong>and</strong>-Hodgman Polygon Clip<br />

• Clip the polygon against each edge of the clip<br />

region in turn<br />

– Clip polygon each time to line containing edge<br />

– Only works for convex clip regions (Why?)<br />

• To clip a polygon to a line/plane:<br />

– Consider the polygon as a list of vertices<br />

– One side of the line/plane is considered inside the clip region,<br />

the other side is outside<br />

– We are going to rewrite the polygon one vertex at a time – the<br />

rewritten polygon will be the polygon clipped to the line/plane<br />

– Check start vertex: if “inside”, emit it, otherwise ignore it<br />

– Continue processing vertices as follows…<br />

13<br />

14<br />

Sutherl<strong>and</strong>-Hodgman Polygon Clip<br />

General <strong>Clipping</strong> (Weiler Algorithm)<br />

Inside<br />

Outside<br />

Inside<br />

Outside<br />

Inside<br />

Outside<br />

Inside<br />

Outside<br />

s<br />

p<br />

p<br />

i<br />

s<br />

p<br />

s<br />

i<br />

p<br />

s<br />

Output p<br />

Output i<br />

No output<br />

Output i <strong>and</strong> p<br />

15<br />

16<br />

General <strong>Clipping</strong> (Weiler Algorithm)<br />

Inside-Outside Testing<br />

Rearranging pointers<br />

makes it possible to<br />

enumerate all components<br />

of the intersection<br />

Changes to<br />

17<br />

18<br />

3


Inside/Outside in Screen Space<br />

Finding Intersection Points<br />

19<br />

20<br />

Backface Culling<br />

Z-Buffer ( a.k.a. Depth Buffer )<br />

• Use additional buffer to store a Z value at each pixel<br />

• When filling pixel write Z value to buffer also<br />

• Don’t fill if new Z value is larger than old<br />

• Quantization <strong>and</strong> aliasing problems sometimes occur<br />

• Very commonly used<br />

• St<strong>and</strong>ard feature in hardware<br />

• Interpolating Z values over polygons<br />

21<br />

22<br />

Z-Buffer <strong>and</strong> Transparency<br />

A-Buffer<br />

• Must render in order back to front<br />

Partially<br />

transparent<br />

3rd<br />

Front<br />

Partially<br />

transparent<br />

1st<br />

• Store list of polygons at each pixel (fragments)<br />

• Draw opaque stuff first,only keep closest frag<br />

• Second pass for transparent stuff<br />

Opaque<br />

Opaque<br />

2nd<br />

1st<br />

Opaque<br />

Opaque<br />

3rd<br />

2nd<br />

• Allows antialiasing…<br />

Good<br />

Not Good<br />

• Not good for hardware implementation<br />

Talk about compositing later…<br />

23<br />

24<br />

4


Scan <strong>Line</strong> Algorithm<br />

Scan <strong>Line</strong> Algorithm<br />

Assume polygons don’t intersect each other<br />

• As scanning keep list of active edges<br />

• Decide who’s on top when crossing edges<br />

• Advantages:<br />

– Simple<br />

– Potentially fewer quantization errors (more bits available for<br />

depth)<br />

– Don’t over-render (each pixel only drawn once)<br />

– Filter anti-aliasing can be made to work (have information<br />

about all polygons at each pixel)<br />

• Disadvantages:<br />

– Invisible polygons clog AEL, ET<br />

– Non-intersection criteria may be hard to meet<br />

25<br />

26<br />

Painters Algorithm<br />

BSP-Trees (Object Precision)<br />

• Sort based on depth <strong>and</strong> draw back to front<br />

• How dow we sort quickly?<br />

• What about intersecting polygons?<br />

• Construct a binary space partition tree<br />

– Depth-first traversal gives a rendering order (varient)<br />

• Tree splits 3D world with planes<br />

– The world is broken into convex cells<br />

– Each cell is the intersection of all the half-spaces of splitting<br />

planes on tree path to the cell<br />

• Also used to model the shape of objects, <strong>and</strong> in other<br />

visibility algorithms<br />

– BSP visibility in games does not necessarily refer to this<br />

algorithm<br />

27<br />

28<br />

BSP-Tree Example<br />

Building BSP-Trees<br />

• Choose polygon (arbitrary)<br />

1<br />

C<br />

4<br />

2<br />

A<br />

3<br />

B<br />

A<br />

- +<br />

B<br />

C<br />

- + - +<br />

3 2 4 1<br />

• Split its cell using plane on which polygon lies<br />

– May have to chop polygons in two (<strong>Clipping</strong>!)<br />

• Continue until each cell contains only one polygon<br />

fragment<br />

• Splitting planes could be chosen in other ways, but there<br />

is no efficient optimal algorithm for building BSP trees<br />

– Optimal means minimum number of polygon fragments in a<br />

balanced tree<br />

29<br />

30<br />

5


Building Example<br />

Building Example<br />

• We will build a BSP<br />

tree, in 2D, for a 3<br />

room building<br />

– Ignoring doors<br />

• Splitting edge order<br />

is shown<br />

– “Back” side of edge<br />

is side with the<br />

number<br />

5<br />

2<br />

3 4<br />

1<br />

6<br />

1<br />

- +<br />

3a, 4a, 6 2, 3b, 4b, 5<br />

3b<br />

3a<br />

5<br />

2<br />

1<br />

6<br />

4b<br />

4a<br />

31<br />

32<br />

Building Example<br />

Building Example<br />

1<br />

- +<br />

5b<br />

5a<br />

1<br />

- +<br />

5b<br />

5a<br />

3a, 4a, 6<br />

2<br />

- +<br />

4b, 5a 3b, 5b<br />

3b<br />

2<br />

4b<br />

3a<br />

+<br />

4a, 6<br />

-<br />

4b, 5a<br />

2<br />

+<br />

3b<br />

+<br />

3b<br />

2<br />

4b<br />

3a<br />

1<br />

4a<br />

5b<br />

3a<br />

1<br />

4a<br />

6<br />

6<br />

33<br />

34<br />

Building Example (Done)<br />

BSP-Tree Rendering<br />

3a<br />

+<br />

4a<br />

1<br />

- +<br />

6<br />

+<br />

4b<br />

-<br />

2<br />

+<br />

3b<br />

+ +<br />

5a<br />

5b<br />

3b<br />

3a<br />

5b<br />

1<br />

6<br />

2<br />

5a<br />

4b<br />

4a<br />

• Observation: Things on the opposite side of a splitting plane from<br />

the viewpoint cannot obscure things on the same side as the<br />

viewpoint<br />

• Rendering algorithm is recursive descent of the BSP Tree<br />

• At each node (for back to front rendering):<br />

– Recurse down the side of the sub-tree that does not contain the viewpoint<br />

Test viewpoint against the split plane to decide which tree<br />

– Draw the polygon in the splitting plane<br />

Paint over whatever has already been drawn<br />

– Recurse down the side of the tree containing the viewpoint<br />

35<br />

36<br />

6


BSP-Tree Rendering Example<br />

BSP-Tree Rendering<br />

C<br />

4<br />

A<br />

3<br />

-<br />

A<br />

+<br />

• Advantages:<br />

– One tree works for any viewing point<br />

B<br />

C<br />

– Filter anti-aliasing <strong>and</strong> transparency work<br />

1<br />

2<br />

B<br />

- + - +<br />

3 2 4 1<br />

3rd 4th 1st 2nd<br />

<br />

Have back to front ordering for compositing<br />

– Can also render front to back, <strong>and</strong> avoid drawing back<br />

polygons that cannot contribute to the view<br />

<br />

User two trees – an extra one that subdivides the window<br />

• Disadvantages:<br />

– Can be many small pieces of polygon<br />

37<br />

38<br />

Cells <strong>and</strong> Portals<br />

Cell <strong>and</strong> Portal Visibility<br />

• Assume the world can be broken into cells<br />

– Simple shapes<br />

– Rooms in a building, for instance<br />

• Define portals to be the transparent boundaries between<br />

cells<br />

– Doorways between rooms, windows, etc<br />

• In a world like this, can determine exactly which parts of<br />

which rooms are visible<br />

– Then render visible rooms plus contents<br />

• An Exact Visibility Algorithm -- sometimes<br />

• Start in the cell containing the viewer, with the<br />

full viewing frustum<br />

• Render the walls of that room <strong>and</strong> its contents<br />

• Recursively clip the viewing frustum to each<br />

portal out of the cell, <strong>and</strong> call the algorithm on the<br />

cell beyond the portal<br />

39<br />

40<br />

Cell-Portal Example<br />

Cell-Portal Example<br />

View<br />

View<br />

41<br />

42<br />

7


Cell-Portal Example<br />

View<br />

43<br />

44<br />

Acknowledgments<br />

• Some of these slide contents were based on<br />

original materials from<br />

– Prof. David Forsyth of U.C. Berkeley<br />

– Prof. Stephen Chenney of U.W. Madison<br />

45<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!