computer graphics inf4/msc 1 computer graphics lecture 7 scanline algorithm and polygon clipping...
TRANSCRIPT
Computer Graphics Inf4/MSc
1
Computer Graphics
Lecture 7
Scanline algorithm and polygon clipping
Taku Komura
Computer Graphics Inf4/MSc
Today’s topics
• Scanline algorithm
• Clipping
Computer Graphics Inf4/MSc
Scanline algorithm
• Computing the barycentric coordinates for all the pixels inside the bounding box can
be costly
• We can try to scan only the pixels inside the polygon
Computer Graphics Inf4/MSc
Scanline algorithm
For each scan line:
1. Find the intersections of the scan line with all edges of the
polygon.
2 Sort the intersections by increasing x coordinate.
3 Fill in all pixels between pairs of intersections.
Can also deal with concave polygons
Computer Graphics Inf4/MSc Span extrema
• Only turn on pixels whose centers are interior to the polygon:
– Otherwise will intrude other adjacent polygons
• round up values on the left edge of a span, round down on the right edge
midpoint algorithm interior
Computer Graphics Inf4/MSc Edge Coherence
• Computing the intersections between scan lines and edges can be costly
• Use a method similar to the midpoint algorithm
• y = mx + b, x = (y – b) / m• At y = s, xs = (s – b ) / m
At y = s + 1, xs+1 = (s+1 - b) / m = xs + 1 / m• Incremental calculation: xs+1 = xs + 1 / m
Computer Graphics Inf4/MSc
Pseudo code of computing the left span extrema (m > 1)
}
}
/* Overflow*/
{ )( if
;
);( ePixel Writ
){;;(for
int
int
int
1
maxmin
minmax
minmax
minmax
minmax
or; denomenatnt - increme
;x
or denomenatincrement
numeratorincrement
x,y
yyyyy
rdenomenatoincrement
yyrdenomenato
xxnumerator
yy
xx
m
Computer Graphics Inf4/MSc Active Edge Table • A table of edges that are currently used to fill the polygon
• Scan lines are processed in increasing Y order.
• Polygon edges are sorted according to their minimum Y.
• When the current scan line reaches the lower endpoint of an edge it becomes active.
• When the current scan line moves above the upper endpoint, the edge becomes inactive.
Computer Graphics Inf4/MSc
•Active edges are sorted according to increasing X.
•Filling in pixels between left most edge intersection and stops at the second.
•Restarts at the third intersection and stops at the fourth.
Active Edge Table
Computer Graphics Inf4/MSc
Polygon fill rules(to ensure consistency)
• Horizontal edges: Do not include in edge table
• Vertices: If local max or min, then count twice, else count once.
• If pixel is on edge, only draw left / bottom edges
Computer Graphics Inf4/MSc
Today’s topics
• Scanline algorithm
• Clipping
Computer Graphics Inf4/MSc Clipping• We need to clip objects outside the canonical view volume
• Clipping lines (Cohen-Sutherland algorithm)
• Clipping polygons (Sutherland-Hodgman algorithm)
Computer Graphics Inf4/MScCohen-Sutherland algorithm
While (true) {
1. Check if the line segment is trivial accept/reject
2. Clip the edge and shorten }
Computer Graphics Inf4/MSc
What is a trivial accept / reject?
• Trivial acceptanceAll line vertices lie inside box accept.
Computer Graphics Inf4/MSc
All line vertices lie outside and on same side reject.
What is a trivial accept / reject?
Computer Graphics Inf4/MSc
Cohen-Sutherland 2D outcodes
– 4-bit code called: Outcode
– First bit : above top of window, y > ymax
– Second bit : below bottom, y < ymin
– Third bit : to right of right edge, x > xmax
– Fourth bit : to left of left edge, x < xmin
Computer Graphics Inf4/MSc
Cohen-Sutherland 2D outcodes
0000
0100
0001
1001 1000 1010
0010
01100101
Computer Graphics Inf4/MSc
Cohen-Sutherland 2D outcodes
0000
0100
0001
1001 1000 1010
0010
01100101
Both endpoint codes 0000, trivial acceptance, else:
Do logical AND of outcodes
Computer Graphics Inf4/MSc
0000
0100
0001
1001 1000 1010
0010
01100101
1000
00000000
Logical AND between codes for 2 endpoints,Reject line if non-zero – trivial rejection.
0001
Cohen-Sutherland 2D outcodes
Computer Graphics Inf4/MSc
0000
0100
0001
1001 1000 1010
0010
01100101
Logical AND between codes for 2 endpoints,Reject line if non-zero – trivial rejection.
What about this one?
Computer Graphics Inf4/MSc
Line Intersection.
• Clip the line by edges of the rectangle• Select a clip edge based on the outcode, split
and feed the new segment on the side of the rectangle back into algorithm
• Need to perform 4 intersection checks for each line.
Computer Graphics Inf4/MSc
Polygon Clipping
• Sutherland-Hodgman’s algorithm
• Polygons are clipped at each edge of the window while traversing the polygon
• Output : a list of vertices of the clipped polygon
Computer Graphics Inf4/MScSutherland-Hodgman’s algorithm
• The edges of the polygon are traversed
• The edges can be divided into four types
Inside Outside Inside Outside Inside Outside Inside Outside
Case 3No
output.Case 1
OutputVertex
Case 2.
OutputIntersection
Case 4
SecondOutput
FirstOutput
Computer Graphics Inf4/MScSutherland-Hodgman’s algorithm
• For each of the edges of the clipping rectangle
– For each edge of the polygon (connecting pi, pi+1)
• If case 1 add p+1 to the output
• If case 2 add interaction to output
• If case 4 add intersection and p+1 to outputInside Outside Inside Outside Inside Outside Inside Outside
Case 3No
output.
Case 1
OutputVertex
Case 2.
OutputIntersection
Case 4
SecondOutput
FirstOutput
Computer Graphics Inf4/MSc
Example
http://www.sunshine2k.de/stuff/Java/SutherlandHodgman/SutherlandHodgmanApplet.html
Computer Graphics Inf4/MSc
26
References
• Scanline algorithm
Foley et al., Chapter 3.5, 3.6• Clipping lines, polygons
– Foley et al. Chapter 3.12, 3.14
– http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/Haowei.Hsieh/mm.html