Polygon Scan Conversion PDF

Polygon Scan Conversion PDF Polygon Scan Conversion PDF

students.cs.byu.edu
from students.cs.byu.edu More from this publisher

CS 455 – Computer Graphics<strong>Polygon</strong> <strong>Scan</strong> <strong>Conversion</strong>


Filling <strong>Polygon</strong>s• Need to fill the frame buffer.Want more than just line drawingsWant to fill polygons!• Rasterization takes a primitive (e.g., a triangle description)and determines which pixels to illuminate


Filling <strong>Polygon</strong>sFirst approach:1. <strong>Polygon</strong> <strong>Scan</strong>-<strong>Conversion</strong>Rasterize a polygon scan line by scan line, determining whichpixels to illuminate on each line


Goals• Fill polygons<strong>Polygon</strong> full, but no pixels outside the polygon• Handle adjacent polygons correctly:Avoid cracksAvoid overlapsIndependent of the order ofdrawing• Interpolating colors ortexturingDiscussed later


<strong>Polygon</strong> TypesConvexFor every pair of points within aconvex polygon, the line segmentconnecting them is also completelyenclosed within the polygonHorizontally convexSame definitionapplies for anytwo points onthe samehorizontal scanline.ConcaveUse the parity (in/out) rule to determine whether you’re in or out!


Why <strong>Polygon</strong>s?• <strong>Polygon</strong>s are ubiquitous:GamesVirtual environmentsEtc.• Graphics hardware is optimized for polygons (andespecially triangles)• <strong>Polygon</strong>s can approximate arbitrary shapes or surfaces:Local linear approximationTo improve quality add more edgesEllipse≈12-sided<strong>Polygon</strong>24-sided<strong>Polygon</strong>


Filling <strong>Polygon</strong>s• Start simple:Axis-aligned rectanglesTrianglesArbitrary polygons• Two key decisions:Which pixels to fill depends on the shape of the primitiveWhat value (color) to fill them with


Which Pixels to Fill?• Pixels entirely inside polygon Yes• Pixels partially inside ???Shared edges: want to avoid drawing pixels twiceRemember: a pixel is a sample, not a little square• Convention: only fill pixels whosecenters are within the polygon• Problem: pixel center lies exactlyon the polygon edgeDon’t draw Hole<strong>Polygon</strong> edgeOutsideInsideDraw Drawn twice (verynoticeable if drawn using XOR mode)No???Yes


Concepts for <strong>Scan</strong> <strong>Conversion</strong>• Span: adjacent pixels on one scan line that lie inside the polygonfrom left to right.• Primary idea behind rasterization: taking successive scanlines and determining which spans of pixels to illuminate• Exploiting coherence: looking only for those pixels at whichchange occursSpatial: primitives are similar from pixel to pixel within a span or fromscan line to scan lineSpan: the pixels within a span are set to the same value (for solid coloring)<strong>Scan</strong>-line: the spans in consecutive scan lines are similarEdge: consecutive scan lines are likely to intersect the same edges


Coherence<strong>Scan</strong>-lineEdgeSpan


<strong>Polygon</strong> <strong>Scan</strong> <strong>Conversion</strong>• Process scan lines one at a time, left to right• For each line, we need to determine:<strong>Polygon</strong> EdgesWhen we are interior and when we are exterior• How? (assume the entire polygon is on the screen)1. Determine all points on a scan line that intersect a polygonedge2. Sort the intersection points from left to right3. Group the intersection points in pairs and fill the pixelsbetween the pairs


<strong>Polygon</strong> <strong>Scan</strong> <strong>Conversion</strong>Intersection PointsOther points in the span


Determining Inside vs. Outside• Use the odd-parity ruleSet parity even initiallyInvert parity at each intersection pointDraw pixels when parity is odd, do not draw when it is even• How do we count vertices, i.e., do we invert parity whena vertex falls exactly on a scan line?


Vertices and Parity• How do we count the intersecting vertex in the paritycomputation?<strong>Scan</strong> line?????


Vertices and Parity• We need to either count it 0 times, or 2 times to keep paritycorrect.• What about:<strong>Scan</strong> line• We need to count thisvertex once?????


Vertices and Parity• If we count a vertex as one intersection, the secondpolygon gets drawn correctly, but the first does not.• If we count a vertex as zero or two intersections, the firstpolygon gets drawn correctly, but the second does not.• How do we handle this?Count only vertices that are the y minvertex for that line


Vertices and Parity• Both cases now work correctly


Horizontal Edges• How do we deal with horizontal edges????Don’t count their vertices in the parity calculation!


Top Spans of <strong>Polygon</strong>s• Effect of only counting y min:Top spans of polygons are not drawnIf two polygons share this edge,it is not a problem.What about if this is the only polygon with that edge?


Shared <strong>Polygon</strong> Edges• What if two polygons share an edge?Draw Last polygon winsOrange last• Solution:Span is closed on left andBlue lastopen on right (x min≤ x < x max)<strong>Scan</strong> lines closed on bottom andopen on top (y min≤ y < y max)


General Pixel Ownership Rule• Half-plane rule:A boundary pixel (whose center falls exactly on an edge) is notconsidered part of a primitive if the half plane formed by theedge and containing the primitive lies to the left or below theedge.Applies to arbitrary polygonsas well as to rectangles....• Consequences:Shared edgeSpans are missing the right-most pixelEach polygon is missing its top-most span


<strong>Polygon</strong> <strong>Scan</strong>-<strong>Conversion</strong>• Process for scan converting a polygonLook at one polygon at a timeStore information about every polygon edgeCompute spans for each scan lineDraw the pixels between the spans• This can be optimized using an “edge table”• One entry in the edge table for each edge of a polygon• Perform scan conversion one scan line at a time• For each new scan line, consult the edge table todetermine if any new edges are intersected by the scanline


Computing Intersections• For each scan line, we compute the intersection betweenthat scan line and the polygon edges• It is expensive to do a complete line-line intersectioncomputation each time• Once we compute an intersection between a scan lineand an edge, let’s use that information in the nextiteration


<strong>Scan</strong> Line IntersectionIntersectionpointsneeded<strong>Polygon</strong> EdgesCurrent<strong>Scan</strong> liney i+1Intersection pointsFrom previous<strong>Scan</strong> linePrevious<strong>Scan</strong> liney i


<strong>Scan</strong> Line IntersectionUse edge coherence to incrementally update the xintersections to save computationWe knowy=mx + b,m=yx11−−yx00Each new scan line is 1 greater in y, soy i + 1 = yi+1We need to compute x for a given scan line,x =y − bm


Active Edge Table• We will use this coherence in an “active edge table”(AET)• This table keeps one entry for each edge intersected bythe current scan line• At each new scan line:Compute new intersections for all edges using the abovecoherenceAdd any new edges intersectedRemove any edges no longer intersected• To efficiently update the AET, we will maintain a globaledge table (GET)


Global Edge Table• The GET will contain information about all edges in thepolygon• The GET keeps one bucket for each scan line• Each bucket maintains a list of edges that have the scanline y value as their y minvalue• There is one entry in the GET for each polygon edge• Each entry in the GET containsThe y maxvalue for that edgeThe x @yminvalue (the x value at the y minpoint)The x increment value (1/m)


Global Edge Table Example• For the given polygon:876543210eadbc0 1 2 3 4 5 6 7 8(a, b) = [(2, 1), (3, 5)](b, c) = [(3, 5), (6, 6)](c, d) = [(6, 6), (3, 8)](d, e) = [(3, 8), (0, 4)](e, a) = [(0, 4), (2, 1)]GET Entries (y max, x @ymin, 1/m)(a, b)(b, c)(c, d)(d, e)(5, 2, ¼)(6, 3, 3)(8, 6, -3/2)(8, 0, 3/4)(e, a) (4, 2, -2/3)


Global Edge Table Example• These entries are then placed into the ET based on theedge y minvalues876543210eabdc0 1 2 3 4 5 6 7 8(a, b) = [(2, 1), (3, 5)](b, c) = [(3, 5), (6, 6)](c, d) = [(6, 6), (3, 8)](d, e) = [(3, 8), (0, 4)](e, a) = [(0, 4), (2, 1)]876543210ET8 6 -3/26 3 38 0 3/45 2 ¼4 2 -2/3


Active Edge Table Example• For each scan line, you maintain the active edge tablewhich contains information about all edges intersectedby the current scan line• Entries areThe y maxvalue for that edgeThe x value for the intersection of the polygon edge with thisscan lineThe x increment value (1/m)


Active Edge Table Example• AET for scanline 3876543210eabdc0 1 2 3 4 5 6 7 8(a, b) = [(2, 1), (3, 5)](b, c) = [(3, 5), (6, 6)](c, d) = [(6, 6), (3, 8)](d, e) = [(3, 8), (0, 4)](e, a) = [(0, 4), (2, 1)]AET<strong>Scan</strong> Line 34 2/3 -2/35 5/2 1/4


Active Edge Table Example• AET for scanline 4876543210eabdc0 1 2 3 4 5 6 7 8(a, b) = [(2, 1), (3, 5)](b, c) = [(3, 5), (6, 6)](c, d) = [(6, 6), (3, 8)](d, e) = [(3, 8), (0, 4)](e, a) = [(0, 4), (2, 1)]AET<strong>Scan</strong> Line 48 0 3/45 11/4 1/4


Active Edge Table Example• AET for scanline 5876543210eabdc0 1 2 3 4 5 6 7 8(a, b) = [(2, 1), (3, 5)](b, c) = [(3, 5), (6, 6)](c, d) = [(6, 6), (3, 8)](d, e) = [(3, 8), (0, 4)](e, a) = [(0, 4), (2, 1)]AET<strong>Scan</strong> Line 58 3/43/46 3 3


One More Example• What is the global edge table for this polygon?First: edges876543210cab0 1 2 3 4 5 6 7 8(a, b) = ?(b, c) = ?(c, a) = ?


One More Example• What is the global edge table for this polygon?Third: GET EntriesGET Entries (y max, x @ymin, 1/m)8b76543210ca0 1 2 3 4 5 6 7 8(a, b) = [(3, 3), (7, 8)](b, c) = [(7, 8), (1, 6)](c, a) = [(1, 6), (3, 3)](8, 3, 4/5)(8, 1, 3)(6, 3, -2/3)


One More Example• What is the global edge table for this polygon?ETThird: Place entries into GET876543210cab0 1 2 3 4 5 6 7 8(a, b) = [(3, 3), (7, 8)](b, c) = [(7, 8), (1, 6)](c, a) = [(1, 6), (3, 3)](8, 3, 4/5)(8, 1, 3)(6, 3, -2/3)876543210


One More Example• What is the global edge table for this polygon?ETThird: Place entries into GET876543210cab0 1 2 3 4 5 6 7 8(a, b) = [(3, 3), (7, 8)](b, c) = [(7, 8), (1, 6)](c, a) = [(1, 6), (3, 3)](8, 3, 4/5)(8, 1, 3)(6, 3, -2/3)8765432108 1 36 3 -2/38 3 4/5


<strong>Scan</strong> Line Algorithm1. Set the Global Edge Table (ET) to include all edges ofthe polygon2. Set Y to be the smallest y coordinate that has an entryin the ET3. Initialize the Active Edge Table (AET) to be empty4. Repeat until the AET and ET are empty:a. Add edges from the ET to the AET in which y min= Yb. Remove edges from the AET in which y max= Yc. Sort AET on xd. Fill pixels between pairs of intersections in the AETe. For each edge in the AET, replace x with x + 1/mf. Set Y = Y + 1 to move to the next scan line


Filling Techniques• The second approach to polygon fill is using a fillingtechnique, rather than scan conversion• Pick a point inside the polygon, then fill neighboringpixels until the polygon boundary is reached• Boundary Fill Approach:Draw polygon boundary in the frame bufferDetermine an interior pointStarting at the given point, do- If the point is not the boundary color or the fill colorSet this pixel to the fill colorPropagate to the pixel’s neighbors and continue


Filling Techniques• Flood Fill Approach:Set all interior pixels to a certain colorThe boundary can be any other colorPick an interior point and set it to the polygon colorPropagate to neighbors, as long as the neighbor is the interiorcolor• This is used for regions with multi-colored boundariesRegion to be filled


Propagating to Neighbors• Most frequently used approaches:4-connected area8-connected area4-connected8-connected


Fill Problems• Fill algorithms have potential problems• E.g., 4-connected area fill:Starting pointFill complete


Fill Problems• Similarly, 8-connected can “leak” over to anotherpolygon• Another problem: the algorithm is highly recursiveCan use a stack of spans to reduce amount of recursion


Pattern Filling• Often we want to fill a region with a pattern, not just acolor• Define an n by m pixmap (or bitmap) that we wish toreplicate across the region5x4 pixmapObject to bepatternedFinal patternedobject


Pattern Filling• How do you determine the anchor pointA point on the polygon- Left-most point?- The pattern will move with the polygon- Difficult to decide the right anchor pointScreen (or window) origin- Easier to determine anchor point- The pattern does not move with the object


Pattern Filling• How do we determine which color to color a point in theobject?• Use the MOD function to tile the pattern across thepolygon• For point (x, y)Use the pattern color located at (x MOD m, y MOD n)


225235Pattern Example• For the pattern shown, what color does the pixel atlocation (235, 168) get colored, assuming the pattern isanchored at the lower left corner of the object?Pattern168???163


225235Pattern Example168???PatternThe pattern is5x4163Need to find the relative distance to the point to draw:X = (235 – 225) = 10Y = (168 – 163) = 5Next figure out which pattern pixel corresponds to this screen pixel:X pattern= 10 MOD 4 = 2Y pattern= 5 MOD 5 = 0


225235Pattern Example• The pattern pixel (2, 0) should map to screen location(235, 168)(2, 0)168???Let’s map thepattern onto thepolygon and see163(0, 0) patternlocation


Triangles• Triangles are a special case of polygon• Triangles are minimal:Specified with three points: (x 0, y 0), (x 1, y 1), (x 2, y 0)Or three edge equations:a 0x + b 0y + c 0= 0, a 1x + b 1y + c 1= 0, a 2x + b 2y + c 2= 0• 3-D triangles are always planarp 0p 1Three 3-D points define a plane• As such, triangles are verysimple (mathematically andgeometrically)p 2e 2,0 e0,1e 1,2


Triangles (cont.)• Always convex: No matter how you rotate a triangle, it onlyhas one span per scan line• Arbitrary polygons can be decomposed into trianglesConvexConcave


Triangles (cont.)• Rasterization algorithms can take advantage of triangleproperties• Graphics hardware is optimized for triangles• Because triangle drawing is so fast, many systems willsubdivide polygons into triangles prior to scanconversion

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!