Presentation is loading. Please wait.

Presentation is loading. Please wait.

22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff.

Similar presentations


Presentation on theme: "22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff."— Presentation transcript:

1 22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff Parker

2 23 Objectives Introduce basic implementation strategies Clipping Drawing lines Anti-aliasing Drawing Circles Scan conversion Two contributions from Robert Sproull coauthor of first text on Computer Graphics

3 24 Overview At end of the geometric pipeline, vertices have been assembled into primitives Must clip primitives that are outside view frustum Algorithms based on representing primitives by lists of vertices Must find which pixels can be affected by each primitive Fragment generation Rasterization or scan conversion

4 25 Required Tasks Clipping Rasterization or scan conversion Transformations Some tasks deferred until fragment processing Hidden surface removal Antialiasing

5 26 Rasterization Meta Algorithms Consider two approaches to rendering a scene with opaque objects For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel Ray tracing paradigm For every object, determine which pixels it covers and shade these pixels Pipeline approach Must keep track of depths

6 27 Clipping 2D against clipping window 3D against clipping volume Easy for line segments polygons Hard for curves and text Convert to lines and polygons first

7 28 Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window Inefficient: one division per intersection

8 Cohen-Sutherland Algorithm http://www.cs.princeton.edu/~min/cs426/jar/clip.html

9 29 Cohen-Sutherland Algorithm Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window x = x max x = x min y = y max y = y min

10 30 The Cases Case 1: both endpoints of line segment inside all four lines Draw (accept) line segment as is Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment x = x max x = x min y = y max y = y min

11 31 The Cases Case 3: One endpoint inside, one outside Must do at least one intersection Case 4: Both outside May have part inside Must do at least one intersection x = x max x = x min y = y max

12 32 Defining Outcodes For each endpoint, define an outcode Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions b0b1b2b3b0b1b2b3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise

13 33 Using Outcodes Consider the 5 cases below AB: outcode(A) = outcode(B) = 0 Accept line segment

14 34 Using Outcodes CD: outcode (C) = 0, outcode(D)  0 Compute intersection Location of 1 in outcode(D) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections (see addition)

15 35 Using Outcodes EF: outcode(E) logically ANDed with outcode(F) (bitwise)  0 Both outcodes have a 1 bit in the same place Thus and is non-zero Line segment is outside of corresponding side of clipping window Reject

16 36 Using Outcodes GH and IJ: same outcodes, neither zero but logical AND yields zero – no easy reject Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm

17 Cohen-Sutherland Algorithm http://www.cs.princeton.edu/~min/cs426/jar/clip.html

18 37 Cohen Sutherland Efficiency In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step

19 38 Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes

20 39 Liang-Barsky Clipping Consider the parametric form of a line segment We can distinguish between the cases by looking at the ordering of the values of  where the line determined by the line segment crosses the lines that determine the window p(  ) = (1-  )p 1 +  p 2 1  0 p1p1 p2p2

21 40 Liang-Barsky Clipping In (a):  4 >  3 >  2 >  1 Intersect right, top, left, bottom: shorten In (b):  4 >  2 >  3 >  1 Intersect right, left, top, bottom: reject

22 41 Advantages Can accept/reject as easily as with Cohen- Sutherland Using values of , we do not have to use algorithm recursively as with C-S Extends to 3D

23 42 Clipping and Normalization General clipping in 3D requires intersection of line segments against arbitrary plane Example: oblique view

24 43 Plane-Line Intersections Note that any distance measure can be used e.g. Projection onto x axis

25 3 Rasterization Rasterization (scan conversion) Determine which pixels are inside primitive specified by a set of vertices Produces a set of fragments Fragments have a location (pixel location) and other attributes such color and texture coordinates that are determined by interpolating values at vertices Pixel colors determined later using color, texture, normal, and other vertex properties

26 4 Scan Conversion of Line Segments Start with line segment in window coordinates with integer values for endpoints Assume implementation has a write_pixel function y = mx + h

27 Draw a Line We start with the problem of drawing a line between two pixels Without loss, assume we have line with positive slope and p1 is on left of p2 We will derive Bresenham’s algorithm Derivation due to Sproull: Using Program Transformations to Derive Line-Drawing Algorithms

28 Goals We want to draw the right pixels (What are they?) We want to do it quickly We want to do it correctly We want to handle all cases

29 Take 1 float m = (y 2 -y 1 )/(x 2 -x 1 ); for (x = x 1 ; x <= x 2 ; x++) { y = m*(x - x 1 ) + y 1 draw_pixel(x, y) }

30 Speedup float m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y) y = y + m Replace multiplication with addition

31 Problems Works well if delta y <= delta x Otherwise, we need to reverse the roles So the “right” points mean At least one in each row and column from start to finish We add assumption that delta y <= delta x for now

32 Problems float m = (y 2 -y 1 )/(x 2 -x 1 ); for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y) y = y + m m is not an integer (But m is rational) Therefore values of y are not integral But we can only draw integral pixels

33 Draw Integeral Points m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, round(y)) y = y + m Since there is no round() in C, we must say draw_pixel(x, trunc(y + 1/2))

34 Error Term We want to minimize the error The best way to measure the error is the distance to the line, as measured by perpendicular Since we have fixed slope, this is proportional to the vertical distance We minimize by rounding (trunc(y+ 1/2))

35 9 Candidate Pixels 1  m  0 last pixel candidates Note that line could have passed through any part of this pixel

36 Examples Draw lines from (0, 0) to (8, n) for n = 0 through 8

37 Restate - move addition m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, trunc(y)) y = y + m x is an int, while m and y are real numbers But m is a rational number, so y is as well

38 Restate - move addition m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, trunc(y)) y = y + m We move addition of ½ outside loop to simplify draw_pixel(x, trunc(y + 1/2))

39 Split y m = (y 2 -y 1 )/(x 2 -x 1 ); y = y 1 + 1/2 Split y into two parts: integer and fraction y = y i + y f 0 <= y f < 1 y i = y 1 y f = 1/2 Goal: decide when y i increases

40 Restate with split y m = (y 2 -y 1 )/(x 2 -x 1 ); // 2/6 in our example y i = y 1; y f = 1/2 for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y i ) if (y f + m) >= 1 y i = y i + 1; // Bump y y f = (y f + m) - 1; else y f = (y f + m) y f = y f + m x is an int: m and y f are rational numbers x yiyiyiyi yfyfyfyf yiyiyiyi yfyfyfyf 001/2 105/6 217/611/6 313/6

41 Introduce variable r Recall m = ∆y / ∆x = (y 2 -y 1 )/(x 2 -x 1 ) So (m * ∆x) is an integer – multiply by x To convert 1/2 to an integer, multiply by 2 Introduce new form of error term Let r = 2∆y + 2(y f - 1)∆x At start y f = 1/2, so at start r = 2∆y – 2(1/2 – 1)∆x = 2∆y + ∆x

42 Change in r r = 2∆y + 2(y f - 1)∆x // r is a measure of error Increasing y f by slope m is increasing r by 2∆y r = 2∆y + 2(y f + m - 1)∆x = = 2∆y + 2(y f - 1)∆x + 2(∆y/∆x) ∆x = 2∆y + 2(y f - 1)∆x + 2∆y Decreasing y f by 1 is decreasing r by 2∆x r = 2∆y + 2(y f -1 - 1)∆x = 2∆y + 2(y f - 1)∆x - 2∆x

43 Restate with r m = (y 2 -y 1 )/(x 2 -x 1 ); y i = y 1; r = 2(y 2 -y 1 ) - (x 2 -x 1 ) for (x = x 1 ; x <= x 2 ; x++) draw_pixel(x, y i ) if (r >= 0) y i = y i + 1; r = r + 2∆y - 2∆x else r = r + 2∆y x yiyiyiyir 00-2 102 21-6 31-2

44 Reconsider All the steps seem small: let’s review We made assumptions to simplify things Boiled down to the following case: As x moves from left to right, which y? We have two choices: old y or y+1 ? ?

45 Jaggies Our lines have visible steps We are sampling the line at too coarse a level We could make the dots smaller Effect remains We could use a "low pass filter" to smooth it out Low pass filter smears details together

46 20 Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line original antialiased magnified

47 Wu Antialiasing Xialin Wu - An Efficient Antialiasing Technique Assume we have a function with 0 ≤ m ≤ 1 Consider this example: we have sampled function f at three points

48 Wu Antialiasing Given the graph in the upper part We would digitize this with as below First point is rounded down to lattice Second point is rounded up to lattice This misrepresents the shape Had we moved the graph up or down, would have digitized differently

49 Wu Antialiasing Idea: Bracket point with 2 pixels North-South when m < 1 East-West when m > 1 Darkness is proportional to closeness to line

50 Wu Antialiasing Work through an example We are using the ceiling and floor functions

51 Example Work through an example (0, 0) to (5, 2) Compute values of f(x) at x = {0, 1, 2, 3, 4, 5}

52 Example Work through an example (0, 0) to (5, 2) Compute Intensity at x = {0, 1, 2, 3, 4, 5}

53 Example Work through an example (0, 0) to (5, 2) Compute Intensity at x = {0, 1, 2, 3, 4, 5} 1, [2/5, 3/5], [4/5, 1/5], [1/5, 4/5], [3/5, 2/5], 1

54 Algorithm draw_pixel(x 0,y 0 ); draw_pixel(x 1,y 1 ); // Ends D = 0; d = floor(m2 n + 0.5); x 0 = x 0 + 1; x 1 = x 1 – 1; while (x 0 < x 1 ) { D = D + d; if (overflow) y 0 = y 0 + 1; y 1 = y 1 – 1 I(x 0,y 0 ) := I(x 1,y 1 ) := D div 2 k-n I(x 0,y 0 +1) := I(x 1,y 1 -1) := 1 - I(x 0,y 0 ) }

55 Example Work through an example (0, 0) to (5, 2) 2/5 = 0.01100110011…. 1, [2/5, 3/5], [4/5, 1/5], [1/5, 4/5], [3/5, 2/5], 1 xOverflowD 00 101100110011 211001100110 3Overflow00110011001 410011001100 5?11111111111

56 Example 2 Work through an example (0, 0) to (6, 2)

57 Line Width These two lines have the same amount of ink One is 40% longer Diagonal is less dense

58 19 Aliasing Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines

59 Gupta-Sproull Wu does not address the issue of line width Gupta and Sproull provided a way to sample Earlier than Wu's algorithm Can deal with arbitrary line widths Provide lookup table to compute the samples quickly Not as fast as Wu’s algorithm Idea: a low pass filter

60 Gupta-Sproull Problem: Wu does not address the issue of line width Gupta and Sproull provide a way to sample Can deal with arbitrary line widths Provide a way to compute the samples quickly Sample using a cone with radius 1, volume 1

61 Filter: Signal strength is proportional to the volume below cone above the line Uses lookup table to speed up computation Use approximate distance to line

62 21 Polygon Aliasing Aliasing problems can be serious for polygons Jaggedness of edges Small polygons neglected Need compositing so that color of one polygon does not totally determine color of pixel All three polygons should contribute to color

63 Drawing Circles Bresenham addressed the problem of drawing circles and ellipses We deal with range shown Slope goes from 0 to -1 Generate other 7 octants at same time

64 Drawing Circles Decision Function D(x, y) = x 2 + y 2 - R 2 Initialize error term e = -D(x, y) At each step, we update x Do we pick y, or y-1?

65 Update Error Term New error term based on old D(x, y) = x 2 + y 2 - R 2 If we move to (x+1, y) D(x+1, y) = D(x, y) + 2x + 1 If the next point is (x+1, y-1) D(x+1, y-1) = D(x, y) + 2(x-y) + 2

66 Drawing Circles void Circle( int cx, int cy, int radius ) { int x = 0, y = radius, e = 1-radius; DrawPoint( x + cx, y + cy ); while (y > x) { if (e < 0) // Go “east” e += 2*x + 3; else // Go “south-east” e += 2*(x-y) + 5; y--; x++; DrawPoint( x + cx, y + cy ); }

67 12 Polygon Scan Conversion Scan Conversion = Fill How to tell inside from outside Convex easy Non-simple difficult Odd even test Count edge crossings Winding number odd-even fill

68 13 Winding Number winding number = 2 winding number = 1

69 13 Winding Number Count clockwise encirclements of point Alternate definition of inside: inside if winding number  0 winding number = 2 winding number = 1

70 13 Scan Lines Sort vertices by x value Interpolate between left and right endpoints of scan line Sometimes a scan line intersects an odd number Count max or min vertex twice – coming and going Count ordinary vertex once

71 18 Data Structure

72 13 Edges Sort vertices by y value (height) Keep list of current edges hitting this scan line Move to next scan line A new vertex forces us to add new edge and/or drop old edge Keep intersections sorted by x Interpolate across scan line

73 17 Scan Line Fill Can also fill by maintaining a data structure of all intersections of polygons with scan lines Sort by scan line Fill each span vertex order generated by vertex list desired order

74 14 Filling in the Frame Buffer Fill at end of pipeline Convex Polygons only Nonconvex polygons assumed to have been tessellated (see example on prior page for why) Shades (colors) have been computed for vertices (Gouraud shading) Combine with z-buffer algorithm March across scan lines interpolating shades Incremental work small

75 15 Using Interpolation span C1C1 C3C3 C2C2 C5C5 C4C4 scan line C 1 C 2 C 3 specified by glColor or by vertex shading C 4 determined by interpolating between C 1 and C 2 C 5 determined by interpolating between C 2 and C 3 interpolate between C 4 and C 5 along span

76 13 Summary Rasterizing is an important problem Thousands of vertices, millions of fragments Must be done quickly, and accurately We have seen a number of clever algorithms Focus on what we can do quickly Integer operations on simple data


Download ppt "22 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Many changes – Jeff."

Similar presentations


Ads by Google