30
CS-184: Computer Graphics Lecture 15: Clipping and Hidden Surfaces Robin Held UC Berkeley Based on slides by James O’Brien & Greg Humphreys 2 Today Clipping Application: scan-line conversion Clipping to view volume Clipping arbitrary points, lines, polygons, volumes Hidden surface removal 3 Clipping Want to avoid drawing primitives outside area of interest Too close: obscures view Behind camera: obscures view

CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

CS-184: Computer GraphicsLecture 15: Clipping and Hidden Surfaces

Robin HeldUC Berkeley

Based on slides by James O’Brien & Greg Humphreys

2

Today• Clipping • Application: scan-line conversion• Clipping to view volume• Clipping arbitrary points, lines, polygons, volumes

• Hidden surface removal

3

Clipping•Want to avoid drawing primitives outside area of interest

• Too close: obscures view

• Behind camera: obscures view

Page 2: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

4

Clipping• Stuff outside view volume should not be drawn

5

Clipping

• Stuff outside view volume should not be drawn• Too close: obscures view

• Too far :

• Complexity

• Z-buffer problems

• Too high/low/right/left:

• Memory errors

• Broken algorithms

• Complexity

3D Rendering Pipeline3D Primitives

ModelingTransformation

Clipping

Lighting

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

2D Screen Coordinates

2D Screen Coordinates

ViewingTransformation

3D World Coordinates

2D Image Coordinates

Page 3: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

3D Rendering Pipeline3D Primitives

ModelingTransformation

Clipping

Lighting

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

2D Screen Coordinates

2D Screen Coordinates

ViewingTransformation

3D World Coordinates

2D Image Coordinates

2D Rendering Pipeline

ViewportTransformation

ScanConversion

Clipping

Image

•Clip portions of geometric primitives residing outside the window

•Transform the clipped primitives from screen to image coordinates

•Fill pixels representing primitives in screen coordinates

2D Rendering Pipeline

ViewportTransformation

ScanConversion

Clipping

Image

•Clip portions of geometric primitives residing outside the window

•Transform the clipped primitives from screen to image coordinates

•Fill pixels representing primitives in screen coordinates

Page 4: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Clipping• Avoid drawing parts of primitives outside window• Window defines part of scene being viewed • Must draw geometric primitives only inside window

Screen CoordinatesWindow

Clipping• Avoid drawing parts of primitives outside window• Window defines part of scene being viewed • Must draw geometric primitives only inside window

ViewingWindow

Clipping• Avoid drawing parts of primitives outside window• Points • Lines• Polygons• etc.

ViewingWindow

Page 5: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Point Clipping

Windowwx1 wx2

wy2

wy1

(x,y)

• Is point (x,y) inside the clip window?

inside = (x >= wx1) && (x <= wx2) && (y >= wy1) &&

(y <= wy2);

Line Clipping• Find the part of a line inside the clip window

P1

P10

P9

P8

P7

P4P3

P6

P5

P2

Before Clipping

P’8

P’7

P4P3

P6

P’5

After Clipping

Line Clipping• Find the part of a line inside the clip window

Page 6: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen Sutherland Line Clipping• Use simple tests to classify easy cases first

P1

P10

P9

P8

P7

P4P3

P6

P5

P2

Cohen Sutherland Line Clipping• Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

P1

P10

P9

P8

P7

P4P3

P6

P5

P2

Cohen Sutherland Line Clipping• Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

Bit 1 Bit 2

Bit 3

Bit 4P1

P10

P9

P8

P7

P4P3

P6

P5

P2

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 7: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen Sutherland Line Clipping• Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen Sutherland Line Clipping• Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 8: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 9: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 10: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 11: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Page 12: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4

P10

P9

P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Compute intersections with window boundary for lines

that can’t be classified quickly

Bit 1 Bit 2

Bit 3

Bit 4P’8

P’7

P4P3

P6

P’5

0001

0100

0110

1000 0000

1010

0101

0010

1001

Cohen-Sutherland Line Clipping• Pseudo (c++) code:

1. Define constants for each side of clipping rectangle:

2. Use a simple function to test a single point’s location

36

! if (x < xmin) // to the left of clip window! ! code = LEFT;! else if (x > xmax) // to the right of clip window! ! code = RIGHT;! if (y < ymin) // below the clip window! ! code |= BOTTOM;! else if (y > ymax) // above the clip window! ! code |= TOP;! return code;

Low memory footprint

source: http://en.wikipedia.org/wiki/Cohen–Sutherland

Operator accumulates 1’s

const int LEFT = 1; // 0001const int RIGHT = 2; // 0010const int BOTTOM = 4; // 0100const int TOP = 8; // 1000

Page 13: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Cohen-Sutherland Line Clipping3. For a line with endpoints (x0, y0) and (x1, y1):

a) Get position codes for each endpoint, compare them using | (OR)

- If neither code contains any 1’s, draw the entire line

b) Compare using & (AND)

- If true, then both points are outside the box. Reject entire line

37

0001

0100

0110

1000 0000

1010

0101

0010

1001

1000 & 1000 = TRUE

!(0000 | 0000) = TRUE

Cohen-Sutherland Line Clippingc) Otherwise, one point is outside the rectangle, so find the intersection

i) Choose a point outside the viewing rectangle (xt, yt)

ii) Find the closest intersection with the rectangle, relative to that pt.

Use y = yt + slope * (x - xt), x = xt + 1/slope * (y - yt)

Set x or y in the above equation depending on location of point:

Top: y = ymax Bottom: y = ymin

Left: x = xmin Right: x = xmax

38

(x0, y0)

(x1, y1)

y = ymax

x = x0+(x0-x1)/(y0-y1)*(y-y0)ymax

ymin xm

in

xm

ax

Cohen-Sutherland Line Clipping

iv. Reset old point to new location

v. Recursively pass new line to algorithm

39

(x0, y0)

(x1, y1)

ymax

ymin xm

in

xm

ax

1010

Page 14: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

(x1, y1)

Cohen-Sutherland Line Clipping

iv. Reset old point to new location

v. Recursively pass new line to algorithm

40

(x0, y0)ymax

ymin xm

in

xm

ax

1010

41

Liang-Barsky Clipping

P0

P1

• Save time by using parametric equation of line from the start to find its intersections with the window

• Calculations used to determine in/out state of points also useful for finding intersections

42

Liang-Barsky Clipping

P1 (x1, y1)

P0 (x0, y0)

deltaX = x1 - x0

deltaY

= y1 - y0

ymax

ymin

xmin

xmax

• Parametric equation P(t) = P0 + t(P1-P0)

•Want to find the values of t on the edges of the rectangle

• Initial t0 = 0 and t1= 1. We’ll change them as we find for intersections

• Check one side at a time

t0 = 0

t1 = 1

Page 15: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

43

Liang-Barsky ClippingP1 (x1, y1)

P0 (x0, y0)deltaX

deltaY

ymax

ymin

xmin

xmax

• Example: Start with left side

p = -deltaX

q = -(xmin - x0)

r = q/pIntuition: Value of t whenthe line crosses left side

44

Liang-Barsky ClippingP1 (x1, y1)

P0 (x0, y0)

ymax

ymin

xmin

xmax

• Example: Check left side

p = -deltaX

q = -(xmin - x0)

r = q/p

If p == 0 && q <0: Line is vertical and to the left of the rectangle. Break and don’t draw.

deltaX

deltaY

45

Liang-Barsky Clippingymax

ymin

xmin

xmax

• Example: Check left side

p = -deltaX

q = -(xmin - x0)

r = q/p

If p < 0:If (r > t1) Line is outsideelse if (r > t0) t0 = r

If p > 0:If (r < t0) Line is outsideelse if (r < t1) t1 = r

1

1

2

2

3

3

P0

P1P0

P1

P0

P1 P1

P04

4

Page 16: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

46

Liang-Barsky ClippingP1 (x1, y1)

P0 (x0, y0)deltaX = x1 - x0

deltaY

= y1 - y0

ymax

ymin

xmin

xmax

• Example: Check left side

p = -deltaX

q = -(xmin - x0)

r = q/p

If p < 0:If (r > t1) Line is outsideelse if (r > t0) t0 = r

If p > 0:If (r < t0) Line is outsideelse if (r < t0) t0 = r

47

Liang-Barsky ClippingP1 (x1, y1)

P0 (x0, y0) deltaX = x1 - x0

deltaY

= y1 - y0

ymax

ymin

xmin

xmax

• Redefine P0 and P1 using new values for t0 and t1

P0 = t0(P1 - P0)

P0 = t1(P1 - P0)

• Proceed to next side (bottom, then right, then top)

Liang-Barsky ClippingP1 (x1, y1)

P0 (x0, y0) deltaX = x1 - x0

deltaY

= y1 - y0

ymax

ymin

xmin

xmax

• Variables for each side:

Left: p = -deltaX, q = -(xmin - x0)

Right: p = deltaX, q = (xmax - x0)

Top: p = deltaY, q = (ymax - y0)

Bottom: p = -deltaY, q = -(ymin - y0)

Page 17: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

49

Clipping Line to Arbitrary Line/Plane

a

bn̂

Line segment to be clipped

Line/plane that clips it r

x(t) = a+ t(b−a)

n̂ ·x− n̂ · r= 0

50

Clipping Line to Line/Plane

a

bn̂

Line segment to be clipped

Line/plane that clips it

x(t) = a+ t(b−a)

n̂ ·x− f = 0

}n̂ · (a+ t(b−a))− f = 0

n̂ ·a+ t(n̂ · (b−a))− f = 0

t =f − n̂ ·an̂ ·d

51

Clipping Line to Line/Plane

a

bn̂

t =f − n̂ ·an̂ ·d

• Segment may be on one side

• Lines may be parallel

t �∈ [0 . . .1]

n̂ ·d= 0|n̂ ·d|≤ ε (Recall comments about numerical issues)

Page 18: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

52

Recap: Line Clipping• Sutherland-Hodgman algorithm• Basically edge walking

• Clipping done often... should be efficient• Liang-Barsky parametric space algorithm• Use dot-product trick for finding intersections with arbitrary lines/planes• See text for clipping in 4D homogenized coordinates

Polygon Clipping• Avoid drawing parts of primitives outside window• Points • Lines Polygons• etc.

2D Screen Coordinates

Window

Polygon Clipping• Find the part of a polygon inside the clip window?

Before Clipping

Page 19: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Polygon Clipping• Find the part of a polygon inside the clip window?

After Clipping

• Clip to each window boundary one at a time

Sutherland-Hodgman Clipping

• Clip to each window boundary one at a time

Sutherland-Hodgman Clipping

Page 20: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

• Clip to each window boundary one at a time

Sutherland-Hodgman Clipping

Sutherland-Hodgman Clipping• Clip to each window boundary one at a time

Sutherland-Hodgman Clipping• Clip to each window boundary one at a time

Page 21: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Triangle Clip/Split

61

a

b

c

-

+

-

•One clipping plane at a time

•Want to remove triangle, keep triangle, or clip and produce smaller triangles

•Depends on number of vertices outside clipping plane:• 0: Keep triangle• 3: Remove triangle• 2: Replace with ONE triangle• 1: Replace with TWO triangles

Triangle Clip/Split

62

a

b

c

-

+

-a

b

c

-

+

-a

b

c

-

+

-

Double vertices if you want separation...

63

Polygon Clipping

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 22: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

Page 23: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

P’

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

P’

Page 24: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

P’

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P5

P4

P3

P’ P’’

Clipping to a Boundary•Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

P1P2

P’ P’’

Page 25: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

73

Polygon Clip to Convex Domain

Polygon Clip to Convex Domain

74

Note double edges.

• Bad for shadows!

75

Polygon Clip to Convex Domain• Convex domain defined by collection of planes (or lines or

hyper-planes)

• Planes have outward pointing normals

• Clip against each plane in turn

• Check for early/trivial rejection

Page 26: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

76

General Polygon Clipping

A B A−B

B−A

A∩B

A∪B

77

•Weiler-Atherton Algorithm• Double edges

General Polygon Clipping

78

•Weiler-Atherton Algorithm

General Polygon Clipping• Solid = inside

•Dashed = outside

Page 27: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

79

•Weiler-Atherton Algorithm

General Polygon Clipping

Neither

PolygonInside

ClippingPlane Polygon

Outside

•New borders determine ownership

• Replace one polygon with a clipping plane to separates the other one into inside and outside portions

80

•Weiler-Atherton Algorithm

General Polygon Clipping

Neither

PolygonInside

ClippingPlane Polygon

Outside

• Pros: Allows for shadow calculations with off-screen polygons

• Cons: Slower than Sutherland-Hodgman

81

Hidden Surface Removal

• True 3D to 2D projection would put every thing overlapping into the view plane.

•We need to determine what’s in front and display only that.

Page 28: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

82

Z-Buffers• Add extra depth channel to image

•Write Z values when writing pixels

• Test Z values before writing

Images from Okan Arikan

83

Z-Buffers• Benefits• Easy to implement• Works for most any geometric primitive• Parallel operation in hardware

• Limitations• Quantization and aliasing artifacts• Overfill• Transparency does not work well

84

Z-Buffers• Transparency requires partial sorting:

Partiallytransparent

Opaque

Opaque 1st

2nd

3rdFront

Good

Partiallytransparent

Opaque

Opaque 2nd

3rd

1st

Not Good

Page 29: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

85

A-Buffers• Store sorted list of “fragments” at each pixel

•Draw all opaque stuff first then transparent

• Stuff behind full opacity gets ignored

•Nice for antialiasing...

86

Scan-line Algorithm

• Assume polygons don’t intersect

• Each time an edge is crossed determine who’s on top

87

Painter’s Algorithm• Sort Polygons Front-to-Back• Draw in order• Back-to-Front works also, but wasteful

• How to sort quickly?

• Intersecting polygons?

• Cycles?

Page 30: CS-184: Computer Graphics - University of California, Berkeleyvis.berkeley.edu/courses/cs184-fa10/wiki/images/7/78/Clipping.pdf · 5 Clipping •Stuff outside ... •Too far: •Complexity

88

BSP-Trees• Binary Space Partition Trees• Split space along planes• Allows fast queries of some spatial relations

•Draw Front-to-Back• Draw same-side polygons first• Draw root node polygon (if any)• Draw other-side polygons last