Professional Documents
Culture Documents
Clipping
D
A
C
B
Clipping
Analytically calculating the portions of primitives
within the viewport
Why Clip?
Bad idea to rasterize outside of framebuffer
bounds
Also, dont waste time scan converting pixels
outside window
Clipping
The nave approach to clipping lines:
for each line segment
for each edge of viewport
find intersection point
pick nearest point
D
C
Trivial Accepts
Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is
entirely inside the viewport?
A: test both endpoints.
Trivial Rejects
How can we know a line is outside viewport?
A: if both endpoints on wrong side of same edge,
can trivially reject line
1000
1010
0001
0000
0010
0101
0100
0110
C
B
A
C
B
A
Cohen-Sutherland Review
Use opcodes to quickly eliminate/include lines
Best algorithm when trivial accepts/rejects are common
Must compute viewport clipping of remaining lines
Non-trivial clipping cost
Redundant clipping of some lines
t<0
Defines line before P0
t>1
Defines line after P1
Viewport Edge L:
xL = xL0 + (xL1 - xL0) tL
yL = yL0 + (yL1 - yL0) tL
Cyrus-Beck Algorithm
We wish to optimize line/line intersection
Start with parametric equation of line:
P(t) = P0 + (P1 - P0) t
And a point and normal for each edge
P L , NL
Cyrus-Beck Algorithm
Find t such that
PL
NL [P(t) - PL] = 0
P(t)
P0
Inside
NL
P1
Cyrus-Beck Algorithm
Compute t for line intersection with all four edges
Discard all (t < 0) and (t > 1)
Classify each remaining intersection as
Potentially Entering (PE)
Potentially Leaving (PL)
Cyrus-Beck Algorithm
Compute PE with largest t
Compute PL with smallest t
Clip to these two points
PL
PE
P0
PE
PL
P1
Cyrus-Beck Algorithm
Because of horizontal and vertical clip lines:
Many computations reduce
Comparison
Cohen-Sutherland
Repeated clipping is expensive
Best used when trivial acceptance and rejection is possible for most lines
Cyrus-Beck
Computation of t-intersections is cheap
Computation of (x,y) clip points is only done once
Algorithm doesnt consider trivial accepts/rejects
Best when many lines must be clipped
Clipping Polygons
Clipping polygons is more complex than clipping
the individual lines
Input: polygon
Output: original polygon, new polygon, or nothing
triangle triangle
triangle quad
triangle 5-gon
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the viewport edges equation
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping:
The Algorithm
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Input/output for algorithm:
Input: list of polygon vertices in order
Output: list of clipped poygon vertices consisting of
old vertices (maybe) and new vertices (maybe)
Sutherland-Hodgeman Clipping
Sutherland-Hodgman basic routine:
Go around polygon one vertex at a time
Current vertex has position p
Previous vertex had position s, and it has been added to
the output if appropriate
Sutherland-Hodgeman Clipping
Edge from s to p takes one of four cases:
(Orange line can be a line or a plane)
inside
outside
inside
outside
inside
p
p output
s
i output
outside
inside
s
no output
i output
p output
outside
Sutherland-Hodgeman Clipping
Four cases:
s inside plane and p inside plane
Add p to output
Note: s has already been added
s inside plane and p outside plane
Find intersection point i
Add i to output
s outside plane and p outside plane
Add nothing
s outside plane and p inside plane
Find intersection point i
Add i to output, followed by p
Point-to-Plane test
A very general test to determine if a point p is inside
a plane P, defined by q and n:
(p - q) n < 0:
p inside P
(p - q) n = 0:
p on P
(p - q) n > 0:
p outside P
q
n
p
P
q
n
p
P
p
P
Line-Plane Intersections
Again, lots of opportunity for optimization