ECM2410:GraphicsandAnimation R asterisation Part I: Simple Lines Rasterisation 1/16 Rendering a scene User space Device space Affine transformation Compose Transform Render Com pose from primitives (lines, ellipses, etc) in user space. Transform affine transformations: lines lines; ellipses ellipses Render primitives on device. Rasterisation Line Rasterisation 2/16 Rasterisation Vector displays Pen plotters Tektronix cathode ray tubes Raster displays Monitors Televisions Laser, ink-jet, dot matrix printers Liquid crystal displays Arrays of LEDs Rasterisation is the conversion of ideal, mathematical graphics primitives onto raster displays. Drawing lines Drawing circles and ellipses Filling areas Hearn & Baker, Chapter 2 for displays. Rasterisation Line Rasterisation 3/16
Fram ebuffer Raster display is a 2D array of picture elements: pixels Framebuffer: in-memory image of the display. 2 1 0 0 1 2 Screen coordinates of pixels are centered at the integers. Pixels are individually set or cleared (random access). Sequential readout by physical device. Graphical objects are continuous. Framebuffer is discrete. Rasterisation / Framebuffer Line Rasterisation 4/16 Rasterisation Rasterisation is the conversion of ideal, mathematical graphics primitives onto raster displays. Efficiency we seek efficient algorithms for computing the discrete approximation to continuous description. Scan Conversion rasterisation line by line of the raster display. Rasterisation / Framebuffer Line Rasterisation 5/16 Line Segm ents Problem: Draw a line from (X start, Y start ) to (X end, Y end ) Simplified: Draw a line from (0, 0) to (X end, Y end ) (X end > 0 and Y end > 0) (19,4) (0,0) Grid marks pixel centres Example: Line from (0, 0) to (X end, Y end ) = (19, 4) Lines Line Rasterisation 6/16
Whichpixelstoilluminate? Illuminate every pixel the line intersects Illuminate the pixel Lines / Which Pixel? Line Rasterisation 7/16 Gradient Gradient of line y = m x + c m = Y end Y start X end X start Simplified line from (0, 0) to (X end, Y end ): Lines / Gradient Line Rasterisation 8/16 NaïveMidpointAlgorithm Illuminate pixel vertically closest to the line 4/19 12/19 8/19 16/19 double m = Yend / Xend; double y = 0.0; while ( x <= Xend ) { y = m * x; iy = round(y); DDA Digital Differential Analyser. Hearn & Baker, p86. Lines / Naïve Algorithm Line Rasterisation 9/16
Rem ove round() Line coherence: As x x + 1 then y y + m 4/19 8/19 12/19 16/19 At each step: move right one pixel and up by 4 19. midpoint indicates pixel centre above illuminated pixel. When y crosses vertical midpoint between two centres move up and compute new midpoint target. double m = Yend / Xend; int iy = 0; double y = 0.0; double midpoint = 0.5; while (x <= Xend) { y += m; if (y > midpoint) { Lines / Eliminating Floats Line Rasterisation 10/16 Integers only m = Y end X end y = m x = Y end x X end Multiply m, y and midpoint by twice total change in x; (i.e. ) m = 2 Y end y=8 16 y = 2 Y end x 24 32 int m = 2 * Yend; int iy = 0; int y = 0; int midpoint = Xend; while (x <= Xend) { y += m; if (y > midpoint) { iy++; midpoint += 2 * Xend; 19 midpoint 19 19 57 57 Lines / Eliminating Floats Line Rasterisation 11/16 Bresenham s algorithm Eliminate midpoint by adjusting y to start at the midpoint and decreasing to 0. y=19 11 3 5 => 33 Hearn & Baker, page 88 25 int m = 2 * Yend; int iy = 0; int y = Xend; while (x <= Xend) { y -= m; if (y < 0) { iy++; y += 2 * Xend; Lines / Bresenham s Algorithm Line Rasterisation 12/16
Caveats Needsadjustmentfor: References Lines with different slope may have different brightness. Alleviated with anti-aliasing (give pixels different brightness). Hearn & Baker, pp. 84-94 Foley & van Dam Lines / Bresenham s Algorithm Line Rasterisation 13/16 Bresenham Circle A lgorithm Choose pixel with centre closest to circle. Work on a single octant and draw others by symmetry. Can be written in integer arithmetic. Details in Hearn & Baker, Foley et al. Lines / Bresenham Circle Algorithm Line Rasterisation 14/16 ThickPrimitives Colum n Replication Efficient, easily implemented. Perceived thickness varies with slope. Gaps at joins. Rectangular Pen Ends are thicker than middle. Perceived thickness varies with slope. Pixels are set multiple times. Lines / Thick Primitives Line Rasterisation 15/16
ThickPrimitives Circular Pen Thickness independent of slope. Pixels are set multiple times. Can be made efficient by setting only boundary pixels. Lines / Thick Primitives Line Rasterisation 16/16