Raster Displays and Scan Conversion Computer Graphics, CSCD18 Fall 28 Instructor: Leonid Sigal
Rater Displays Screen is represented by 2D array of locations called piels y
Rater Displays Screen is represented by 2D array of locations called piels At each piel 2 N intensities/colors can be generated Grayscale 2 8 = 256 Color (2 8 + 2 8 + 2 8 ) y
Rater Displays Screen is represented by 2D array of locations called piels At each piel 2 N intensities/colors can be generated Grayscale 2 8 = 256 Color (2 8 + 2 8 + 2 8 ) Colors are stored in a frame buffer physical memory on a graphics card Primitive operations setpiel (,y,c) getpiel (,y) y
Scan Conversion Convert basic CG objects (2D) into corresponding pielmap representation Since objects are often specified using real valued mathematical primitives (e.g. lines, circles, arcs, etc.), often an approimation to object Continuous line Digital line (8,6) y y (1,5)
Scan Conversion for Lines Set piels to desired line color to approimate the line from (, y ) to ( 1, y 1 ) Goals Accuracy: piels should approimate the line as closely as possible Speed: line drawing should be as efficient as possible Visual quality: uniform brightness Usability: independent of point order, independent of the slope
Equation of the Line y = m + Points that are on the line must satisfy equation above (where m = slope, b = y-intercept) y y 1 m b = = = = y m m y 1 1 1 + + y b m b y = m( + y ) y b Continuous line Continuous line ( 1,y 1 ) (,y) m (,y ) This is the form we ll prefer to use for this lecture
Line Drawing: Basic Idea We need to determine the piels that lie closest to the mathematical line Digital line Simple Algorithm 1. Draw piel at line start
Line Drawing: Basic Idea We need to determine the piels that lie closest to the mathematical line Digital line Simple Algorithm 1. Draw piel at line start 2. Increment piel position by 1
Line Drawing: Basic Idea We need to determine the piels that lie closest to the mathematical line Digital line Simple Algorithm? 1. Draw piel at line start 2. Increment piel position by 1 3. Determine the y position of the piel lying closest to the line
Line Drawing: Basic Algoruithm We need to determine the piels that lie closest to the mathematical line Digital line Simple Algorithm? compute m for (=, <= 1, ++) y = m (- ) + y setpiel (, round(y), c) end y is real if m is real Problem: What if points are given in the wrong order? Solution: Detect ( 1 < ) and switch order of points
Line Drawing: Basic Algorithm Let s test with m = 1 Simple Algorithm compute m for (=, <= 1, ++) y = m (- ) + y setpiel (, round(y), c) end
Line Drawing: Basic Algorithm Let s test with m = 1/2 Simple Algorithm compute m for (=, <= 1, ++) y = m (- ) + y setpiel (, round(y), c) end
Line Drawing: Basic Algorithm Let s test with m = 2 Simple Algorithm compute m for (=, <= 1, ++) y = m (- ) + y setpiel (, round(y), c) end Problem: When m > 1 Solution: Loop over y instead of when m > 1
Line Drawing: Basic Algorithm Let s test with m = 2 Simple Algorithm (etended) compute m if (m <= 1) for ( =, <= 1, ++) y = m (- ) + y setpiel (, round(y), c) end else for (y = y, y <= y 1, y++) = (y-y )/m + setpiel (round(), y, c) end end
Line Drawing: Basic Algorithm Key disadvantage: inefficiency relies on floating-point operations to compute piel positions floating-point operations are slow Alternative: Bresnham s Algorithm Incremental integer approach
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? y i +1 Line point @ i+1 : Q = m ( i + 1 ) + y Basic Idea 1. Test if Q < is above or bellow M y i i i +1 Midpoint: M = ( i +1, y i +.5) (also known as Midpoint Algorithm)
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? y i +1 Line point @ i+1 : Q = m ( i + 1 ) + y Basic Idea 1. Test if Q < is above or bellow M 2. If Q bellow M turn on ( i+1, y i ) y i i i +1 Midpoint: M = ( i +1, y i +.5) (also known as Midpoint Algorithm)
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? y i +1 Line point @ i+1 : Q = m ( i + 1 ) + y Basic Idea 1. Test if Q < is above or bellow M 2. If Q bellow M turn on ( i+1, y i ) 3. If Q above M turn on ( i+1, y i+1 ) y i i i +1 Midpoint: M = ( i +1, y i +.5) (also known as Midpoint Algorithm)
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? y i +1 Line point @ i+1 : Q = m ( i + 1 ) + y Basic Idea 1. Test if Q < is above or bellow M 2. If Q bellow M turn on ( i+1, y i ) 3. If Q above M turn on ( i+1, y i+1 ) y i 4. Repeat above steps for the newly draw point i i +1 Midpoint: M = ( i +1, y i +.5) Steps guarantee that closest piel to line is always chosen (also known as Midpoint Algorithm)
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? Line point @ i+1 : Q = m ( i + 1 ) + y Basic Idea How do we decide if Q is above or bellow M? y i +1 Look at the implicit function of the line f(,y) y i i i +1 Midpoint: M = ( i +1, y i +.5)
Implicit function of the line 1 1 W y y H = = H W m ) ( ) ( Wy H Wy y W H y + = + = ) ( y m y + = ) ( ) ( ), ( y W y H y f + = = ) ( 2 ) ( 2 ), ( y W y H y f = = If f(,y) = then (,y) on the line If f(,y) < then (,y) above line Eercise for home, show that indeed If f(,y) > then (,y) bellow line
Bresenham s Algorithm Incremental approach: assume piel ( i, y i ) is on how do we tell which piel to turn on net? Line point @ i+1 : Q = y = m ( i + 1 ) + y Basic Idea How do we decide if Q is above or bellow M? y i +1 y i i i +1 Look at the implicit function of the line f(,y) @ the midpoint If f( i +1, y i +.5) < then j = i +1 y j = y i If f( i +1, y i +.5) < then j = i +1 Midpoint: y j = y i +1 M = ( i +1, y i +.5)
Now, why did we multiply by 2? ) ( 2 ) ( 2 ), ( y W y H y f = = ).5 ( 2 ) 1 ( 2.5) 1, ( y W y H y f i i i i + + = = + + Now this computation can be done in terms of integers
Now, why did we multiply by 2? f (, y) = = 2H( ) 2W( y y ) f ( i + 1, y i +.5) = = 2H( i + 1 ) 2W( y i +.5 y ) Note, that we only need to keep track of f(,y) at the mid points, which can be done efficiently incrementally f ( + 1, y) = f (, y) + 2H f ( + 1, y + 1) = f (, y) + 2( H W)
Now, why did we multiply by 2? f (, y) = = 2H( ) 2W( y y ) f ( i + 1, y i +.5) = = 2H( i + 1 ) 2W( y i +.5 y ) Note, that we only need to keep track of f(,y) at the mid points, which can be done efficiently incrementally Very Efficient f ( + 1, y) = f (, y) + 2H f ( + 1, y + 1) = f (, y) + 2( H W)
Bresenham s Algorithm y = y H = y 1 y W = 1 f = 2H W for ( =, <= 1, ++) setpiel (, y, c) if (f <) f += 2H else y++ f += 2(H-W) end end Note, initially f(,y )=, so first test is @ f( +1, y +.5) f ( // y stays the same // y increases + 1, y +.5) = 2H( + 1 2W( y = 2H W ) +.5 y )
Bresenham s Algorithm Limitations: same as the basic line drawing, the Bresenham s algorithm in the last slide only works for m < 1 and has to be altered for a more general cases To make it general need to (as in the basic line drawing algorithm) Switch order of points if necessary Iterate over y if m > 1
Aliasing An unfortunate artifact of the line scan conversion discussed is that lines have jaggy appearance This phenomenon is called aliasing
Anti-aliasing Main idea: rather than just drawing in s and 1 s, use in-between values in neighborhood of the mathematical line Aliased line Anti-aliased line
Anti-aliasing Comparison
Polygon Filling Scan Conversion Goal: find piels that occupy inside of the polygon and fill them with a given color Polygon Filling 2 3 P = ( 1, 2, 3, 4, 5) 5 1 4
Polygon Filling Idea Goal: find piels that occupy inside of the polygon and fill them with a given color Polygon Filling 2 3 Simple Idea For each horizontal scanline L L 5 1 4
Polygon Filling Idea Goal: find piels that occupy inside of the polygon and fill them with a given color L Polygon Filling 2 3 Simple Idea For each horizontal scanline L 1. Find intersection of L with P (store in active edge list AEL) 5 1 4
Polygon Filling Idea Goal: find piels that occupy inside of the polygon and fill them with a given color Polygon Filling 2 3 L 1 2 5 Simple Idea For each horizontal scanline L 1. Find intersection of L with P (store in active edge list AEL) 2. Sort intersections by increasing value of 1 4
Polygon Filling Idea Goal: find piels that occupy inside of the polygon and fill them with a given color Polygon Filling 2 3 L 1 2 5 1 4 Simple Idea For each horizontal scanline L 1. Find intersection of L with P (store in active edge list AEL) 2. Sort intersections by increasing value of 3. Fill piels between pairs of intersections
Polygon Filling Algorithm Algorithm Active Edge List (AEL) y=n... for each edge [(, y ), ( 1,y 1 )] in P =; compute m for (y=y, y<=y 1, y++) = + 1/m place (round(), y) in AEL end end Does this remind you of anything? y= 1 2 3
Polygon Filling Special Cases A B C Intersections: one @ A two @ B one @ C A B C D Intersections: one @ A two @ B one @ C one @ D
Polygon Filling Special Cases A B C Intersections: one @ A two @ B one @ C works!!! A B C D problem!!! Intersections: one @ A two @ B one @ C one @ D
Polygon Filling Handling Special Cases All problems can be handled by 2 simple rules only rasterize edges (not intersections) Ignore horizontal edges
Clipping Clipping: used to determine which parts of the line/polygon lie inside viewing window allows efficient rendering and rastering (W R, W T ) (W L, W B )
Clipping Algorithm Every line segment of the polygon is either trivially inside (both endpoints lie inside the window) trivially outside (both endpoints lie outside of one of the half-spaces that define the window) candidate for clipping (W R, W T ) (W L, W B )
Clipping Algorithm Every line segment of the polygon is either trivially inside (both endpoints lie inside the window) Keep trivially outside (both endpoints lie outside of one of the half-spaces that define the window) candidate for clipping (W R, W T ) Remove (W L, W B )
Clipping Algorithm Every line segment of the polygon is either trivially inside (both endpoints lie inside the window) Keep trivially outside (both endpoints lie outside of one of the half-spaces that define the window) candidate for clipping Find the intersection with the window (if eists) Disregard irrelevant part of the segment Remove