290 likes | 310 Views
This text discusses various algorithms for line drawing and polygon filling, including Bresenham's algorithm and sweep-fill methods. It addresses issues such as anti-aliasing and coherence in scanline and edge-based approaches.
E N D
Where We Stand • At this point we know how to: • Convert points from local to window coordinates • Clip polygons and lines to the view volume • Next thing: • Determine which pixels are covered by any given line or polygon • Anti-Aliasing
Drawing Lines • Task: Decide which pixels 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!) • Issues: • If slope between -1 and 1, one pixel per column. Otherwise, one pixel per row • Constant brightness? • Anti-aliasing? (Getting rid of the “jaggies”)
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:
Bresenham’s Algorithm Overview • Plot the pixel whose y-value is closest to the line • Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi) • Idea: compute a decision variable • Value that will determine which pixel to draw • Easy to update from one pixel to the next
Decision Variable • Decision variable is: yi+1 d2 d1 yi xi xi+1
What Can We Decide? • d1<d2 => pi negative => next point at (xi+1,yi) • d1>d2 => pi positive => next point at (xi+1,yi+1) • So, we know what to draw based on the decision variable • How do we update it? What is pk+1?
Updating The Decision Variable • If yi+1=yi+1: • If yi+1=yi: • What is p1 (assuming integer endpoints)?
Bresenham’s Algorithm • For integers, slope between 0 and 1: • x=x1, y=y1, p=2 dy - dx, draw (x, y) • until x=x2 • x=x+1 • p>0 ? y=y+1, draw (x, y), p=p+2 y - 2 x • p<0? y=y, draw (x, y), p=p+2 y • Compute the constants once at the start • Only does add and comparisons • Floating point has slightly more difficult initialization
Example: (2,2) to (7,6) x=5, y=4 i x y p 1 2 2 3 2 3 3 1 3 4 4 -1 4 5 4 7 5 6 5 5 6 7 6 3 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8
Filling polygons • Sampling polygons: • When is a pixel inside a polygon? • Given a pixel, which polygon does it lie in? • Polygon issues: • Polygon defined by a list of edges - each is a pair of vertices • All vertices are inside the view volume and map to valid pixels. (Clipping gave us this.) Also, assume integers in window coordinates
What is inside - 1? • Easy for simple polygons - no self intersections • OpenGL requires these. Undefined for other cases • OpenGL also requires convex polygons • For general polygons, three rules are possible: • non-exterior rule • non-zero winding number rule • parity rule
Parity Non-zero Winding No. Polygon Non-exterior
What is inside - 2? • Assume sampling with an array of spikes • If spike is inside, pixel is inside
What is inside - 2? • Assume sampling with an array of spikes • If spike is inside, pixel is inside
Rules • Ambiguous cases: • On edge? if (x+d, y+e) is in, pixel is in • Keeps left and bottom edges • What if it’s on a vertex? Which do we want to keep?
Ambiguous Case 1 • Rule: • On edge? If (x+d, y+e) is in, pixel is in • Which pixels are colored?
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?
Ambiguous Case 2 ? ? or ? ? ? ?
Exploiting Coherence • When filling a polygon • Several contiguous pixels along a row tend to be in the polygon - 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
Sweep Fill Algorithms Watt Sect 6.4.2 • 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?
Spans • Process - fill the bottom horizontal span of pixels; move up and keep filling • Have xmin, xmax for each span • Define: • floor(x): largest integer < x • ceiling(x): smallest integer >=x • Fill from ceiling(xmin) up to floor(xmax) • Consistent with convention
Algorithm • For each row in the polygon: • Throw away irrelevant edges • Obtain newly relevant edges • Fill span • Update current edges • Issues: • How do we update existing edges? • When is an edge relevant/irrelevant? • All can be resolved by referring to our convention about what polygon pixel belongs to
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
When are Edges Relevant (1) • Use figures and convention to determine when edge is irrelevant • For y<ymin and y>=ymax of edge • Similarly, edge is relevant when y>=ymin and y<ymax of edge • What about horizontal edges? • m’ is infinite
When are Edges Relevant (2) 2 Convex polygon: Always only two edges active 1,2 1 1,3 3 4 3,4
When are Edges Relevant (3) 2 2? Horizontal edges Ignore them! 1 3 1,3 4? 4
Maintain a list of active edges in case there are multiple spans of pixels - known as Active Edge List. For each edge on the list, must know: x-value, maximum y value of edge, m’ Maybe also depth, color… Keep edges in a table, indexed by minimum y value - Edge Table For row = min to row=max AEL=append(AEL, ET(row)); remove edges whose ymax=row sort AEL by x-value fill spans update each edge in AEL Sweep Fill Details