300 likes | 709 Views
Lecture 7 Scanline algorithm and polygon clipping Taku Komura. Computer Graphics. Today’s topics. Scanline algorithm Clipping . Scanline algorithm. Computing the barycentric coordinates for all the pixels inside the bounding box can be costly
E N D
Lecture7 Scanline algorithm and polygon clipping Taku Komura Computer Graphics
Today’s topics • Scanline algorithm • Clipping
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
Scanline algorithm For each scan line: • Find the intersections of the scan line withall edges of the polygon. 2 Sort the intersections by increasing xcoordinate. 3 Fill in all pixels between pairs of intersections. Can also deal with concave polygons
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 theright edge midpoint algorithm interior
EdgeCoherence • 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
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 lowerendpoint of an edge itbecomes active. • When the current scan line moves above the upper endpoint,the edge becomes inactive.
Active Edge Table • Active edges are sorted according to increasing X. • Filling in pixels between leftmost edge intersection • and stops at the second. • Restarts at the third intersection and stops at the fourth.
Polygon fill rules(to ensure consistency) • Horizontal edges: Do not include in edge table • Vertices: If local max or min, then count twice, else countonce. • If pixel is on edge, only draw left / bottom edges
Today’s topics • Scanline algorithm • Clipping
Clipping • We need to clip objects outside the canonical view volume • Clipping lines (Cohen-Sutherland algorithm) • Clipping polygons (Sutherland-Hodgman algorithm)
Cohen-Sutherland algorithm While (true) { • Check if the line segment is trivial accept/reject 2. Clip the edge and shorten }
What is a trivial accept / reject? • Trivial acceptance All line vertices lie inside box accept.
What is a trivial accept / reject? All line vertices lie outside and on same side reject.
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
1001 1000 1010 0001 0000 0010 0101 0100 0110 Cohen-Sutherland 2D outcodes
1001 1000 1010 0000 0001 0010 0100 0101 0110 Cohen-Sutherland 2D outcodes Both endpoint codes 0000, trivial acceptance, else: Do logical AND of outcodes
Cohen-Sutherland 2D outcodes 1001 1010 1000 1000 0001 0000 0010 0001 0000 0000 0100 0101 0110 Logical AND between codes for 2 endpoints, Reject line if non-zero – trivial rejection.
What about this one? 1000 1001 1010 0000 0010 0001 0100 0101 0110 Logical AND between codes for 2 endpoints, Reject line if non-zero – trivial rejection.
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.
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
Inside Outside Inside Outside Inside Outside Inside Outside First Output Output Vertex Output Intersection Second Output Case 3 No output. Case 1 Case 2. Case 4 Sutherland-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 First Output Output Vertex Output Intersection Second Output Case 3 No output. Case 1 Case 2. Case 4 Sutherland-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 output
Example http://www.sunshine2k.de/stuff/Java/SutherlandHodgman/SutherlandHodgmanApplet.html
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