Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
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
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
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
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
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
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
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
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
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
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
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
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
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
(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
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
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)
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)
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
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
• 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
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
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
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’
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’’
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
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
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.
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
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?
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