implementation dr. amy zhang. reading 2 hill, chapters 9.4-9.7 hill, chapter 10

88
Implementation Dr. Amy Zhang

Upload: phyllis-gibbs

Post on 19-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Implementation

Dr. Amy Zhang

Page 2: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Reading

2

Hill, Chapters 9.4-9.7 Hill, Chapter 10

Page 3: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Outline

3

The Rasterization Problem Scan converting lines Filling polygons

Clipping Hidden surface removal (z-buffer)

Page 4: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

3D Graphics Pipeline

4

The rasterization step scan converts the object into pixels

Page 5: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 6: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Implicit Lines

6

The implicit form of the slope‐intercept equation:

Page 7: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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 (x0,y0) and (x1,y1):

Page 8: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Example

8

What is the implicit equation of this line?

Page 9: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 10: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Example

10

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

Page 11: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

The Rasterization Problem

11

Primitives map to discrete display space

Page 12: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Solution

12

Selection of discrete representation values

Page 13: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Scan converting lines

13

Characterizing the problem: 2 cases Move vertical scanline from x0 to xn

Move horizontal scanline from bottom to top dot: center of pixel

Page 14: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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.

Page 15: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

15

DDA (Digital Differential Analyzer) Algorithm

Page 16: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

16

DDA Algorithm, Incremental Form

Page 17: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

17

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

Page 18: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Bresenham’s Algorithm

18

Allowable Pixel Selections Standard algorithm used in hardware/software

rasterizers.

Page 19: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

19

Iterating

Page 20: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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(xi+1, yi+1/2)<0, M lies above the line, chose Eif F(xi+1, yi+1/2)>0, M lies below the line, chose NE

Page 21: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

21

Calculating the decision function

Initial condition:(x0,y0): the point on the line

Page 22: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

22

Problem: Complete computation of d along the line

Solution: incremental calculation of di

Page 23: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

The code

23

Page 24: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Bresenham’s Algorithm

24

An example

Page 25: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

25

Page 26: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

26

Page 27: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Filling Polygons

27

Page 28: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

28

Scan Line Algorithm Compute the bounding pixels Fill the spans

Page 29: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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)

Page 30: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Filling polygons: scan line algorithm

30

http://www.cs.rit.edu/~icss571/filling/example.html

Page 31: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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: ymin

The maximum y value of the 2 vertices: ymax

The x value associated with the minimum y value: xval

1/The slope of the edge:1/m (?)

Page 32: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 33: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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.

Page 34: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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)

Page 35: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Filling the polygon

35

Scanline=11: update x = x +1/m sort by xval

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.

Page 36: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

36

Scanline+=1, until ymax 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.

Page 37: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 38: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

38

Now add the edges from the global edge table to the active edge table since ymin =scanline.

reorder

Page 39: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

39

Scanline=17: update = x +1/m, sort by xva

at x=12, parity = odd. draw pixels left to x=20, parity = even.l

Page 40: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

40

Scanline ++, until scanline=19

at x=15, parity = odd. draw pixels left to x=18, parity = even.

Page 41: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

41

scanline++ remove the edges if ymax=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 ymin =scanline.

Iterate until both tables are empty.

Page 42: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

42

Demo Algorithm1. Initiate the GET, scanline, AET2. Draw the pixels based on AET and the parity3. Scanline++4. Remove the edges from AET is

scanline=ymax ,terminate if both AET and GET are empty

5. Update X values6. Add edges to AET if GET is not empty7. Reorder AET8. Goto step 2.

Page 43: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Problem

43

Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage

by ideal line

Page 44: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 45: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Outline

45

The Rasterization Problem Scan converting lines Filling polygons

Clipping Hidden surface removal (z-buffer)

Page 46: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Clipping

46

Clipping Against a Rectangular Region Multiple Cases

Page 47: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Division of Space

47

Page 48: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Cohen Sutherland Clipping: Outcodes

48

Page 49: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Cohen Sutherland Clipping: Region Outcodes

49

Page 50: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

50

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

Page 51: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

51

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

Page 52: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

52

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

Page 53: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

53

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

Page 54: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Any suggestions

54

to handle the non-trival cases Find the intersecting points

Page 55: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Algorithm

55

1. Compute the outcodes for the two vertices2. Test for trivial acceptance or rejection3. 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

Page 56: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Example 1

56

Page 57: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

57

Page 58: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Example 2

58

Page 59: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

59

Page 60: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

60

Page 61: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

61

Page 62: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 63: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Parametric Representation of Lines

63

Page 64: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Liang Barsky Parametric Clipping

64

Page 65: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Potentially Entering (PE) andPotentially Leaving (PL) Points

65

Page 66: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Liang Barsky Clipping: Computing theIntersection

66

Page 67: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Liang Barsky Clipping: Potentially Leaving vs. Potentially Entering

67

Page 68: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Algorithm Strategy

68

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

Page 69: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Pseudocode

69

Page 70: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Sutherland Hodgeman Pipeline Clipping

70

Page 71: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Polygon Clipping: Convex Polygons

71

Page 72: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Polygon Clipping: The Convexity Problem

72

Page 73: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Outline

73

The Rasterization Problem Scan converting lines Filling polygons

Clipping Hidden surface removal (z-buffer)

Page 74: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

One Triangle

74

With one triangle, things are simple Fragments never overlap!

Page 75: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Two Triangles

75

Things get more complicated with multiple triangles

Fragments might overlap in screen space!

Page 76: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Fragments vs. Pixels

76

Each pixel has a unique framebuffer (image) location

But multiple fragments may end up at same address

Page 77: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Which triangle wins?

77

Two possible cases:

green triangle on toporange triangle on top

Page 78: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Which (partial) triangle wins?

78

Many other cases possible!

intersection #1 intersection #2

Page 79: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Hidden Surface Removal

79

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

Page 80: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

First Attempt: Painter’s Algorithm

80

Sort triangles (using z values in eye space) Draw triangles from back to front

Page 81: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Problems?

81

Correctness issues: Intersections Cycles Solve by splitting triangles, but ugly and

expensive Efficiency (sorting)

Page 82: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 83: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

83

Example: fragment from green triangle has z value of 0.7

Page 84: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

84

Example: fragment from red triangle has z value of 0.3

Page 85: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

85

Since 0.3 < 0.7, the red fragment wins

Page 86: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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)

Page 87: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

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

Page 88: Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters 9.4-9.7  Hill, Chapter 10

Z‐buffer Algorithm Properties

88

What makes this method nice? simple (faciliates hardware implementation) handles intersections handles cycles draw opaque polygons in any order