Sie sind auf Seite 1von 7

Filling Polygons

CS/CptS 442/542

1. Flavors of polygons

convex

concave

complex

Convex : All points on the line connecting any two points inside the polygon are also inside the polygon. Convex polygons can be lled fast, thus 3D polygonal meshes are usually composed of convex polygons. Concave polygons can be subdivided into convex polygons (e.g., triangles), but require more work to ll. The interior of a complex or degenerate polygon is dened by three dierent rules: (a) Even-Odd Rule: Ray from point inside polygon will intersect polygon an odd number of times.

0 1 2 3 scan line out in out in

4 out in

5 out

Note that a scan line will intersect an even number edges (ignoring horizontal edges). Sort intersections from left to right (i.e. sort on x). Six intersections for a given scan line are labeled 0 to 5 in gure. Each subsequent pair of intersections dene a span, which is a sequence of adjacent pixels on a scan line that are inside the polygon.

(b) Nonzero Winding Rule: Edges of polygon wrap one or more times around interior pixels.

+ +
4 out in 5 out

0 1 2 3 scan line out in in in

+ +

Traverse the edges of the polygon clockwise (or counter-clockwise) and label edges with a + or a depending on if the edge increases or decreases in y (ignore horizontal lines). Sort scan line intersections on x. Initialize a counter to zero and visit each intersection from left to right. Each time you encounter a + intersection, add one to the counter, otherwise subtract one from the counter. Whenever the counter is dierent from zero, you are inside the polygon. (c) Non-Exterior Rule: Any point that is fenced in is an interior point.

0 1 2 3 scan line out in in in

4 in in

5 out

Scan conversion using this rule is non-trivial. If a scan line passes directly through a vertex we need to specify which edges we consider the scan line to intersect.
0 intersections

edges are considered to contain their top vertex but not their bottom vertex

scan line 1 intersection (with top edge) 2 intersections

2. Coherence: to be logically consistent or connected Exploiting spatial coherence: Create algorithms that capitalize on the fact that polygons do not change much from pixel to pixel within a scan line (span coherence) or from scan line to scan line (edge coherence). Remember that neighboring pixels in a span are adjacent to each other in the frame buer. Scan conversion is rasterizing a primitive scan line by scan line. 3. Scan conversion Starting from the top of the polygon, we compute spans between left and right edges for each scan line. We store the inverse slope 1/m (as a xed or oating point number) for each edge and use the update equation 1 xi+1 = xi + m as we move from scan line yi to yi+1 .

scan line
round x down round x up

The trick is to ll only pixels that are interior to the polygon. If an edge goes through the center of a pixel we need to dene the following rule: If the edge is a left edge we ll the pixel; If the edge is a right edge we do not ll the pixel. This guarantees that we can tile polygons together without creating seams or holes for shared edges (and no pixels are lled twice). How do we handle fractional x values at edge intersections so that only interior pixels are lled? left edge (i.e., moving from outside to inside) : round x up to x + 0.999 right edge (i.e., move from inside to outside) : round x down to x 0.001 Remember, the bottom vertex of an edge is not considered to be part of the edge. Horizontal edges are ignored. What about slivers (i.e., polygons that are less than a pixel thick in certain areas)?

4. The Algorithm (uses even-odd rule) (a) We rst compute the following information for all non-horizontal edges: minimum and maximum y -values (as integers); x-value associated with ymin (as a xed or oating point number); inverse slope 1/m (xed or oating point). The data structure holding these edges is referred to as the edge table (ET). (b) The edge table is sorted on the ymin eld (stored in ascending order). (c) Another data structure called the active edge table (AET), which is used to hold edges that intersect the current scan line, is initialized to be empty. (d) Initialize the current scan line y to the smallest ymin in the ET. (e) Repeat the following until both the ET and AET are empty: i. ii. iii. iv. v. vi. Move any edges from the ET to the AET where y = ymin . Sort the AET on x. Fill desired pixels on scan line y using pairs of x coordinates from the AET. Increment y by 1. Remove from the AET any edges where y = ymax . Update each edges x value in the AET for the next scan line

Note that there is always an even number of edges in the AET when it comes time to ll the span. There is exactly 2 edges in the AET for a convex shape. How would you change the algorithm if you had access to a hardware assisted trapezoid ll subroutine. It is important that you check for edges that cross over each other (i.e., bow-ties) if complex polygons are allowed. How do we modify the algorithm to use either the nonzero winding rule or the nonexterior rule? 5. Filling polygons with non-integer vertices The last stage of a 3-D graphics pipeline often involves scan converting convex polygons (typically triangles and quadrilaterals).
modeling transform polygon specied by user viewing transform projection window to viewport polygon to be rasterized

graphics pipeline

Neither the polygon specied by the user (programmer), nor the nal polygon to be rasterized has vertices that lie on an integer grid. The resulting triangle may contain no interior pixels! Consider a ne triangular mesh that is rendered at a coarse resolution. To avoid cracks and seams and handle ne polygonal meshes, it is imperative that our rasterizer can properly scan convert polygons with non-integer vertices.

Edge information using xed or oating point values


(x0, y0) A e x ey

B (x1, y1) (x2, y2)

Consider the edge with vertices (x0 , y0 ) and (x1 , y1 ) (y0 < y1 ) in the above gure. We rst determine the edges inverse slope: x x1 x0 = . y y1 y0 Note that if y0 = y1 then the edge is eectively horizontal and can be ignored (see bottom edge in gure). We need to adjust the initial x-value corresponding to the rst scan line that intersects the edge. We can compute the vertical distance ey between the rst intersecting scan line and y0 as ey = y0 y0 . Therefore, the amount to adjust our initial x-value is ex = ey So our initial value for x (at point A in gure) is x = x0 + ex 6. Adding color and depth information We will often want to associate a color and a depth value with each interior pixel. Given the appropriate depth d and rgb (red, green, blue) color at each vertex, we can interpolate these values across the face of the polygon. We store the red slope (similarly for green and blue) r r1 r0 = . y y1 y0 The red adjustment value is er = ey so our initial red value (at point A in the gure) is r = r0 + er . We also store the depth slope d/y and the adjusted depth value d. 5 r y x y .

The depth value is used to determine which pixels are closer to the eye and occlude objects already rendered to the frame buer. A special buer, called the depth buer or z-buer, is used to store pixel depth values (we will discuss this in detail later in the course). Resulting entry in edge table ymin ymax x dxdy r, g, b drdy, dgdy, dbdy d dddy minimum y value maximum y value current x value inverse slope current color color slopes current depth depth slope

ymin tells us when the edge moves from the ET to the AET, and ymax tells when when the edge is removed from the AET. x, r, g, b and d are the respective interpolated values as we proceed down the edge. The various slopes tell us how to update the interpolated values for each new scan line. Filling a span from (xl , y ) to (xr , y ) where y is the current scan line.
left edge xl pixels to ll right edge xr

round up

round down

We round xl up and xr down to nd the starting and ending pixels on the span. We subtract by a tiny value from xr in case the right edge passes through the center of a pixel (in which case it is not considered to be interior according to our tiling rules). xstart xend = = xl + 0.9999 , xr 0.0001 .

The number of pixels to ll is n = xend xstart + 1. Note that it is possible for this number to be zero (how can this happen?). To interpolate the red color component across the span we rst nd the spans red slope given the left and right r values rl and rr : r rr rl = x xr xl We need to adjust the initial red component as we round up to the rst pixel. The distance between xstart and xl is ex = xstart xl . The amount to adjust rl by is er = ex r x .

So we begin lling the span at xstart using a red value of r = rl + er . As move to the next pixel, we add r/x to r. 6

We interpolate the remaining color components and depth values across the span in a similar fashion. The depth value of each pixel is compared to the corresponding value in the z-buer to see if the pixel is occluded or not. int x, xend; float r, g, b, drdx, dgdx, dbdy; float d, dddx; /* initialize local vars base on left and right edge info */ while (x <= xend) { int depth = round(d); if (depth > zbuffer[y][x]) { zbuffer[y][x] = depth; framebuffer[y][x] = color(round(r),round(g),round(b)); } x++; d += dddx; r += drdx; g += dgdx; b += dbdx; } 7. More to come. . . We have just described how to ll a polygon using bilinear interpolated color and depth values across its face. This is what OpenGL uses to ll polygons when smooth shading is enabled. We will talk in more depth about this when we cover Gouraud shading. We will also want to interpolate texture coordinates across the face of the polygon in order to paint the polygon with texels retrieved from a texture map. Unfortunately, linear interpolation is not quite sucient for this. We will discuss how to do this correctly using hyperbolic interpolation later.

Das könnte Ihnen auch gefallen