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

Preview:

Citation preview

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

3

Review: Backface Culling

view direction

n

v

0vn , draw polygon

4

Review: Painter’s Example

z = 0.7z = 0.3

z = 0.1

Sort by depth:Green rectRed circle

Blue triz = 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

+

-

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

+ -

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

+

-

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

+

-

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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?

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

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

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

49

Scan Line Algorithm

Scan line algorithm for polygon drawing

- how can we modify it for hidden surface removals?

Scan line

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

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

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

53

Scan Line Algorithm

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

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!

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!

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

57

Scan Line Algorithm

58

Scan Line Algorithm

59

Scan Line Algorithm

Calculate and compare depth values in overlapping regions

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

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

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

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

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

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

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

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)

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

Recommended