10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already – sept1603.ppthttp:// sept1603.ppt
10/15/02 (c) 2002 University of Wisconsin, CS559 Sampling and Drawing Today we’ll talk about rasterization A regular, rectangular grid is referred to as a raster –A piece of history – there are also vector displays –The raster name comes from the ways the pixels are drawn to the screen – left to right, top to bottom Rasterization is the problem of determining which pixels to color – which samples to use – to draw an object
10/15/02 (c) 2002 University of Wisconsin, CS559 Drawing Points You can draw points at any continuous position, not just at a pixel center (a sample point) –How do we know which pixel to turn on?
10/15/02 (c) 2002 University of Wisconsin, CS559 Drawing Points You can draw points at any continuous position, not just at a pixel center (a sample point) –How do we know which pixel to turn on? Solution is the simple one –Find the closest sample and turn it on – fill the pixel –Can also specify a radius – fill a square of that size, or fill a circle Square is faster The best solution is anti-aliased points, where you partially color multiple pixels
10/15/02 (c) 2002 University of Wisconsin, CS559 Drawing Lines Lines have a similar problem to points: they are not likely to hit any pixel centers exactly What might we do instead? What are the aesthetic/perceptual issues in drawing lines?
10/15/02 (c) 2002 University of Wisconsin, CS559 Drawing Lines Task: Decide which pixels to fill (samples to use) to represent a line Issues: –If slope between -1 and 1, one pixel per column. Otherwise, one pixel per row –Constant brightness? Lines of the same length should light the same number of pixels (we normally ignore this) –Anti-aliasing? (Getting rid of the “jaggies”)
10/15/02 (c) 2002 University of Wisconsin, CS559 Line Drawing Algorithms Consider lines of the form y=m x + c, where m= y/ x, 0<m<1, integer coordinates –All others follow by symmetry Variety of slow algorithms (Why slow?): –step x, compute new y at each step by equation, rounding: –step x, compute new y at each step by adding m to old y, rounding:
10/15/02 (c) 2002 University of Wisconsin, CS559 Bresenham’s Algorithm Overview Aim: For each x, plot the pixel whose y-value is closest to the line Given (x i,y i ), must choose from either (x i +1,y i +1) or (x i +1,y i ) Idea: compute a decision variable –Value that will determine which pixel to draw –Easy to update from one pixel to the next Bresenham’s algorithm is the midpoint algorithm for lines –Other midpoint algorithms for conic sections (circles, ellipses)
10/15/02 (c) 2002 University of Wisconsin, CS559 yiyi y i +1 x i +1 Midpoint Methods Consider the midpoint between (x i +1,y i +1) and (x i +1,y i ) If it’s above the line, we choose (x i +1,y i ), otherwise we choose (x i +1,y i +1) Amazing thing: You can do this with only integer add/subtract and if/then xixi Choose (x i +1,y i ) yiyi y i +1 x i +1xixi Choose (x i +1,y i +1)
10/15/02 (c) 2002 University of Wisconsin, CS559 Midpoint Decision Variable Write the line in implicit form: The value of F(x,y) tells us where points are with respect to the line –F(x,y)=0: the point is on the line –F(x,y)<0: The point is above the line –F(x,y)>0: The point is below the line The decision variable is the value of d i = 2F(x i +1,y i +0.5) –The factor of two makes the math easier
10/15/02 (c) 2002 University of Wisconsin, CS559 What Can We Decide? d i negative => next point at (x i +1,y i ) d i positive => next point at (x i +1,y i +1) At each point, we compute d i and decide which pixel to draw How do we update it? What is d i+1 ?
10/15/02 (c) 2002 University of Wisconsin, CS559 Updating The Decision Variable d k+1 is the old value, d k, plus an increment: If we chose y i+1 =y i +1: If we chose y i+1 =y i : What is d 1 (assuming integer endpoints)? Notice that we don’t need c any more
10/15/02 (c) 2002 University of Wisconsin, CS559 Bresenham’s Algorithm For integers, slope between 0 and 1: –x=x 1, y=y 1, d=2dy - dx, draw (x, y) –until x=x 2 x=x+1 If d>0 then { y=y+1, draw (x, y), d=d+2 y - 2 x } If d<0 then { y=y, draw (x, y), d=d+2 y } Compute the constants (2 y-2 x and 2 y ) once at the start –Inner loop does only adds and comparisons Floating point has slightly more difficult initialization, but is otherwise the same Care must be taken to ensure that it doesn’t matter which order the endpoints are specified in (make a uniform decision if d==0)
10/15/02 (c) 2002 University of Wisconsin, CS559 Example: (2,2) to (7,6) x=5, y=4 xyd
10/15/02 (c) 2002 University of Wisconsin, CS559 Example: (2,2) to (7,6) x=5, y=4 xyd
10/15/02 (c) 2002 University of Wisconsin, CS559 Filling Triangles Sampling triangles: –When is a pixel inside a triangle? –Given a pixel, which triangle does it lie in? Point location Triangle representation: –Triangle defined by three vertices, in known order (clockwise, ccw) We care most about triangles for 3D graphics We care most about rectangles for 2D graphics Filling more general shapes is difficult
10/15/02 (c) 2002 University of Wisconsin, CS559 What is inside? Assume sampling with an array of spikes If spike is inside, pixel is inside
10/15/02 (c) 2002 University of Wisconsin, CS559 What is inside? Assume sampling with an array of spikes If spike is inside, pixel is inside
10/15/02 (c) 2002 University of Wisconsin, CS559 Ambiguous Case Ambiguous cases: What if a pixel lies on an edge? –Problem because if two polygons share a common edge, we don’t want pixels on the edge to belong to both –Ambiguity would lead to different results if the drawing order were different Rule: if (x+ , y+ ) is in, (x,y) is in ( is a small number) What if a pixel is on a vertex? Does our rule still work? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
10/15/02 (c) 2002 University of Wisconsin, CS559 Ambiguous Case 1 Rule: –On edge? If (x+ , y+ ) is in, pixel is in –Which pixels are colored?
10/15/02 (c) 2002 University of Wisconsin, CS559 Ambiguous Case 1 Rule: –Keep left and bottom edges –Assuming y increases in the up direction –If rectangles meet at an edge, how often is the edge pixel drawn? ?
10/15/02 (c) 2002 University of Wisconsin, CS559 Ambiguous Case 2
10/15/02 (c) 2002 University of Wisconsin, CS559 Ambiguous Case 2 ? ? ? ? or
10/15/02 (c) 2002 University of Wisconsin, CS559 Really Ambiguous We will accept ambiguity in such cases –The center pixel may end up colored by one of two polygons in this case –Which two?
10/15/02 (c) 2002 University of Wisconsin, CS559 Exploiting Coherence When filling a triangle (or polygon) –Several contiguous pixels along a row tend to be in the triangle - a span of pixels Scanline coherence –Consider whole spans, not individual pixels –The pixels required don’t vary much from one span to the next Edge coherence –Incrementally update the span endpoints
10/15/02 (c) 2002 University of Wisconsin, CS559 Sweep Fill Algorithms Algorithmic issues: –Reduce to filling many spans –Which edges define the span of pixels to fill? –How do you update these edges when moving from span to span? –What happens when you cross a vertex?
10/15/02 (c) 2002 University of Wisconsin, CS559 Spans Process - fill the bottom horizontal span of pixels; move up and keep filling Have x min, x max for each span Define: –floor(x): largest integer < x (not standard floor) –ceiling(x): smallest integer >=x Fill from ceiling(x min ) up to floor(x max ) Consistent with convention
10/15/02 (c) 2002 University of Wisconsin, CS559 Updating Edges Each edge is a line of the form: Next row is: So, each current edge can have it’s x position updated by adding a constant stored with the edge Other values may also be updated, such as depth or color information For max efficiency, use a version of the midpoint algorithm
10/15/02 (c) 2002 University of Wisconsin, CS559 When are Edges Relevant? Edge is relevant when y>=y min and y<y max of edge What about horizontal edges? –m’ is infinite
10/15/02 (c) 2002 University of Wisconsin, CS559 Avoiding Floating Point For edge, m= x/ y, which is a rational number View x as xi+xn/ y, with xn< y. Store xi and xn Then x->x+m’ is given by: –xn=xn+ x –if (xn>= y) { xi=xi+1; xn=xn- y } Advantages: –no floating point –can tell if x is an integer or not, and get floor(x) and ceiling(x) easily, for the span endpoints