Frame buffer Basic rasterization Computer Graphics 1, Spring 2004 Lecture 2 Chapter 8.9-8.12 Array for display n x m pixels x-y coordinate system Origin in lower-left corner: computer graphics upper-left corner: image analysis Drawing lines Discrete line segments Rosenfeld in the 1960 s 8.9 Scan Conversion Digital Differential Analyzer (DDA) Simplest algorithm for rasterization of line segments End-points at integer points Compute the slope Line algorithm 1: DDA Simple, but uses floating point value computations delta_y = Ydifference / Xdifference; y += delta_y; Patrick Karlsson 1
Line algorithm 2 Keep the fraction part of y in a separate variable, Yerror. Start one half up. delta_y = Ydifference / Xdifference; Yerror = 0.5; Yerror += delta_y; if Yerror >= 1 then Yerror--; 8.10 Bresenham s Algorithm DDA appears efficient. It is coded easily, but requires one floating point addition per generated pixel In 1965, Bresenham derived an algorithm that avoids floating point computations Standard algorithm in HW and SW Line algorithm 3: integer math Multiply Yerror with 2*Xdifference Yerror = 0.5*2*Xdifference; Yerror = Yerror + 2*Ydifference; if Yerror >= 2*Xdifference then Yerror = Yerror - 2*Xdifference; Line algorithm 4 Subtract 2*Xdifference from Yerror Yerror = -Xdifference; Yerror = Yerror + 2*Ydifference; if Yerror >= 0 then Yerror = Yerror - 2*Xdifference; Line algorithm 5: Bresenham Brushing up things a little Using symmetry Yerror = 2*Ydifference Xdifference; StepEast = 2*Ydifference; StepNorthEast = 2*Ydifference - 2*Xdifference; if Yerror <= 0 then Yerror = Yerror + StepEast; else Yerror = Yerror + StepNorthEast; (-x,y) (-x,-y) (-y,x) (-y,-x) (y,x) (y,-x) (x,y) (x,-y) Patrick Karlsson 2
8.11 Scan Conversion of Polygons Many methods for rasterizing polygons Inside-outside testing Concave polygons Fill and sort Flood fill Scan lines Defining a polygon A polygon is an enclosed area with at least 3 vertices (or 3 edges) Convex polygon Concave polygon Complex polygon Order of vertices Inside-outside testing Filling polygons Filling polygons Draw one scan-line at a time = scan conversion Patrick Karlsson 3
Scan converting convex polygons Sliver Sort the edges, so we know which ones are left / right. Calculate left and right edge for each scan line (y value) and fill between the left and the right edge values. Be careful to only fill pixels inside the polygon, we do not want double edges. Standard is to include the low valued edges (left edge and first scan line) in the polygon. A pixel-perfect scan conversion algorithm Start by splitting the polygon with horizontal lines at each intermediate vertex and calculate the positions of any new vertices. How to calculate the edge values? Bresenham? Requires integer end-points => polygon jumps one pixel at a time when moving. (Solve by using fixed point mathematics.) Here, we will use the simpler DDA algorithm instead. Each part is now a special case of a figure limited by two arbitrary sloped (not horizontal) lines and two horizontal lines. Note! Horizontal edges may have length 0 Linear interpolation Flat top and bottom p(a) = (1- a)p 1 +a p 2 p 1 =(X1,Y1) p 2 =(X2,Y2) y(x) = kx + m k = dy/dx= (Y2- Y1) / (X2- X1) m = Y1 height: H=Y2-Y1 slope left/right: SL=(X2L-X1L)/H SR=(X2R-X1R)/H (X2L,Y2) (X1L,Y1) (X2R,Y2) (X1R,Y1) Patrick Karlsson 4
Scan converting polygons Scan converting polygons xl=x1l xr=x1r y=ceil(y1) yskip=y-y1 xl+=yskip*sl xr+=yskip*sr (X1L,Y1) yskip while y floor(y2) for x from ceil(xl) to floor(xr) write_pixel(x,y); x++; end for xl+=sl*1 xr+=sr*1 end while (X1L,Y1) Interpolated scan conversion When interpolating colour, think of the colour value as an additional dimension. So instead of drawing polygons in 2D, we draw them in 3D (or rather 5D) where the colour value is the z dimension. Bi-linear interpolation Linear interpolation in two directions. First we interpolate in the y direction, then we interpolate in the x direction. Unique solution for flat objects Interpolate the new variable over the polygon in the same way as before using the colour slope: dc/dy. Interpolate horizontally by adding dc/dx CSL=(C2L-C1L)/H CSR=(C2R-C1R)/H xl=x1l xr=x1r cl=c1l cr=c1r y=ceil(y1) yskip=y-y1 xl+=yskip*sl xr+=yskip*sr cl+=yskip*csl cr+=yskip*csr (X1L,Y1,C1L) yskip while y floor(y2) dcdx=(cr-cl)/(xr-xl) xskip=ceil(xl)-xl c=cl+xskip*dcdx for x from ceil(xl) to floor(xr) write_pixel(x,y,c); x++; c+=dcdx end for xl+=sl*1 xr+=sr*1 cl+=csl*1 cr+=csr*1 end while xskip Patrick Karlsson 5
8.11 Anti-aliasing Rasterized line segments and edges of polygons look jagged The phenomenon is called aliasing Anti-aliasing by, e.g., averaging Nyquist s Sampling Theorem In order to accurately reconstruct a signal from a periodically sampled version of it, the sampling frequency must be at least twice the maximum frequency of the signal. Due to limited resolution in the spatial (pixel size) or colour domain, we are forced to undersample. This results in effects called aliasing. Anti-aliasing Line segments To reduce the effect of aliasing various antialiasing techniques can be applied. Some examples are: Low-pass filtering ( poor man s anti-aliasing) Area-sampling (colour proportional to area) Super-sampling (more samples than pixels) Dithering (anti-aliasing in the colour domain) Area-sampling Intensity proportional to the area covered by the object. Simplified area-sampling Simplified version: Intensity proportional to 1 - distance to pixel Patrick Karlsson 6
Super-sampling Anti-aliasing solves the problem of sliver as well as the problem of shared vertices Normal rasterisation, in a frame-buffer of higher resolution than the screen. Average the intensity of the covered sub-pixels when setting the real pixel value. Sliver 13/16 colour + 3/16 white Scissoring 8.6.3 Clipping in the frame buffer When filling polygons, the pixels outside the screen should not be drawn The small starting step we made, yskip, may be of arbitrary size CSL=(C2L-C1L)/H CSR=(C2R-C1R)/H xl=x1l xr=x1r cl=c1l cr=c1r y=max(ceil(y1),miny) yskip=y-y1 Quite easy to insert scissoring into the polygon filling routine xl+=yskip*sl cl+=yskip*csl (X1L,Y1,C1L) yskip Same thing for x. At the top and right side we can just stop. Additional WWW content while y min(floor(y2),maxy) dcdx=(cr-cl)/(xr-xl) xskip=max(ceil(xl)-xl,minx) c=cl+xskip*dcdx for x from ceil(xl) to min(floor(xr),maxx) write_pixel(x,y,c); x+=1 c+=dcdx end for y+=1 xl+=sl*1 xr+=sr*1 cl+=csl*1 cr+=csr*1 end while xskip Derivation of the Bresenham algorithm Interpolated filling Patrick Karlsson 7