1 / 26

Computer Graphics

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

faylinn
Télécharger la présentation

Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture7 Scanline algorithm and polygon clipping Taku Komura Computer Graphics

  2. Today’s topics • Scanline algorithm • Clipping

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

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

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

  7. Pseudo code of computing the left span extrema (m > 1)

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

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

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

  11. Today’s topics • Scanline algorithm • Clipping

  12. Clipping • We need to clip objects outside the canonical view volume • Clipping lines (Cohen-Sutherland algorithm) • Clipping polygons (Sutherland-Hodgman algorithm)

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

  14. What is a trivial accept / reject? • Trivial acceptance All line vertices lie inside box  accept.

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

  16. 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. 1001 1000 1010 0001 0000 0010 0101 0100 0110 Cohen-Sutherland 2D outcodes

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

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

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

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

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

  25. Example http://www.sunshine2k.de/stuff/Java/SutherlandHodgman/SutherlandHodgmanApplet.html

  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

More Related