Prof. Feng Liu Fall 08 http://www.cs.pd.edu/~fliu/courses/cs7/ 0/5/08
Last time Clipping
Toda Rasterization In-class Mid-term November Close-book eam Notes on page of A or Letter size paper
Where We Stand At this point we know how to: Convert points from local to screen coordinates Clip polgons and lines to the view volume Net thing: Determine which piels to fill for an given point, line or polgon
Drawing Points When points are mapped into window coordinates, the could land anwhere not just at a piel center Solution is the simple, obvious one Map to window space Fill the closest piel Can also specif a radius fill a square of that size, or fill a circle Square is faster 5
Drawing Points () 7 6 5 5 6 7 8 6
Drawing Lines Task: Decide which piels to fill (samples to use) to represent a line We know that all of the line lies inside the visible region (clipping gave us this!) 7
Line Drawing Algorithms Consider lines of the form =m + c, where m=/, 0<m<, integer coordinates All others follow b smmetr, modif for real numbers Variet of slow algorithms (Wh slow?): step, compute new at each step b equation, rounding: step, compute new at each step b adding m to old, rounding: i, i round ( mi b), round ( ) i i i i i m 8
Bresenham s Algorithm Overview Aim: For each, plot the piel whose -value is closest to the line Given ( i, i ), must choose from either ( i +, i +) or ( i +, i ) Idea: compute a decision variable Value that will determine which piel to draw Eas to update from one piel to the net Bresenham s algorithm is the midpoint algorithm for lines Other midpoint algorithms for conic sections (circles, ellipses) 9
Midpoint Methods Consider the midpoint between ( i +, i +) and ( i +, i ) If it s above the line, we choose ( i +, i ), otherwise we choose ( i +, i +) i + i + i i i i + i i + Choose ( i +, i ) Choose ( i +, i +) 0
Midpoint Decision Variable Write the line from (, ) to (, ) in implicit form: F, a b c Assume <= = -, = - The value of F(,) tells us where points are with respect to the line F(,)=0: the point is on the line F(,)>0: The point is above the line F(,)<0: The point is below the line The decision variable is the value of d i = F( i +, i +0.5) The factor of two makes the math easier
What Can We Decide? d i i ( i ) d i positive=> net point at ( i +, i ) d i negative => net point at ( i +, i +) At each point, we compute d i and decide which piel to draw How do we update it? What is d i+?
Updating The Decision Variable d k+ is the old value, d k, plus an increment: If we chose i+ = i +: d d k dk ( dk dk k dk ) If we chose i+ = i : d k dk What is d (assuming integer endpoints)? Notice that we don t need c an more d
Bresenham s Algorithm For integers, slope between 0 and : =, =, d= d -d, draw (, ) until = =+ If d<0 then { =+, draw (, ), d=d- + } If d>0 then { =, draw (, ), d=d- } Compute the constants (- and ) once at the start Inner loop does onl adds and comparisons For floating point, initialization is harder, and will be floating point, but still no rounding required
Eample: (,) to (7,6) 7 6 5 =5, = d - 5 6 7 8 5
Eample: (,) to (7,6) 7 6 5 =5, = d - - 5 6 7 8 6
Eample: (,) to (7,6) 7 6 5 =5, = d - - 5 6 7 8 7
Eample: (,) to (7,6) 7 6 5 =5, = d - - 5-7 5 6 7 8 8
Eample: (,) to (7,6) 7 6 5 =5, = d - - 5-7 6 5-5 5 6 7 8 9
Eample: (,) to (7,6) 7 6 5 =5, = d - - 5-7 6 5-5 7 6-5 6 7 8 0
Filling Triangles 7 6 5 5 6 7 8
Filling Triangles 7 6 5 5 6 7 8
Algorithm Decide which piels to fill (samples to use) to represent a triangle? Calculate the color for each piel?
Barcentric coordinates P0=(0, 0) P P P P 0 0,, P=(, ) P=(, )
Barcentric coordinates 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ) ( ) (, ) ( ) (, ) ( ) (,, /,, /,, /, f f f f f f f f f
Rasterizing Triangle min =min( 0,, ), ma =ma( 0,, ) min =min( 0,, ), ma =ma( 0,, ) for = min to ma for = min to ma calculate,, and if 0,,and c c 0 c c draw (, ) with color c 6
Anti-Aliasing Recall: We can t sample and then accuratel reconstruct an image that is not band-limited Infinite Nquist frequenc Attempting to sample sharp edges gives jaggies, or stairstep lines Solution: Band-limit b filtering (pre-filtering) What sort of filter will give a band-limited result? In practice, difficult to do for graphics rendering 7
Alpha-based Anti-Aliasing Set the of a piel to simulate a thick line The piel gets the line color, but with <= This supports the correct drawing of primitives one on top of the other Draw back to front, and composite each primitive over the eisting image Onl some hidden surface removal algorithms support it 0 0 0 /8 0 / 0 0.9 /8 / 0.9/ 0 /8.9 / 0 0 0 /8 0 0 0 8
Calculating Consider a line as having thickness (all good drawing programs do this) Consider piels as little squares Set according to the proportion of the square covered b the line The sub-piel coverage interpretation of 0 0 0 /8 0 0 0 /.9 /8 0 /.9 / 0 /8.9 / 0 0 0 /8 0 0 0 9
Weighted Sampling Instead of using the proportion of the area covered b the line, use convolution to do the sampling Equivalent to filtering the line then point sampling the result Place the filter at each piel, and integrate product of piel and line Common filters are cones (like Bartlett) or Gaussians 0
Post-Filtering (Supersampling) Sample at a higher resolution than required for displa, and filter image down Eas to implement in hardware Tpical is sampling per piel, with simple averaging to get final What kind of filter? More advanced methods generate different samples (eg. not on regular grid) and filter properl Issues of which samples to take, and how to filter them
Net Time Hidden Surface Removal