Presentation is loading. Please wait.

Presentation is loading. Please wait.

© University of Wisconsin, CS559 Fall 2004

Similar presentations


Presentation on theme: "© University of Wisconsin, CS559 Fall 2004"— Presentation transcript:

1 © University of Wisconsin, CS559 Fall 2004
Last Time Clipping Liang-Barksy Weiler-Atherton Drawing points Intro to drawing lines Midterm Oct 28 10/21/04 © University of Wisconsin, CS559 Fall 2004

2 © University of Wisconsin, CS559 Fall 2004
Today Drawing lines Filling polygons 10/21/04 © University of Wisconsin, CS559 Fall 2004

3 © University of Wisconsin, CS559 Fall 2004
Drawing Points When points are mapped into window coordinates, they could land anywhere – not just at a pixel center Solution is the simple, obvious one Map to window space Fill the closest pixel Can also specify a radius – fill a square of that size, or fill a circle Square is faster What function are we sampling with? 10/21/04 © University of Wisconsin, CS559 Fall 2004

4 © University of Wisconsin, CS559 Fall 2004
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? Lines of the same length should light the same number of pixels (we normally ignore this) Anti-aliasing? (Getting rid of the “jaggies”) Sampling theory? 10/21/04 © University of Wisconsin, CS559 Fall 2004

5 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, modify for real numbers 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/21/04 © University of Wisconsin, CS559 Fall 2004

6 Bresenham’s Algorithm Overview
Aim: For each x, 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 Bresenham’s algorithm is the midpoint algorithm for lines Other midpoint algorithms for conic sections (circles, ellipses) 10/21/04 © University of Wisconsin, CS559 Fall 2004

7 Bresenham’s Algorithm Overview
Input: Line with integer endpoints, slope 0<m<1 Aim: For each x, 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 Bresenham’s algorithm is the midpoint algorithm for lines Other midpoint algorithms for conic sections (circles, ellipses) 10/21/04 © University of Wisconsin, CS559 Fall 2004

8 © University of Wisconsin, CS559 Fall 2004
Midpoint Methods Consider the midpoint between (xi+1,yi+1) and (xi+1,yi) If it’s above the line, we choose (xi+1,yi), otherwise we choose (xi+1,yi+1) yi+1 yi+1 yi yi xi xi+1 xi xi+1 Choose (xi+1,yi) Choose (xi+1,yi+1) 10/21/04 © University of Wisconsin, CS559 Fall 2004

9 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 di = 2F(xi+1,yi+0.5) The factor of two makes the math easier 10/21/04 © University of Wisconsin, CS559 Fall 2004

10 © University of Wisconsin, CS559 Fall 2004
What Can We Decide? di negative => next point at (xi+1,yi) di positive => next point at (xi+1,yi+1) At each point, we compute di and decide which pixel to draw How do we update it? What is di+1? 10/21/04 © University of Wisconsin, CS559 Fall 2004

11 Updating The Decision Variable
dk+1 is the old value, dk, plus an increment: If we chose yi+1=yi+1: If we chose yi+1=yi: What is d1 (assuming integer endpoints)? Notice that we don’t need c any more 10/21/04 © University of Wisconsin, CS559 Fall 2004

12 Bresenham’s Algorithm
For integers, slope between 0 and 1: x=x1, y=y1, d=2dy - dx, draw (x, y) until x=x2 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 For floating point, initialization is harder, x and y will be floating point, but still no rounding required 10/21/04 © University of Wisconsin, CS559 Fall 2004

13 © University of Wisconsin, CS559 Fall 2004
Example: (2,2) to (7,6) x=5, y=4 x y d 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 10/21/04 © University of Wisconsin, CS559 Fall 2004

14 © University of Wisconsin, CS559 Fall 2004
Filling polygons Sampling polygons: When is a pixel inside a polygon? Given a pixel, which polygon does it lie in? Point location Polygon representation: 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). Assume integers in window coordinates to simplify things for now 10/21/04 © University of Wisconsin, CS559 Fall 2004

15 © University of Wisconsin, CS559 Fall 2004
What is inside - 1? Easy for simple polygons - no self intersections or holes OpenGL requires these. Undefined for other cases OpenGL also requires convex polygons – easy triangulation For general polygons, three rules are possible: Non-exterior rule: A point is inside if every ray to infinity intersects the polygon Non-zero winding number rule: Draw a ray to infinity that does not hit a vertex, if the number of edges crossing in one direction is not equal to the number crossing the other way, the point is inside Parity rule: Draw a ray to infinity and count the number or edges that cross it. If even, the point is outside, if odd, it’s inside 10/21/04 © University of Wisconsin, CS559 Fall 2004

16 © University of Wisconsin, CS559 Fall 2004
Inside/Outside Rules Polygon Non-exterior Non-zero Winding No. Parity 10/21/04 © University of Wisconsin, CS559 Fall 2004

17 © University of Wisconsin, CS559 Fall 2004
What is inside - 2? Assume sampling with an array of spikes If spike is inside, pixel is inside 10/21/04 © University of Wisconsin, CS559 Fall 2004

18 © University of Wisconsin, CS559 Fall 2004
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 Aim: A consistent rule that (almost) always results in one polygon “owning” the edge One Rule: if (x+, y+) is in, (x,y) is in (for arbitrarily small ) Another: edge belongs to polygon on same side as some off-screen point What if a pixel is on a vertex? Do our rules still work? 10/21/04 © University of Wisconsin, CS559 Fall 2004

19 © University of Wisconsin, CS559 Fall 2004
Ambiguous Case 1 Rule 1: On edge? If (x+, y+) is in, pixel is in Which pixels are colored? Rule 2: If polygon is on same side as point (huge,huge) Which pixels? Corners? 10/21/04 © University of Wisconsin, CS559 Fall 2004

20 © University of Wisconsin, CS559 Fall 2004
Ambiguous Case 2 10/21/04 © University of Wisconsin, CS559 Fall 2004

21 © University of Wisconsin, CS559 Fall 2004
Ambiguous Case 2 ? ? ? or ? 10/21/04 © University of Wisconsin, CS559 Fall 2004

22 © University of Wisconsin, CS559 Fall 2004
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? 1 6 2 5 3 4 10/21/04 © University of Wisconsin, CS559 Fall 2004

23 © University of Wisconsin, CS559 Fall 2004
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 10/21/04 © University of Wisconsin, CS559 Fall 2004

24 © University of Wisconsin, CS559 Fall 2004
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/21/04 © University of Wisconsin, CS559 Fall 2004

25 © University of Wisconsin, CS559 Fall 2004
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/21/04 © University of Wisconsin, CS559 Fall 2004

26 © University of Wisconsin, CS559 Fall 2004
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? What are the endpoints of a span All can be resolved by referring to our convention about what polygon a pixel belongs to 10/21/04 © University of Wisconsin, CS559 Fall 2004

27 When are Edges Relevant (1)
Use figures and convention to determine which edges are needed for which scanlines (rows of pixels) Edges from ymin to ymax are relevant for what y values? What about horizontal edges? ymax ymin 10/21/04 © University of Wisconsin, CS559 Fall 2004

28 When are Edges Relevant (2)
Convex polygon: Always only two edges active 2 1,2 1 1,3 3 3,4 4 10/21/04 © University of Wisconsin, CS559 Fall 2004

29 © University of Wisconsin, CS559 Fall 2004
Spans Fill rows from bottom to top, one at a time Have pixels xmin, xmax for each span Fill [xmin, xmax) Include xmin, exclude xmax xmax xmin, 10/21/04 © University of Wisconsin, CS559 Fall 2004

30 © University of Wisconsin, CS559 Fall 2004
Tracking xmin and xmax Use a variant of the midpoint method, but not quite the same Which pixel, with respect to the edge, do we wish to choose for xmin and xmax? What is the decision variable? 10/21/04 © University of Wisconsin, CS559 Fall 2004

31 Midpoint Method For Polygon Edges
Consider a left edge with slope [0-1] Always want to choose point on line or below Consider the point (xi+1,yi+1). Is it above or below the line? yi+1 yi+1 yi yi xi xi+1 xi xi+1 Choose (xi+1,yi+1) Choose (xi+1,yi) 10/21/04 © University of Wisconsin, CS559 Fall 2004

32 © University of Wisconsin, CS559 Fall 2004
Sweep Fill Details For convex polygons there are always 2 edges, a left and a right Fixes the amount of memory required, good for hardware Can generate memory addresses (for pixel writes) efficiently You know address of leftmost pixel, and address of rightmost, can fill all in between quickly Other values may also be updated, such as z, w or color information Use a midpoint-like rule for these too Modern hardware, with floating point frame buffers, just interpolates 10/21/04 © University of Wisconsin, CS559 Fall 2004

33 Extending to Arbitrary Polygons
Can be more than one span in any row Keep sorted list of edges across row, fill between pairs of edges 10/21/04 © University of Wisconsin, CS559 Fall 2004

34 © University of Wisconsin, CS559 Fall 2004
Anti-Aliasing Recall: We can’t sample and then accurately reconstruct an image that is not band-limited Infinite Nyquist frequency Attempting to sample sharp edges gives “jaggies”, or stair-step lines Solution: Band-limit by filtering (pre-filtering) What sort of filter will give a band-limited result? But when doing computer rendering, we don’t have the original continuous function 10/21/04 © University of Wisconsin, CS559 Fall 2004

35 Alpha-based Anti-Aliasing
1/8 Set the  of a pixel to simulate a thick line The pixel gets the line color, but with <=1 This supports the correct drawing of primitives one on top of the other Draw back to front, and composite each primitive over the existing image Only some hidden surface removal algorithms support it 1/4 .914 1/8 1/4 .914 1/4 1/8 .914 1/4 1/8 10/21/04 © University of Wisconsin, CS559 Fall 2004

36 © University of Wisconsin, CS559 Fall 2004
Calculating  Consider a line as having thickness (all good drawing programs do this) Consider pixels as little squares Set  according to the proportion of the square covered by the line The sub-pixel coverage interpretation of  1/8 1/4 .914 1/8 1/4 .914 1/4 1/8 .914 1/4 1/8 10/21/04 © University of Wisconsin, CS559 Fall 2004

37 © University of Wisconsin, CS559 Fall 2004
Weighted Sampling Instead of using the proportion of the area covered by the line, use convolution to do the sampling Equivalent to filtering the line then point sampling the result Place the “filter” at each pixel, and integrate product of pixel and line Common filters are cones (like Bartlett) or Gaussians 10/21/04 © University of Wisconsin, CS559 Fall 2004

38 Post-Filtering (Supersampling)
Sample at a higher resolution than required for display, and filter image down Easy to implement in hardware Typical is 2x2 sampling per pixel, with simple averaging to get final What kind of filter? More advanced methods generate different samples (eg. not on regular grid) and filter properly Issues of which samples to take, and how to filter them 10/21/04 © University of Wisconsin, CS559 Fall 2004


Download ppt "© University of Wisconsin, CS559 Fall 2004"

Similar presentations


Ads by Google