CSE 409:
Computer Graphics
Clipping
Dr. M. Mahfuzul Islam
Dr. M. Mahfuzul Islam
Dr. M. Mahfuzul Islam
Dr. M. Mahfuzul Islam
Why clip?
We don’t want to waste time rendering objects that are
We don’t want to waste time rendering objects that are
outside the
outside the
viewing window (or clipping window)
viewing window (or clipping window)
We don’t want to waste time rendering objects that are
We don’t want to waste time rendering objects that are
outside the
What is clipping?
Analytically calculating the portions of primitives
Analytically calculating the portions of primitives
within the view window
within the view window
Analytically calculating the portions of primitives
Analytically calculating the portions of primitives
within the view window
Clip to what?
View Window
Eye position (focal point)
Right
Back Towards
Up
Why Clip?
Bad idea to rasterize outside of framebuffer
Bad idea to rasterize outside of framebuffer
bounds
bounds
Also, don’t waste time scan converting pixels
Also, don’t waste time scan converting pixels
outside window
outside window
Bad idea to rasterize outside of framebuffer
Bad idea to rasterize outside of framebuffer
bounds
bounds
Also, don’t waste time scan converting pixels
Also, don’t waste time scan converting pixels
outside window
Clipping
The naïve approach to clipping lines:
The naïve approach to clipping lines:
for each line segment
for each line segment
for each edge of view_windowfor each edge of view_window
find intersection pointfind intersection point
pick “nearest” pointpick “nearest” point
if anything is left, draw itif anything is left, draw it
What do we mean by “nearest”?
What do we mean by “nearest”?
How can we optimize this?
How can we optimize this?
The naïve approach to clipping lines:
The naïve approach to clipping lines:
for each line segment
for each line segment
for each edge of view_windowfor each edge of view_window
find intersection pointfind intersection point
pick “nearest” pointpick “nearest” point
if anything is left, draw itif anything is left, draw it
What do we mean by “nearest”?
What do we mean by “nearest”?
How can we optimize this?
How can we optimize this?
A
B
C
Trivial Accepts
Big optimization: trivial accept/rejects
Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is
How can we quickly determine whether a line segment is
entirely inside the view window?
entirely inside the view window?
A: test both endpoints.
A: test both endpoints.
Big optimization: trivial accept/rejects
Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is
How can we quickly determine whether a line segment is
entirely inside the view window?
entirely inside the view window?
A: test both endpoints.
Trivial Rejects
How can we know a line is outside view window?
How can we know a line is outside view window?
A: if both endpoints on wrong side of
A: if both endpoints on wrong side of
same
same
edge,
edge,
can trivially reject line
can trivially reject line
How can we know a line is outside view window?
How can we know a line is outside view window?
A: if both endpoints on wrong side of
A: if both endpoints on wrong side of
same
same
edge,
edge,
can trivially reject line
Clipping Lines To Viewport
Combining trivial accepts/rejects
Combining trivial accepts/rejects
• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the view windowinside all edges of the view window
• Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the view outside the same edge of the view window
window
• Otherwise, reduce to trivial casesOtherwise, reduce to trivial cases by splitting into two segmentsby splitting into two segments
Combining trivial accepts/rejects
Combining trivial accepts/rejects
• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the view windowinside all edges of the view window • Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the view outside the same edge of the view
window
window
Cohen-Sutherland Line Clipping
• Divide Divide view windowview window into regions defined by into regions defined by windowwindow edges edges • Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode::
–Bit 1 indicates y-value of points are above yBit 1 indicates y-value of points are above ymaxmax
• Divide Divide view windowview window into regions defined by into regions defined by windowwindow edges edges
• Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode::
–Bit 1 indicates y-value of points are above yBit 1 indicates y-value of points are above ymaxmax
0000 0010
0001 1001
0101 0100
1000 1010
0110
ymax
Cohen-Sutherland Line Clipping
For each line segment
For each line segment
• Assign an outcode to each vertex Assign an outcode to each vertex • If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept
–Same as performing Same as performing if (bitwise if (bitwise OR = 0)OR = 0)
• ElseElse
–bitwise bitwise ANDAND vertex outcodes together vertex outcodes together –if result if result 0, trivial reject 0, trivial reject
For each line segment
For each line segment
• Assign an outcode to each vertex Assign an outcode to each vertex
• If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept
–Same as performing Same as performing if (bitwise if (bitwise OR = 0)OR = 0)
• ElseElse
–bitwise bitwise ANDAND vertex outcodes together vertex outcodes together
Cohen-Sutherland Line Clipping
• If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded
that one or both segments can be discarded
–Pick an edge of view window that the line crosses (Pick an edge of view window that the line crosses (how?how?)) –Intersect line with edge (Intersect line with edge (how?how?))
–Discard portion on wrong side of edge and assign new Discard portion on wrong side of edge and assign new outcode to new vertex
outcode to new vertex
–Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary
• If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so
that one or both segments can be discarded
that one or both segments can be discarded
–Pick an edge of view window that the line crosses (Pick an edge of view window that the line crosses (how?how?))
–Intersect line with edge (Intersect line with edge (how?how?))
–Discard portion on wrong side of edge and assign new Discard portion on wrong side of edge and assign new
outcode to new vertex
outcode to new vertex
Cohen-Sutherland Line Clipping
If line cannot be trivially accepted or rejected, subdivide
If line cannot be trivially accepted or rejected, subdivide
so that one or both segments can be discarded
so that one or both segments can be discarded
Pick an edge that the line crosses
Pick an edge that the line crosses
• Check against edges in same order each timeCheck against edges in same order each time
– For example: top, bottom, right, leftFor example: top, bottom, right, left
If line cannot be trivially accepted or rejected, subdivide
If line cannot be trivially accepted or rejected, subdivide
so that one or both segments can be discarded
so that one or both segments can be discarded
Pick an edge that the line crosses
Pick an edge that the line crosses
• Check against edges in same order each timeCheck against edges in same order each time
– For example: top, bottom, right, leftFor example: top, bottom, right, left
A
B
D E
Cohen-Sutherland Line Clipping
Intersect line with edge (
Intersect line with edge (
how?
how?
)
)
Intersect line with edge (
Intersect line with edge (
how?
how?
)
)
A
B
D E
Discard portion on wrong side of edge and assign outcode to
Discard portion on wrong side of edge and assign outcode to
new vertex
new vertex
Apply trivial accept/reject tests and repeat if necessary
Apply trivial accept/reject tests and repeat if necessary
Discard portion on wrong side of edge and assign outcode to
Discard portion on wrong side of edge and assign outcode to
new vertex
new vertex
Apply trivial accept/reject tests and repeat if necessary
Apply trivial accept/reject tests and repeat if necessary
Cohen-Sutherland Line Clipping
A
B
View Window Intersection Code
• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright
–yyintersectintersect = y = y11 + m(x + m(xrightright – x1) – x1)
where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))
• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom
–xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m – y1)/m
where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))
• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright
–yyintersectintersect = y = y11 + m(x + m(xrightright – x1) – x1)
where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))
• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom
–xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m – y1)/m
Cohen-Sutherland Review
• Use opcodes to quickly eliminate/include linesUse opcodes to quickly eliminate/include lines
– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common • Must compute viewing window clipping of remaining linesMust compute viewing window clipping of remaining lines
– Non-trivial clipping costNon-trivial clipping cost
– Redundant clipping of some linesRedundant clipping of some lines
More efficient algorithms exist
More efficient algorithms exist
• Use opcodes to quickly eliminate/include linesUse opcodes to quickly eliminate/include lines
– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common
• Must compute viewing window clipping of remaining linesMust compute viewing window clipping of remaining lines – Non-trivial clipping costNon-trivial clipping cost
– Redundant clipping of some linesRedundant clipping of some lines
More efficient algorithms exist
Solving Simultaneous Equations
Equation of a line
Equation of a line
• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b • Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0
• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11
–PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT
–x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t
–y(t) = yy(t) = y00 + (y + (y1 1 - y- y00) t) t
Equation of a line
Equation of a line
• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b • Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0
• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11
–PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT
–x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t
Parametric Line Equation
Describes a finite line
Describes a finite line
Works with vertical lines (like the viewport edge)
Works with vertical lines (like the viewport edge)
• 0 <=t <= 10 <=t <= 1
– Defines line between PDefines line between P00 and P and P11
• t < 0t < 0
– Defines line before PDefines line before P00
• t > 1t > 1
– Defines line after PDefines line after P11
Describes a finite line
Describes a finite line
Works with vertical lines (like the viewport edge)
Works with vertical lines (like the viewport edge)
• 0 <=t <= 10 <=t <= 1–Defines line between PDefines line between P00 and P and P11
• t < 0t < 0
–Defines line before PDefines line before P00
• t > 1t > 1
Parametric Lines and Clipping
Define each line in parametric form:
Define each line in parametric form:
• PP00(t)…P(t)…Pn-1n-1(t)(t)
Define each edge of view window in parametric
Define each edge of view window in parametric
form:
form:
• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)
Perform Cohen-Sutherland intersection tests
Perform Cohen-Sutherland intersection tests
using appropriate view window edge and line
using appropriate view window edge and line
Define each line in parametric form:
Define each line in parametric form:
• PP00(t)…P(t)…Pn-1n-1(t)(t)
Define each edge of view window in parametric
Define each edge of view window in parametric
form:
form:
• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)
Perform Cohen-Sutherland intersection tests
Perform Cohen-Sutherland intersection tests
using appropriate view window edge and line
Line / Edge Clipping Equations
Faster line clippers use parametric equations
Faster line clippers use parametric equations
Line 0:
Line 0:
• xx00 = x = x00 0
0 + (x + (x0011 - x - x0000) t) t00
• yy00 = y = y00 0
0 + (y + (y0011 - y - y0000) t) t00
x
x
00 00
+ (x
+ (x
0011- x
- x
0000) t
) t
0 = 0 =x
x
LL00+ (x
+ (x
LL11- x
- x
LL00) t
) t
LLy
y
00 00
+ (y
+ (y
0011- y
- y
0000) t
) t
0 = 0 =y
y
LL00+ (y
+ (y
LL11- y
- y
LL00) t
) t
LL• Solve for tSolve for t00 and/or t and/or tLL
Faster line clippers use parametric equations
Faster line clippers use parametric equations
Line 0:
Line 0:
• xx00 = x = x000
0 + (x + (x0011 - x - x0000) t) t00
• yy00 = y = y00 0
0 + (y + (y0011 - y - y0000) t) t00
x
x
00 00
+ (x
+ (x
0011- x
- x
0000) t
) t
0 = 0 =x
x
LL00+ (x
+ (x
LL11- x
- x
LL00) t
) t
LLy
y
00 00
+ (y
+ (y
0011- y
- y
0000) t
) t
0 = 0 =y
y
LL00+ (y
+ (y
LL11- y
- y
LL00) t
) t
LL• Solve for tSolve for t00 and/or t and/or tLL
View Window Edge L:
View Window Edge L:
• xxLL = x = xLL 0
0 + (x + (xLL11 - x - xLL00) t) tLL
• yyLL = y = yLL 0
0 + (y + (yLL11 - y - yLL00) t) tLL
View Window Edge L:
View Window Edge L:
• xxLL = x = xLL0
0 + (x + (xLL11 - x - xLL00) t) tLL
• yyLL = y = yLL
0
Cyrus-Beck Algorithm
We wish to optimize line/line intersection
We wish to optimize line/line intersection
• Start with parametric equation of line:Start with parametric equation of line:
–P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t
• And a point and normal for each edgeAnd a point and normal for each edge
–PPLL, N, NLL
We wish to optimize line/line intersection
We wish to optimize line/line intersection
• Start with parametric equation of line:Start with parametric equation of line:
–P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t
• And a point and normal for each edgeAnd a point and normal for each edge
Cyrus-Beck Algorithm
Find t such that
Find t such that
N
NLL [P(t) - P [P(t) - PLL] = 0] = 0
Substitute line equation for P(t):
Substitute line equation for P(t):
• NNLL [ [PP00 + (P + (P1 1 - P- P00) t) t - P - PLL] = 0] = 0
Solve for t
Solve for t
• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]
Find t such that
Find t such that
N
NLL [P(t) - P [P(t) - PLL] = 0] = 0
Substitute line equation for P(t):
Substitute line equation for P(t):
• NNLL [ [PP00 + (P + (P1 1 - P- P00) t) t - P - PLL] = 0] = 0
Solve for t
Solve for t
• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]
Cyrus-Beck Algorithm
Compute t for line intersection with all four edges
Compute t for line intersection with all four edges
Discard all (t < 0) and (t > 1)
Discard all (t < 0) and (t > 1)
Classify each remaining intersection as
Classify each remaining intersection as
• Potentially Entering (PE)Potentially Entering (PE) • Potentially Leaving (PL)Potentially Leaving (PL)
N
N
LL[P
[P
11- P
- P
00] > 0 implies PL
] > 0 implies PL
N
N
LL[P
[P
11- P
- P
00] < 0 implies PE
] < 0 implies PE
• Note that we computed this term when computing t so we can keep it Note that we computed this term when computing t so we can keep it around
around
Compute t for line intersection with all four edges
Compute t for line intersection with all four edges
Discard all (t < 0) and (t > 1)
Discard all (t < 0) and (t > 1)
Classify each remaining intersection as
Classify each remaining intersection as
• Potentially Entering (PE)Potentially Entering (PE)• Potentially Leaving (PL)Potentially Leaving (PL)
N
N
LL[P
[P
11- P
- P
00] > 0 implies PL
] > 0 implies PL
N
N
LL[P
[P
11- P
- P
00] < 0 implies PE
] < 0 implies PE
• Note that we computed this term when computing t so we can keep it Note that we computed this term when computing t so we can keep it
around
Compute PE with largest t
Compute PE with largest t
Compute PL with smallest t
Compute PL with smallest t
Clip to these two points
Clip to these two points
Cyrus-Beck Algorithm
PE
PL P1
PL
PE
Cyrus-Beck Algorithm
Because of horizontal and vertical clip lines:
Because of horizontal and vertical clip lines:
• Many computations reduceMany computations reduce
Normals: (-1, 0), (1, 0), (0, -1), (0, 1)
Normals: (-1, 0), (1, 0), (0, -1), (0, 1)
Pick constant points on edges
Pick constant points on edges
solution for t:
solution for t:
• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))
• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))
• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))
• (y(y00 - y - ytoptop) / -(y) / -(y11 - y - y00))
Because of horizontal and vertical clip lines:
Because of horizontal and vertical clip lines:
• Many computations reduceMany computations reduce
Normals: (-1, 0), (1, 0), (0, -1), (0, 1)
Normals: (-1, 0), (1, 0), (0, -1), (0, 1)
Pick constant points on edges
Pick constant points on edges
solution for t:
solution for t:
• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))
• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))
• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))
Comparison
Cohen-Sutherland Cohen-Sutherland• Repeated clipping is expensiveRepeated clipping is expensive
• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines
Cyrus-Beck Cyrus-Beck
• Computation of t-intersections is cheapComputation of t-intersections is cheap
• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once • Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects • Best when many lines must be clippedBest when many lines must be clipped
Liang-Barsky: Optimized Cyrus-Beck Liang-Barsky: Optimized Cyrus-Beck Nicholl et al.: Fastest, but doesn’t do 3D Nicholl et al.: Fastest, but doesn’t do 3D Cohen-Sutherland
Cohen-Sutherland
• Repeated clipping is expensiveRepeated clipping is expensive
• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines
Cyrus-Beck Cyrus-Beck
• Computation of t-intersections is cheapComputation of t-intersections is cheap
• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once • Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects • Best when many lines must be clippedBest when many lines must be clipped
Liang-Barsky: Optimized Cyrus-Beck Liang-Barsky: Optimized Cyrus-Beck
Clipping Polygons
Clipping polygons is more complex than clipping
Clipping polygons is more complex than clipping
the individual lines
the individual lines
• Input: polygonInput: polygon
• Output: original polygon, new polygon, or nothingOutput: original polygon, new polygon, or nothing
The biggest optimizer we had was trivial accept or
The biggest optimizer we had was trivial accept or
reject…
reject…
When can we trivially accept/reject a polygon as
When can we trivially accept/reject a polygon as
opposed to the line segments that make up the
opposed to the line segments that make up the
polygon?
polygon?
Clipping polygons is more complex than clipping
Clipping polygons is more complex than clipping
the individual lines
the individual lines
• Input: polygonInput: polygon
• Output: original polygon, new polygon, or nothingOutput: original polygon, new polygon, or nothing
The biggest optimizer we had was trivial accept or
The biggest optimizer we had was trivial accept or
reject…
reject…
When can we trivially accept/reject a polygon as
When can we trivially accept/reject a polygon as
opposed to the line segments that make up the
opposed to the line segments that make up the
polygon?
What happens to a triangle during clipping?
What happens to a triangle during clipping?
Possible outcomes
Possible outcomes
:
:
What happens to a triangle during clipping?
What happens to a triangle during clipping?
Possible outcomes
Possible outcomes
:
:
triangle triangle
Why Is Clipping Hard?
triangle quad triangle 5-gon
How many sides can a clipped triangle have?
How many sides?
Seven…
Seven…
Seven…
A really tough case:
A really tough case:
A really tough case:
A really tough case:
A really tough case:
A really tough case:
A really tough case:
A really tough case:
Why Is Clipping Hard?
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the view window individuallyConsider each edge of the view window individually
• Clip the polygon against the view window edge’s equationClip the polygon against the view window edge’s equation
Basic idea:
Basic idea:
• Consider each edge of the view window individuallyConsider each edge of the view window individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
Sutherland-Hodgman Clipping
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped
Basic idea:
Basic idea:
• Consider each edge of the viewport individuallyConsider each edge of the viewport individually
• Clip the polygon against the edge equationClip the polygon against the edge equation
Sutherland-Hodgman Clipping
Input/output for algorithm:
Input/output for algorithm:
• Input: list of polygon vertices in order Input: list of polygon vertices in order
• Output: list of clipped polygon vertices consisting of Output: list of clipped polygon vertices consisting of old vertices (maybe) and new vertices (maybe)
old vertices (maybe) and new vertices (maybe)
Note: this is exactly what we expect from the
Note: this is exactly what we expect from the
clipping operation against each edge
clipping operation against each edge
Input/output for algorithm:
Input/output for algorithm:
• Input: list of polygon vertices in order Input: list of polygon vertices in order
• Output: list of clipped polygon vertices consisting of Output: list of clipped polygon vertices consisting of
old vertices (maybe) and new vertices (maybe)
old vertices (maybe) and new vertices (maybe)
Note: this is exactly what we expect from the
Note: this is exactly what we expect from the
clipping operation against each edge
Sutherland-Hodgman Clipping
Sutherland-Hodgman basic routine:
Sutherland-Hodgman basic routine:
• Go around polygon one vertex at a timeGo around polygon one vertex at a time • Current vertex has position Current vertex has position pp
• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriate
the output if appropriate
Sutherland-Hodgman basic routine:
Sutherland-Hodgman basic routine:
• Go around polygon one vertex at a timeGo around polygon one vertex at a time
• Current vertex has position Current vertex has position pp
• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriate
Sutherland-Hodgman Clipping
Edge from
Edge from
s
s
to
to
p
p
takes one of four cases:
takes one of four cases:
(Orange line can be a line or a plane) (Orange line can be a line or a plane)
Edge from
Edge from
s
s
to
to
p
p
takes one of four cases:
takes one of four cases:
(Orange line can be a line or a plane)
(Orange line can be a line or a plane) inside outside
s
p
p output
inside outside
s p
no output
inside outside
s p
i output
inside outside
s p
Sutherland-Hodgman Clipping
Four cases: Four cases:
• ss inside plane and inside plane and pp inside plane inside plane
– Add Add pp to output to output
– Note: Note: ss has already been added has already been added
• ss inside plane and inside plane and pp outside plane outside plane
– Find intersection point Find intersection point ii
– Add Add ii to output to output
• ss outside plane and outside plane and pp outside planeoutside plane
– Add nothingAdd nothing
• ss outside plane and outside plane and pp inside plane inside plane
– Find intersection point Find intersection point ii
– Add Add ii to output, followed by to output, followed by pp
Four cases:
Four cases:
• ss inside plane and inside plane and pp inside plane inside plane
– Add Add pp to output to output
– Note: Note: ss has already been added has already been added
• ss inside plane and inside plane and pp outside plane outside plane
– Find intersection point Find intersection point ii
– Add Add ii to output to output
• ss outside plane and outside plane and pp outside planeoutside plane
– Add nothingAdd nothing
• ss outside plane and outside plane and pp inside plane inside plane
– Find intersection point Find intersection point ii
Point-to-Plane test
A very general test to determine if a point
A very general test to determine if a point
p
p
is “inside”
is “inside”
a plane
a plane
P
P
, defined by
, defined by
q
q
and
and
n
n
:
:
(
(pp - - qq) • ) • nn < 0: < 0: pp inside inside PP (
(pp - - qq) • ) • nn = 0: = 0: pp on on PP (
(pp - - qq) • ) • nn > 0: > 0: pp outside outside PP Remember: p
Remember: p • n = |p| |n| cos (• n = |p| |n| cos ())
= angle between p and n= angle between p and n
A very general test to determine if a point
A very general test to determine if a point
p
p
is “inside”
is “inside”
a plane
a plane
P
P
, defined by
, defined by
q
q
and
and
n
n
:
:
(
(pp - - qq) • ) • nn < 0: < 0: pp inside inside PP (
(pp - - qq) • ) • nn = 0: = 0: pp on on PP (
(pp - - qq) • ) • nn > 0: > 0: pp outside outside PP Remember: p
Remember: p • n = |p| |n| cos (• n = |p| |n| cos ())
= angle between p and n= angle between p and n
Finding Line-Plane Intersections
Edge intersects plane
Edge intersects plane
P
P
where
where
E
E
(t)
(t)
is on
is on
P
P
• q q is a point on is a point on PP
• nn is normal to is normal to PP
(
(LL(t) - q(t) - q) • ) • nn = 0 = 0
(L
(L00 + (L + (L1 1 - L- L00) t ) t - q- q) • ) • nn = 0 = 0
t
t = [( = [(q - Lq - L00) • ) • nn] / [(] / [(LL11 - L - L00) • ) • nn]]
• The intersection point The intersection point i = Li = L(t)(t) for this value of for this value of tt
Edge intersects plane
Edge intersects plane
P
P
where
where
E
E
(t)
(t)
is on
is on
P
P
• q q is a point on is a point on PP
• nn is normal to is normal to PP
(
(LL(t)(t) - q - q) • ) • nn = 0 = 0
(L
(L00 + (L + (L1 1 - L- L00) t ) t - q- q) • ) • nn = 0 = 0
t
t = [( = [(q - Lq - L00) • ) • nn] / [(] / [(LL11 - L - L00) • ) • nn]]