1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

68
1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

Page 1: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

1

CSCE 441: Computer Graphics Hidden Surface Removal (Cont.)

Jinxiang Chai

Page 2: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

2

Outline

Backface Culling Painter’s algorithm BSP Z-buffer Scan line Ray casting Reading: section 9-1 to 9-11, 9-14,9-15

Page 3: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

3

Review: Backface Culling

view direction

n

v

0vn , draw polygon

Page 4: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

4

Review: Painter’s Example

z = 0.7z = 0.3

z = 0.1

Sort by depth:Green rectRed circle

Blue triz = 0

Page 5: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

5

Review: Painter’s Algorithm

1. Sort all objects’ zmin and zmax

2. If an object is uninterrupted (its zmin and zmax are adjacent in the sorted list), it is fine

3. If 2 objects DO overlap

3.1 Check if they overlap in x

- If not, they are fine

3.2 Check if they overlap in y

- If not, they are fine

- If yes, need to split one

Page 6: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

6

5-2

2

7-2

Review: Building a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

b

b

bb

b f

f

f

Page 7: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

7

Rendering with a BSP Tree

How to traverse the tree Draw “back” polygons Draw “on” polygons Draw “front” polygons

1

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

Page 8: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

8

5-2

2

7-2

1

Different View Points?

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

b

b

bb

b f

f

fDo we need to build a new tree?

V0

Page 9: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

9

5-2

2

7-2

1

Different View Points?

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

b

b

bb

b f

f

fDo we need to build a new tree? - No, we use the same tree if objects are static

Page 10: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

10

5-2

2

7-2

1

Different View Points?

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

b

b

bb

b f

f

fDo we need to build a new tree? - No, we use the same tree if objects are static

How can we traverse the tree?

V0

Page 11: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

11

Rendering with a BSP Tree

If eye is in front of plane Draw “back” polygons Draw “on” polygons Draw “front” polygons

If eye is behind plane Draw “front” polygons Draw “on” polygons Draw “back” polygons

Else eye is on plane Draw “front” polygons Draw “back” polygons

1

2

3

4 5

6

7

+

-

6,7

5bf

1,2,3,4

V0 Vnew

Page 12: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

12

Rendering with a BSP Tree

If eye is in front of plane Draw “back” polygons Draw “on” polygons Draw “front” polygons

If eye is behind plane Draw “front” polygons Draw “on” polygons Draw “back” polygons

Else eye is on plane Draw “front” polygons Draw “back” polygons

1

2

3

4 5

6

7

+

-

6,7

5bf

1,2,3,4

V0

Vnew

Page 13: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

13

Rendering with a BSP Tree

If eye is in front of plane Draw “back” polygons Draw “on” polygons Draw “front” polygons

If eye is behind plane Draw “front” polygons Draw “on” polygons Draw “back” polygons

Else eye is on plane Draw “front” polygons Draw “back” polygons

1

2

3

4 5

6

7

+

-

6,7

5bf

1,2,3,4

Vnew

V0

Page 14: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

14

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+-V0

Vnew

1

Page 15: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

15

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

V0

Vnew

1

Page 16: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

16

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+

-

V0

Vnew

1

Page 17: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

17

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+ -V0

Vnew

1

Page 18: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

18

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+ -V0

Vnew

1

Page 19: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

19

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2->(7-1)

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+ -V0

Vnew

1

Page 20: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

20

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2->(7-1)->4

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+

-

V0

Vnew

1

Page 21: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

21

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2->(7-1)->4->(5-1)

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

+ -

V0

Vnew

1

Page 22: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

22

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2->(7-1)->4->(5-1)->3

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

V0

Vnew

1

Page 23: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

23

1

Different View Points?

1 2

3

4

6

7-2

7-1

5-2

5-1

Traversal order:1->2->(7-1)->4->(5-1)->3->(7-2)->(5-2)->6

7-1bf

3f

4f

5-2

2

7-2

5-1

6

b

b

b

b

V0

Vnew

1

Page 24: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

24

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Page 25: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

25

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

+-

Traversal order?1

Page 26: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

26

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

+-

Traversal order?1->?

Page 27: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

27

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Traversal order?1->?

+

-

Page 28: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

28

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Traversal order?1->?

+ -

Page 29: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

29

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Traversal order?1->?

+

-

Page 30: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

30

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Traversal order?1->6

+

-

Page 31: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

31

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?

b

b

b f

f

f

b

b

Traversal order?1->6->?

Page 32: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

32

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?1->6->5-2

b

b

b f

f

f

b

b

Page 33: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

33

5-2

2

7-2

Rendering with a BSP Tree

1 2

3

4

6

5-1

37-1

4 6

1

7-2

7-1

5-2

5-1

Traversal order?1->6->(5-2)->(7-2)->3->4->(5-1)->(7-1)->2

b

b

b f

f

f

b

b

Page 34: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

34

Summary: BSP Trees

Pros:Simple, elegant schemeNo depth comparisons neededPolygons split and ordered automaticallyWorks for moving camerasOnly writes to framebuffer (i.e., painters

algorithm)

Page 35: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

35

Summary: BSP Trees

Cons:Computationally intense preprocess stage

restricts algorithm to static scenesSplitting increases polygon count Redraws same pixel many timesChoosing splitting plane not an exact

scienceNot suitable for moving objects

Page 36: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

36

Outline

Backface Culling Painter’s algorithm BSP Z-buffer Scan line Ray casting Reading: section 9-1 to 9-11, 9-14,9-15

Page 37: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

37

Depth (“Z”) Buffer

Simple modification to scan-conversion

Maintain a separate buffer storing the closest “z” value for each pixel—depth buffer

Only draw pixel if depth value is closer than stored “z” value Update buffer with closest depth value

Page 38: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

38

Z-Buffering Example

z = 0.7z = 0.3

z = 0.1

z = 1.0

z = 1.0

z = 0.3

z = 1.0

z = 0.3

z = 0.1 z = 1.0

z = 0.7z = 0.3

z = 0.1

NOTE: Can draw these shapes in any order

NOTE: Can draw these shapes in any order

Page 39: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

39

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

),( 11 kk yx

),( kk yx

kk

kk

xx

yym

1

1

mxx kk

11

),( 00 yx

m

kxxk 0

),( endend yx

Page 40: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

40

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

),( 11 kk yx

),( kk yx

kk

kk

xx

yym

1

1

mxx kk

11

),( 00 yx

m

kxxk 0

),( endend yx Plane equation:

Ax+By+Cz+D=0

Page 41: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

41

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

0

0111

DCzByAx

DCzByAx

kkk

kkk

),,( 111 kkk zyx

),,( kkk zyx

kk

kk

xx

yym

1

1

mxx kk

11

),,( 000 zyx

m

kxxk 0

),( endend yx Plane equation:

Ax+By+Cz+D=0

Page 42: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

42

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

0

0111

DCzByAx

DCzByAx

kkk

kkk

kk

kk

xx

yym

1

1

mxx kk

11

m

kxxk 0

),( endend yx Plane equation:

Ax+By+Cz+D=0

0)()()( 111 kkkkkk zzCyyBxxA

),,( 111 kkk zyx

),,( kkk zyx

),,( 000 zyx

Page 43: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

43

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

0

0111

DCzByAx

DCzByAx

kkk

kkk

kk

kk

xx

yym

1

1

mxx kk

11

m

kxxk 0

),( endend yx Plane equation:

Ax+By+Cz+D=0

0)()()( 111 kkkkkk zzCyyBxxA

0)(11

1 kk zzCBm

A

),,( 111 kkk zyx

),,( kkk zyx

),,( 000 zyx

Page 44: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

44

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

0

0111

DCzByAx

DCzByAx

kkk

kkk

kk

kk

xx

yym

1

1

mxx kk

11

m

kxxk 0

),( endend yx Plane equation:

Ax+By+Cz+D=0

0)()()( 111 kkkkkk zzCyyBxxA

0)(11

1 kk zzCBm

A

C

BmAzz kk

/1

),,( 111 kkk zyx

),,( kkk zyx

),,( 000 zyx

Page 45: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

45

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

),,( kkk zyx

Plane equation:

Ax+By+Cz+D=0

What’s the x,y,z value for this pixel?

Page 46: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

46

How to Calculate “z”?

Update “z” values using scan line conversion algorithm

),,( kkk zyx

Plane equation:

Ax+By+Cz+D=0

What’s the xk+1,yk+1,zk+1 values for the next horizontal pixel?

CAzz

yy

xx

kk

kk

kk

1

1

1 1

Page 47: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

47

Depth (“Z”) Buffer

Advantages Always works. The nearest object always determines the

color of a pixel Polygon drawn in any order Commonly in hardware

Disadvantages Needs a whole extra buffer Requires extra storage space (How big?) Still lots of overdraw

Page 48: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

48

Depth (“Z”) Buffer

Advantages Always works. The nearest object always determines the

color of a pixel Polygon drawn in any order Commonly in hardware

Disadvantages Needs a whole extra buffer Requires extra storage space (How big?) (1k*1k*24bits) Still lots of overdraw

Page 49: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

49

Scan Line Algorithm

Scan line algorithm for polygon drawing

- how can we modify it for hidden surface removals?

Scan line

Page 50: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

50

Scan Line Algorithm

Assume for each line of screen, we have scan-lines for all polygons intersecting that line

For each polygon, keep track of extents of scan line

Whenever the x-extents of two scan lines overlap, determine ordering of two polygons

Page 51: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

51

Scan Line Algorithm

Scan line algorithm with depth information

z=0.1

z=0.3

z=0.2

z=0.5 z=0.5

z=0.5z=0.5

z=0.4

Scan line

Page 52: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

52

Scan Line Algorithm

Scan line algorithm with depth information

z=0.1

z=0.3

z=0.2

z=0.5 z=0.5

z=0.5z=0.5

z=0.4

Z=0.18 Z=0.38

Z=0.5 Z=0.5

Scan line

Scan line spans

Page 53: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

53

Scan Line Algorithm

How can we determine overlapping regions? - for each scan line, label the pixel “inside” or “outside”

Page 54: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

54

Scan Line Algorithm

How can we determine overlapping regions? - for each scan line, label the pixel “inside” or “outside”

- Multiple “inside” are overlapping regions.

No overlapping regions!

Page 55: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

55

Scan Line Algorithm

How can we determine overlapping regions? - for each scan line, label the pixel “inside” or “outside”

- Multiple “inside” are overlapping regions.

One overlapping region!

Page 56: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

56

Scan Line Algorithm

How can we determine overlapping regions? - for each scan line, label the pixel “inside” or “outside”

- Multiple “inside” are overlapping regions.

One overlapping region!

Calculate and compare depth values in overlapping regions

Page 57: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

57

Scan Line Algorithm

Page 58: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

58

Scan Line Algorithm

Page 59: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

59

Scan Line Algorithm

Calculate and compare depth values in overlapping regions

Page 60: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

60

Scan Line Algorithm

Advantages Takes advantage of coherence resulting in fast algorithm Does not require as much storage as depth buffer Only draw visible pixels Commonly in software

Disadvantages More complex algorithm Requires all polygons sent to renderer before drawing

Page 61: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

61

Ray Casting

For each pixel enter Pij - Send a ray from eye point, c, through pij into scene

- Intersect ray with each object

- Select the nearest intersection

Page 62: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

62

Ray Casting

Implementation - Might parameterize each ray as

- Each object Ok returns tk>0 such that first intersection with ok occurs at r(tk)

)()( cptctr ij

Page 63: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

63

Ray Casting

Implementation - Might parameterize each ray as

- Each object Ok returns tk>0 such that first intersection with ok occurs at r(tk)

- Q: given the set {tk}, what is the first intersection point?

)()( cptctr ij

Page 64: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

64

Ray Casting

Implementation - Might parameterize each ray as

- Each object Ok returns tk>0 such that first intersection with ok occurs at r(tk)

- Q: given the set {tk}, what is the first intersection point?

)()( cptctr ij

Page 65: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

65

Ray Casting

Ray casting properties:

- process pixels one at a time

- draw each visible pixel once

- efficient algorithm needed for ray-object intersection

- may (not) use pixel coherence

- simple but generally not used

Page 66: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

66

Hidden Surface removal: opengl

Backface culling - glEnable(GL_CULL_FACE), glDisable(GL_CULL_FACE)

- glCullFace(GL_BACK)

view direction

n

v

0vn , cull polygon

Page 67: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

67

Z-buffer: opengl

In opengl, depth values are normalized to [0.0,1.0]

- Specify depth-buffer operations

glutInitDisplayMode // with argument GLUT_DEPTH

- Specify initial depth-buffer value glClear(GL_DEPTH_BUFFER_BIT) // initialize depth-buffer values to 1.0

glClearDepth (depth) // specify an initial depth-buffer value

- Activate depth-testing operations glEnable (GL_DEPTH_TEST)

Page 68: 1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.

68

Summary

Backface Culling Painter’s algorithm BSP Z-buffer Scan line Ray casting Reading: section 9-1 to 9-11,9-14,9-15