Scan Conversion of Polygons Dr. Scott Schaefer 1 Drawing Rectangles Which pixels should be filled? 2/70 Drawing Rectangles Is this correct? 3/70 Drawing Rectangles What if two rectangles overlap? 4/70 Drawing Rectangles Is this correct? 5/70 Drawing Rectangles Is this correct? Overlap!!! 6/70 Drawing Rectangles Solution: Exclude pixels on top and right 7/70 Drawing Rectangles Artifacts are possible 8/70 General Polygons – Basic Idea Intersect scan lines with edges Find ranges along x Fill interior of those ranges Don’t fill top/right Edges may NOT match with line drawing algo 9/70 General Polygons – Basic Idea Intersect scan lines with edges Find ranges along x Fill interior of those ranges Use coherence to speed up Don’t fill top/right Edges may NOT match with line drawing algo 10/70 General Polygons – Basic Idea Intersect scan lines with edges Find ranges along x Fill interior of those ranges Use coherence to speed up Edges intersection one scan line are mostly same as those intersecting previous scan line 11/70 General Polygons – Basic Idea Intersect scan lines with edges Find ranges along x Fill interior of those ranges Use coherence to speed up The x-value of an intersection with one scan line is close to the intersection with the previous one 12/70 General Polygons – Data Structures Edge: ( x i , y i ) ( x i 1 , y i 1 ) Edge maxY currentX xIncr maxY: max( y i , y i 1 ) y i min( y i , y i 1 ) xi , currentX: x i 1 , otherwise xIncr: x i 1 x i y i 1 y i 13/70 General Polygons – Data Structures Edge: ( x i , y i ) ( x i 1 , y i 1 ) Edge maxY currentX xIncr maxY: max( y i , y i 1 ) y i min( y i , y i 1 ) xi , currentX: x i 1 , otherwise xIncr: x i 1 x i y i 1 y i Horizontal edges will not be used!!! 14/70 General Polygons – Data Structures Scan Lines Active Edge Table: 7 6 5 4 3 2 1 0 Store a linked-list per scanline. Insert edges into table at scan-line associated with lowest end-point. Edges 15/70 General Polygons – Data Structures Active Edge List: Edges List of all edges intersecting current scan-line sorted by their x-values 16/70 General Polygons – Algorithm line = 0 While (line < height ) Add edges to Active Edge List from Active Edge Table starting at line Remove edges that end at line Fill pixels Increment x-values on edges in Active Edge List Increment line 17/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 Active Edge List C 7 B 6 D 5 4 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 18/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 D 5 4 G 3 AB Active Edge List 2 1 F 0 E A 0 1 2 3 4 5 6 7 19/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D 5 4 G 3 AB Active Edge List 2 1 F 0 E A 0 1 2 3 4 5 6 7 20/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC CD D 5 4 G 3 AB Active Edge List 2 1 F 0 E A 0 1 2 3 4 5 6 7 21/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC CD D 5 4 ED AB Active Edge List G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 22/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC CD D 5 4 ED AB Active Edge List G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 23/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB Active Edge List G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 24/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 25/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB AG maxY 6 3 currentX 0 0 xIncr 0 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 3 26/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB AG FG ED maxY 6 3 3 5 currentX 0 2 3 5 xIncr 0 2 3 1 3 G F 0 E A 0 1 2 3 4 5 6 7 1 2 2 27/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB AG FG ED maxY 6 3 3 5 currentX 0 2 3 5 xIncr 0 2 3 1 3 G F 0 E A 0 1 2 3 4 5 6 7 1 2 2 28/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB AG FG ED maxY 6 3 currentX 0 4 3 2 12 5 5 12 xIncr 0 2 3 1 3 G F 0 E A 0 1 2 3 4 5 6 7 1 2 2 29/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB AG FG ED maxY 6 3 currentX 0 4 3 2 12 5 5 12 xIncr 0 2 3 1 3 G F 0 E A 0 1 2 3 4 5 6 7 1 2 2 30/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB AG FG ED maxY 6 3 3 5 currentX 0 2 2 6 xIncr 0 2 1 1 3 G 2 F 0 E A 0 1 2 3 4 5 6 7 2 31/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB ED maxY 6 5 currentX 0 6 xIncr 0 1 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 2 32/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB ED maxY 6 5 currentX 0 6 xIncr 0 1 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 2 33/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB ED maxY 6 currentX 0 5 6 12 xIncr 0 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 1 2 34/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB ED maxY 6 currentX 0 5 6 12 xIncr 0 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 1 2 35/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB ED CD maxY 6 5 currentX 0 7 7 7 xIncr 0 1 2 G F 0 E A 0 1 2 3 4 5 6 7 2 36/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG 3 2 1 Active Edge List AB ED CD maxY 6 7 currentX 0 5 6 12 xIncr 0 1 2 G F 0 E A 0 1 2 3 4 5 6 7 7 2 37/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB CD maxY 6 7 currentX 0 7 xIncr 0 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 38/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List AB CD maxY 6 7 currentX 0 7 xIncr 0 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 39/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG G 3 2 1 Active Edge List AB BC CD maxY 6 7 7 currentX 0 0 5 xIncr 0 3 2 F 0 E A 0 1 2 3 4 5 6 7 40/70 General Polygons – Example C Active Edge Table 7 6 5 4 3 2 1 0 7 B 6 BC D CD 5 4 FG ED AB AG G 3 2 1 Active Edge List AB BC CD maxY 6 7 7 currentX 0 0 5 xIncr 0 3 2 F 0 E A 0 1 2 3 4 5 6 7 41/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List BC CD maxY 7 7 currentX 0 5 xIncr 3 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 42/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List BC CD maxY 7 7 currentX 0 5 xIncr 3 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 43/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List BC CD maxY 7 7 currentX 3 3 xIncr 3 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 44/70 General Polygons – Example Active Edge Table 7 6 5 4 3 2 1 0 C 7 B 6 BC D CD 5 4 FG ED AB AG Active Edge List BC CD maxY 7 7 currentX 3 3 xIncr 3 2 G 3 2 1 F 0 E A 0 1 2 3 4 5 6 7 45/70 General Polygons – Problems Sliver polygons may not be drawn correctly No simple solution Long, thin triangles cause problems Want triangles with good aspect ratio (close to equilateral) 46/70 Boundary Fill Start with drawn outline of a polygon and an interior point Recursively recolor outward from that point • If neighbor different, then recolor and recur Everything within the boundary is changed to that color 47/70 Boundary Fill How to define a neighbor? 4-connected 8-connected 48/70 Boundary Fill – Example 49/70 Boundary Fill – Example 50/70 Boundary Fill – Example 51/70 Boundary Fill – Example 52/70 Boundary Fill – Example 53/70 Boundary Fill – Example 54/70 Flood Fill Start with a point Define color under that point as the interior color Recursively recolor outward from that point • If neighbor is interior color, then recolor and recur Contiguous regions of the same color are recolored 55/70 Flood Fill – Example 56/70 Flood Fill – Example 57/70 Flood Fill – Example 58/70 Flood Fill – Example 59/70 Flood Fill – Example 60/70 Flood Fill – Example 61/70 Flood Fill – Example 62/70 Flood Fill – Example 63/70 Flood Fill – Example 64/70 Flood Fill – Example 65/70 Flood Fill – Example 66/70 Flood Fill – Example 67/70 Flood Fill – Example 68/70 OpenGL: Drawing Polygons Lots of different primitives supported GPU only draws triangles • OpenGL triangulates all polygons • Problems with concave polygons! How data is passed to GPU makes a significant difference in speed GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON 69/70 Performance Nvidia 7300 GT1 Nvidia 8800 GTX1 ATI Radeon HD 48502 ATI Radeon HD 48502 New Drivers Immediate Mode 15.5 15.5 21.0 33.0 Display Lists 28.3 22.0 463.5 497.5 Vertex Arrays 33.5 35.5 75.0 335.0 Vertex Buffer Objects 50.0 200.0 476.3 506.0 1 2 Frames per second displaying a 413,236 triangle model. CPU was an Intel Core 2 6700 or Core i7 940 . 70/70