221 likes | 332 Vues
This article explores the rasterization problem and strategies for converting idealized geometric primitives into discrete pixel representations. It discusses efficient algorithms, such as the Digital Differential Analyzer (DDA) and Midpoint Algorithm, which minimize computational overhead during line rendering. The article also covers the Scan-Line Algorithm for polygon filling, including handling special cases like shared vertices and horizontal edges. The presented methods emphasize incremental calculations to enhance performance, ensuring accurate and efficient rendering in graphical applications.
E N D
The Rasterization Problem: Idealized PrimitivesMap to Discrete Display Space
Solution Involves Selection of DiscreteRepresentation Values
Scan Converting Lines:Characterizing the Problem ideal line i.e. for each x, choose y i.e. for each y, choose x
Scan Converting Lines:The Strategy • Pick pixels closest to endpoints • Select in between pixels “closest” to ideal line • Minimize calculation required!
Calculate ideal line equation Pick endpoints: two dimensional round. Starting at leftmost point: for each xi Calculate Select pixel at Scan Converting Lines:A Basic Algorithm Selected Not Selected
Scan Converting Lines:DDA Algorithm, Incremental Form Therefore, rather than recomputing y each step, simply add m. The Algorithm: void Line(int x0, int y0, int x1, int y1) { int x; float dy, dx, y, m; dy = y1 - y0; dx = x1 - x0; m = dy/dx; y = y0; for (x = x0; x<=x1,x++){ WritePixel(x, round(y)); y += m; } }
Midpoint Algorithm:Allowable Pixel Selections Not Allowed Option NE 0 < Slope < 1 Option E Last Selection
Midpoint Algorithm:Iterating 0 < Slope < 1 Select E Select NE
Midpoint AlgorithmDecision Function: (implicit equation of the line)
Option NE Option E Midpoint Algorithm:Incremental Calculation of di
Midpoint Algorithm:The Code void MidpointLine(int x0, int y0, int xn, int yn) { int dx,dy,incrE,incrNE,d,x,y; dx=xn-x0; dy=yn-y0; d=2*dy-dx; /* initial value of d */ incrE=2*dy; /* decision funct incr for E */ incrNE=2*dy-2*dx; /* decision funct incr for NE */ x=x0; y=y0; DrawPixel(x,y) /* draw the first pixel */ while (x<xn){ if (d<=0){ /* choose E */ d+=incrE; x++; /* move E */ }else{ /* choose NE */ d+=incrNE; x++; y++; /* move NE */ } DrawPixel (x,y); } }
Midpoint AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9
Midpoint AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9
Midpoint AlgorithmAn example 12 11 10 9 8 7 4 5 6 7 8 9
Filling Polygons:Scan Line Algorithm 14 12 10 8 6 4 2 0 0 2 4 6 8 10 12 14
Filling Polygons:Scan Line Algorithm 1. Find the intersections of current scan line with all edges of the polygon. 2. Sort the intersections by increasing x coordinate. 3. Fill in pixels that lie between pairs of intersections that lie interior to the polygon using the odd/even parity rule. Special Cases: 1. Intersections at integer coordinates 2. Shared vertices 3. Horizontal edges
Filling Polygons:Special Cases Only count ymin, not ymax. Leftmost integer pixel is interior; rightmost exterior. 14 12 10 8 6 4 2 0 0 2 4 6 8 10 12 14
11 10 9 8 7 6 5 4 3 2 1 0 Scan Line Polygon Fill Algorithm:Exploiting Coherence l l 14 l D 12 l EF DE F 10 -5/2 6/4 l 9 7 7 12 CD l 8 l 12 13 0 E 6 C FA l 4 l 9 2 0 A 2 l AB BC B 0 6/4 l -5/2 7 3 7 5 0 2 4 6 8 10 12 14 l Scan Line
Scan Line Polygon Fill:The Algorithm • Set y to smallest y coordinate that has an entry in ET • Initialize the active edge table (AET) to empty • Repeat until the ET and AET are both empty: • Move from ET bucket at y to AET those edges whose ymin = y. (entering edges) • Remove from AET those edges for which y = ymax. • Sort AET on x • Fill in desired pixels on scan line using even-odd parity from AET • Increment y by one (next scan line) • For each edge in the AET, update x by adding Dx/Dy