Download presentation

Presentation is loading. Please wait.

Published byPhyllis Gibbs Modified over 6 years ago

1
Implementation Dr. Amy Zhang

2
Reading 2 Hill, Chapters 9.4-9.7 Hill, Chapter 10

3
Outline 3 The Rasterization Problem Scan converting lines Filling polygons Clipping Hidden surface removal (z-buffer)

4
3D Graphics Pipeline 4 The rasterization step scan converts the object into pixels

5
Implicit Lines 5 Implicit equation in two dimensions: Points with f(x,y) = 0 are on the line Points with f(x,y) != 0 are not on the line

6
Implicit Lines 6 The implicit form of the slope ‐ intercept equation:

7
7 The slope ‐ intercept form can not represent some lines, such as x = 0. A more general implicit form is more useful: y = mx + b or The implicit line through two points (x 0,y 0 ) and (x 1,y 1 ):

8
Example 8 What is the implicit equation of this line?

9
Example 9 Solution 1: ‐ 2X+4Y=0 Solution 2: 2X ‐ 4Y=0 What’s the lesson here? k f(x,y) = 0 is the same line, for any value of k

10
Example 10 The value of f(x,y) = ‐ 2x +4y tells us which side of the line a point (x,y) is on

11
The Rasterization Problem 11 Primitives map to discrete display space

12
Solution 12 Selection of discrete representation values

13
Scan converting lines 13 Characterizing the problem: 2 cases Move vertical scanline from x 0 to x n Move horizontal scanline from bottom to top dot: center of pixel

14
14 Exactly one pixel per column: Fewer: disconnected More: too thick Only discuss m≤1 case The strategy Pick pixels closest to endpoints Select in between pixels “closest”to ideal line Objective: To minimize the required calculations.

15
15 DDA (Digital Differential Analyzer) Algorithm

16
16 DDA Algorithm, Incremental Form

17
17 Disadvantage of DDA algorithm: Floating point addition. Slow!! Solution: integer operation

18
Bresenham’s Algorithm 18 Allowable Pixel Selections Standard algorithm used in hardware/software rasterizers.

19
19 Iterating

20
20 Decision function: Q(x,y)=y-mx-b above line L: +; on: 0; below: - F(x,y)=ax+by+c=0 (implicit equation of the line) if F(x i +1, y i +1/2)<0, M lies above the line, chose E if F(x i +1, y i +1/2)>0, M lies below the line, chose NE

21
21 Calculating the decision function Initial condition: (x0,y0): the point on the line

22
22 Problem: Complete computation of d along the line Solution: incremental calculation of d i

23
The code 23

24
Bresenham’s Algorithm 24 An example

25
25

26
26

27
Filling Polygons 27

28
28 Scan Line Algorithm Compute the bounding pixels Fill the spans

29
29 Scan Line Algorithm Find the intersections of current scan line with all edges of the polygon. Sort the intersections by increasing x coordinate. Fill in pixels that lie between pairs of intersections that lie interior to the polygon using the odd/even parity rule. Parity: even, change parity once encounter an edge Special parity: no change of the parity (draw 1 pixel)

30
Filling polygons: scan line algorithm 30 http://www.cs.rit.edu/~icss571/filling/example.html

31
Edge table 31 Initializing the all_edges table: determine how the polygon's vertices are related Each adjacent set of vertices defines an edge. For each edge, we need to keep: The minimum y value of the 2 vertices: y min The maximum y value of the 2 vertices: y max The x value associated with the minimum y value: x val 1/The slope of the edge:1/m (?)

32
Global edge table 32 Initializing the Global Edge Table (GET): keep track of the edges that are still needed to complete the polygon. place the edges with m≠0 (?) be inserted with edges grouped by increasing minimum y values and further by x values

33
Active Edge Table 33 Initializing Parity even since no edges have been crossed yet. Initializing the Scan-Line is equal to the lowest y value for all of the global edges.(10) Initializing the Active Edge Table (AET) keep track of the ordered edges that are intersected by the current scan-line.

34
34 Scanline = 10: at x=10, parity = odd. draw pixels left to x=22, parity = even. at x=28, draw a pixel (the special parity case)

35
Filling the polygon 35 Scanline=11: update x = x +1/m sort by x val at x=10, parity = odd. draw pixels left to x=23, parity = even. at x=27, parity = odd. draw pixels left to x=28, parity = even.

36
36 Scanline+=1, until y max is equal to the next scan-line Scanline = 15: at x=10, parity = odd. draw pixels left to x=22, parity = even. at x=27, parity = odd. draw pixels left to x=28, parity = even.

37
37 Scanline++ (16) remove the edges if ymax=scanline from the active edge table (for the edges at indices 0, 2, and 3) update the x values for all remaining edges in the active edge table

38
38 Now add the edges from the global edge table to the active edge table since y min =scanline. reorder

39
39 Scanline=17: update = x +1/m, sort by x va at x=12, parity = odd. draw pixels left to x=20, parity = even. l

40
40 Scanline ++, until scanline=19 at x=15, parity = odd. draw pixels left to x=18, parity = even.

41
41 scanline++ remove the edges if y max =scanline from the active edge table (for the edges at indices 0, 1) add the edges from the global edge table to the active edge table if y min =scanline. Iterate until both tables are empty.

42
42 Demo Algorithm 1. Initiate the GET, scanline, AET 2. Draw the pixels based on AET and the parity 3. Scanline++ 4. Remove the edges from AET is scanline=ymax,terminate if both AET and GET are empty 5. Update X values 6. Add edges to AET if GET is not empty 7. Reorder AET 8. Goto step 2.

43
Problem 43 Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line

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

45
Outline 45 The Rasterization Problem Scan converting lines Filling polygons Clipping Hidden surface removal (z-buffer)

46
Clipping 46 Clipping Against a Rectangular Region Multiple Cases

47
Division of Space 47

48
Cohen Sutherland Clipping: Outcodes 48

49
Cohen Sutherland Clipping: Region Outcodes 49

50
50 Trivial Acceptance: O( P0 ) = O( P1) = 0

51
51 Trivial Rejection: O( P0) & O( P1) (bitwise AND) ≠ 0

52
52 O( P0 ) =0, O( P1) ≠ 0

53
53 O( P0) &O( P1) (bitwise AND)= 0

54
Any suggestions 54 to handle the non-trival cases Find the intersecting points

55
Algorithm 55 1. Compute the outcodes for the two vertices 2. Test for trivial acceptance or rejection 3. Select a vertex for which outcode is not zero 1. There will always be one 4. Select the first nonzero bit in the outcode to define the boundary against which the line segment will be clipped 5. Compute the intersection and replace the vertex with the intersection point 6. Compute the outcode for the new point and iterate

56
Example 1 56

57
57

58
Example 2 58

59
59

60
60

61
61

62
Advantages/Extension 62 Easily extended to 3 dimensions by adding two bits to the outcode for the z axis. Calculations then reduce to intersection of line with plane Very efficient when most segments can either be trivially accepted or trivially rejected http://www.cs.princeton.edu/%7Emin/cs426/jar/clip.html

63
Parametric Representation of Lines 63

64
Liang Barsky Parametric Clipping 64

65
Potentially Entering (PE) and Potentially Leaving (PL) Points 65

66
Liang Barsky Clipping: Computing the Intersection 66

67
Liang Barsky Clipping: Potentially Leaving vs. Potentially Entering 67

68
Algorithm Strategy 68 Find the largest PE greater than zero. Find the smallest PL less than one. Reject the segment if PE > PL.

69
Pseudocode 69

70
Sutherland Hodgeman Pipeline Clipping 70

71
Polygon Clipping: Convex Polygons 71

72
Polygon Clipping: The Convexity Problem 72

73
Outline 73 The Rasterization Problem Scan converting lines Filling polygons Clipping Hidden surface removal (z-buffer)

74
One Triangle 74 With one triangle, things are simple Fragments never overlap!

75
Two Triangles 75 Things get more complicated with multiple triangles Fragments might overlap in screen space!

76
Fragments vs. Pixels 76 Each pixel has a unique framebuffer (image) location But multiple fragments may end up at same address

77
Which triangle wins? 77 Two possible cases: green triangle on top orange triangle on top

78
Which (partial) triangle wins? 78 Many other cases possible! intersection #1 intersection #2

79
Hidden Surface Removal 79 Idea: keep track of visible surfaces Typically, we see only the front ‐ most surface Exception: transparency

80
First Attempt: Painter’s Algorithm 80 Sort triangles (using z values in eye space) Draw triangles from back to front

81
Problems? 81 Correctness issues: Intersections Cycles Solve by splitting triangles, but ugly and expensive Efficiency (sorting)

82
The Depth Buffer (Z ‐ buffer) 82 Perform hidden surface removal per ‐ fragment Idea: Each fragment gets a z value in screen space Keep only the fragment with the smallest z value

83
83 Example: fragment from green triangle has z value of 0.7

84
84 Example: fragment from red triangle has z value of 0.3

85
85 Since 0.3 < 0.7, the red fragment wins

86
86 Lots of fragments might map to the same pixel location How to track their z ‐ values? Solution: z ‐ buffer (2D buffer, same size as image)

87
Z ‐ buffer Algorithm 87 Let CB be color buffer, ZB be z ‐ buffer Initialize z ‐ buffer contents to 1.0 (far away) For each triangle T Rasterize T to generate fragments For each fragment F with screen position (x,y,z) and color value C If ( z < ZB[x,y] ) then Update color: CB[x,y] = C Update depth: ZB[x,y] = z

88
Z ‐ buffer Algorithm Properties 88 What makes this method nice? simple (faciliates hardware implementation) handles intersections handles cycles draw opaque polygons in any order

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google