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

Post on 19-Dec-2015

223 views 0 download

transcript

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)

3D Graphics Pipeline

4

The rasterization step scan converts the object into pixels

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

Implicit Lines

6

The implicit form of the slope‐intercept equation:

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

Example

8

What is the implicit equation of this line?

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

Example

10

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

The Rasterization Problem

11

Primitives map to discrete display space

Solution

12

Selection of discrete representation values

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

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

DDA (Digital Differential Analyzer) Algorithm

16

DDA Algorithm, Incremental Form

17

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

Bresenham’s Algorithm

18

Allowable Pixel Selections Standard algorithm used in hardware/software

rasterizers.

19

Iterating

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

21

Calculating the decision function

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

22

Problem: Complete computation of d along the line

Solution: incremental calculation of di

The code

23

Bresenham’s Algorithm

24

An example

25

26

Filling Polygons

27

28

Scan Line Algorithm Compute the bounding pixels Fill the spans

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)

Filling polygons: scan line algorithm

30

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

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 (?)

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

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

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)

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.

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.

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

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

reorder

39

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

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

40

Scanline ++, until scanline=19

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

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.

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.

Problem

43

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

by ideal line

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

Outline

45

The Rasterization Problem Scan converting lines Filling polygons

Clipping Hidden surface removal (z-buffer)

Clipping

46

Clipping Against a Rectangular Region Multiple Cases

Division of Space

47

Cohen Sutherland Clipping: Outcodes

48

Cohen Sutherland Clipping: Region Outcodes

49

50

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

51

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

52

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

53

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

Any suggestions

54

to handle the non-trival cases Find the intersecting points

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

Example 1

56

57

Example 2

58

59

60

61

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

Parametric Representation of Lines

63

Liang Barsky Parametric Clipping

64

Potentially Entering (PE) andPotentially Leaving (PL) Points

65

Liang Barsky Clipping: Computing theIntersection

66

Liang Barsky Clipping: Potentially Leaving vs. Potentially Entering

67

Algorithm Strategy

68

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

Pseudocode

69

Sutherland Hodgeman Pipeline Clipping

70

Polygon Clipping: Convex Polygons

71

Polygon Clipping: The Convexity Problem

72

Outline

73

The Rasterization Problem Scan converting lines Filling polygons

Clipping Hidden surface removal (z-buffer)

One Triangle

74

With one triangle, things are simple Fragments never overlap!

Two Triangles

75

Things get more complicated with multiple triangles

Fragments might overlap in screen space!

Fragments vs. Pixels

76

Each pixel has a unique framebuffer (image) location

But multiple fragments may end up at same address

Which triangle wins?

77

Two possible cases:

green triangle on toporange triangle on top

Which (partial) triangle wins?

78

Many other cases possible!

intersection #1 intersection #2

Hidden Surface Removal

79

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

First Attempt: Painter’s Algorithm

80

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

Problems?

81

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

expensive Efficiency (sorting)

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

Example: fragment from green triangle has z value of 0.7

84

Example: fragment from red triangle has z value of 0.3

85

Since 0.3 < 0.7, the red fragment wins

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)

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

Z‐buffer Algorithm Properties

88

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