Computer Graphics, Lecture 2 October 31, 2006 Frame buffer Basic Basic rasterization Computer Graphics 1, Fall 2006 Lecture 2 Chapter 7.8-7.10, 7.12, 8.13 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 7.8 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; Ingela Nyström 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--; 7.9 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 Line algorithm 4 Subtract 2*Xdifference from Yerror Yerror = 0.5*2*Xdifference; Yerror += 2*Ydifference; if Yerror >= 2*Xdifference then Yerror -= 2*Xdifference; Yerror = -Xdifference; Yerror += 2*Ydifference; if Yerror >= 0 then 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 += StepEast; else Yerror += StepNorthEast; (-x,y) (-x,-y) (-y,x) (-y,-x) (y,x) (y,-x) (x,y) (x,-y) Ingela Nyström 2
7.10 Scan Conversion of Polygons Defining a polygon Many methods for rasterizing polygons: Inside-outside testing Concave polygons Fill and sort Flood fill Scan lines 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, i.e., scan conversion Flood-fill: Rasterize the edges of the polygon Find seed-point inside Recursively fill neighbours until edges are reached Ingela Nyström 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 (X2L,Y2) (X2R,Y2) p 2 =(X2,Y2) height: H=Y2-Y1 p 1 =(X1,Y1) y(x) = kx + m k = dy/dx= (Y2- Y1) / (X2- X1) m = Y1 slope left/right: SL=(X2L-X1L)/H SR=(X2R-X1R)/H (X1L,Y1) (X1R,Y1) Ingela Nyström 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 Bi-linear interpolation 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. 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 dy Interpolate horizontally by adding dc/dx 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 Ingela Nyström 5
7.12 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 under-sample. This results in effects called aliasing. Anti-aliasing To reduce the effect of aliasing various anti-aliasing techniques can be applied: 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) Line segments Area-sampling Intensity proportional to the area covered by the object Simplified area-sampling Intensity proportional to 1 - distance to pixel Ingela Nyström 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 7.6.3 Clipping in the frame buffer When filling polygons, the pixels outside the screen should not be drawn CSL=(C2L-C1L)/H CSR=(C2R-C1R)/H xl=x1l xr=x1r cl=c1l cr=c1r y=max(ceil(y1),miny) yskip=y-y1 The small starting step we made, yskip,, may be of arbitrary size 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 material 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 Useful in computer exercise 1 Ingela Nyström 7