Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura.

Similar presentations


Presentation on theme: "Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura."— Presentation transcript:

1 Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura

2 Computer Graphics Inf4/MSc Today’s topics Scanline algorithm Clipping

3 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

4 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

5 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

6 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, x s = (s – b ) / m At y = s + 1, x s+1 = (s+1 - b) / m = x s + 1 / m Incremental calculation: x s+1 = x s + 1 / m

7 Computer Graphics Inf4/MSc Pseudo code of computing the left span extrema (m > 1)

8 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.

9 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

10 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

11 Computer Graphics Inf4/MSc Today’s topics Scanline algorithm Clipping

12 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)

13 Computer Graphics Inf4/MSc Cohen-Sutherland algorithm While (true) { 1.Check if the line segment is trivial accept/reject 2. Clip the edge and shorten }

14 Computer Graphics Inf4/MSc What is a trivial accept / reject? Trivial acceptance All line vertices lie inside box  accept.

15 Computer Graphics Inf4/MSc All line vertices lie outside and on same side  reject. What is a trivial accept / reject?

16 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

17 Computer Graphics Inf4/MSc Cohen-Sutherland 2D outcodes 0000 0100 0001 1001 10001010 0010 01100101

18 Computer Graphics Inf4/MSc Cohen-Sutherland 2D outcodes 0000 0100 0001 1001 10001010 0010 01100101 Both endpoint codes 0000, trivial acceptance, else: Do logical AND of outcodes

19 Computer Graphics Inf4/MSc 0000 0100 0001 1001 1000 1010 0010 01100101 1000 0000 Logical AND between codes for 2 endpoints, Reject line if non-zero – trivial rejection. 0001 Cohen-Sutherland 2D outcodes

20 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?

21 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.

22 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

23 Computer Graphics Inf4/MSc Sutherland-Hodgman’s algorithm The edges of the polygon are traversed The edges can be divided into four types InsideOutsideInsideOutsideInsideOutsideInsideOutside Case 3 No output. Case 1 Output Vertex Case 2. Output Intersection Case 4 Second Output First Output

24 Computer Graphics Inf4/MSc 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 InsideOutsideInsideOutsideInsideOutsideInsideOutside Case 3 No output. Case 1 Output Vertex Case 2. Output Intersection Case 4 Second Output First Output

25 Computer Graphics Inf4/MSc Example http://www.sunshine2k.de/stuff/Java/SutherlandHodgman/SutherlandHodgmanApplet.html

26 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


Download ppt "Computer Graphics Inf4/MSc 1 Computer Graphics Lecture 7 Scanline algorithm and polygon clipping Taku Komura."

Similar presentations


Ads by Google