31
CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James O’Brien University of California, Berkeley V2007-F-10-1.0

CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

CS-184: Computer Graphics

Lecture #10: Clipping andHidden Surfaces

Prof. James O’BrienUniversity of California, Berkeley

V2007-F-10-1.0

Page 2: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

2

Today

Clipping Clipping to view volume

Clipping arbitrary polygons

Hidden Surface RemovalZ-Buffer

BSP Trees

Others

Page 3: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

3

Clipping

Stuff outside view volume should not be drawn

Too close: obscures view

Page 4: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

4

Clipping

Stuff outside view volume should not be drawn

Too close: obscures view

Too far:Complexity

Z-buffer problems

Too high/low/right/left:Memory errors

Broken algorithms

Complexity

Page 5: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

5

Clipping Line to Line/Plane

a

bn̂

Line segment to be clipped

Line/plane that clips it r

x(t) = a+ t(b!a)

n̂ ·x! n̂ · r= 0

Page 6: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

6

Clipping Line to Line/Plane

a

bn̂

Line segment to be clipped

Line/plane that clips it

x(t) = a+ t(b!a)

n̂ ·x! f = 0

}n̂ · (a+ t(b!a))! f = 0

n̂ ·a+ t(n̂ · (b!a))! f = 0

t =f ! n̂ ·an̂ ·d

Page 7: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

7

Clipping Line to Line/Plane

a

bn̂

t =f ! n̂ ·an̂ ·d

Segment may be on one side

Lines may be parallel

t !" [0 . . .1]

n̂ ·d= 0|n̂ ·d|! ε (Recall comments about numerical issues)

Page 8: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

8

Polygon Clip to Convex Domain

Convex domain defined by collection of planes (or lines or hyper-planes)

Planes have outward pointing normals

Clip against each plane in turn

Check for early/trivial rejection

Page 9: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

9

Polygon Clip to Convex Domain

Page 10: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

10

Polygon Clip to Convex Domain

Inside Outside

s

p

Output p

Inside Outside

sp

Output i

Inside Outside

s

p

No output

Inside Outside

sp

Output i and p

i

i

Page 11: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

11

Polygon Clip to Convex Domain

Sutherland-Hodgman algorithmBasically edge walking

Clipping done often... should be efficientLiang-Barsky parametric space algorithm

See text for clipping in 4D homogenized coordinates

Page 12: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

12

General Polygon Clipping

A B A!B

B!A

A!B

A!B

Page 13: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

13

Weiler AlgorithmDouble edges

General Polygon Clipping

Page 14: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

14

Hidden Surface Removal

True 3D to 2D projection would put every thing overlapping into the view plane.

We need to determine what’s in front and display only that.

Page 15: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

15

Z-Buffers

Add extra depth channel to image

Write Z values when writing pixels

Test Z values before writing Images from Okan Arikan

Page 16: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

16

Z-Buffers

BenefitsEasy to implement

Works for most any geometric primitive

Parallel operation in hardware

LimitationsQuantization and aliasing artifacts

Overfill

Transparency does not work well

Page 17: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

17

Z-Buffers

Transparency requires partial sorting:

Partiallytransparent

Opaque

Opaque 1st

2nd

3rdFront

Good

Partiallytransparent

Opaque

Opaque 2nd

3rd

1st

Not Good

Page 18: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

18

Z-Buffers

Recall depth-value distortions.

It’s a feature... More resolution near viewer Best use of limited precision

Page 19: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

19

A-Buffers

Store sorted list of “fragments” at each pixel

Draw all opaque stuff first then transparent

Stuff behind full opacity gets ignored

Nice for antialiasing...

Page 20: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

20

Scan-line Algorithm

Assume polygons don’t intersect

Each time an edge is crossed determine who’s on top

Page 21: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

21

Painter’s Algorithm

Sort Polygons Front-to-BackDraw in order

Back-to-Front works also, but wasteful

How to sort quickly?

Intersecting polygons?

Cycles?

Page 22: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

22

BSP-Trees

Binary Space Partition TreesSplit space along planes

Allows fast queries of some spatial relations

Simple construction algorithmSelect a plane as sub-tree root

Everything on one side to one child

Everything on the other side to other child

Use random polygon for splitting plane

Page 23: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

23

a,b,c,d,e,f,g

BSP-Trees

a

b

c

d

e

g

f

Page 24: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

24

a

b,c1,d c2,e,f,ga

b

c1

d

e

g

f

c2

BSP-Trees

Page 25: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

25

a

b c2,e,f,g

c1 d

a

b

c1

d

e

g

f

c2

BSP-Trees

Page 26: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

26

a

b c2

c1 d e1 ,f e2 ,g

a

b

c1

d

g

f

c2

e1 e2

BSP-Trees

Page 27: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

27

a

b c2

c1 d e1 e2 ,g

f

a

b

c1

d

g

f

c2

e1 e2

BSP-Trees

Page 28: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

28

a

b c2

c1 d e1 e2

f g

a

b

c1

d

g

f

c2

e1 e2

BSP-Trees

+ -

+ - + -

+ +

+

++

+

Page 29: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

29

BSP-Trees

Visibility TraversalVariation of in-order-traversal

Child one

Sub-tree root

Child two

Select “child one” based on location of viewpoint

Child one on same side of sub-tree root as viewpoint

Page 30: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

30

a

b c2

c1 d e1 e2

f g

a

b

c1

d

g

f

c2

e1 e2

BSP-Trees

c1:b:d:a:f:e1:c2:g:e2

Page 31: CS-184: Computer Graphicsinst.eecs.berkeley.edu/~cs184/fa07/lectures/10-ClippingAndHidden.pdf · CS-184: Computer Graphics Lecture #10: Clipping and Hidden Surfaces Prof. James OÕBrien

31

g:e2:c2:f:e1:a:c1:b:d

a

b c2

c1 d e1 e2

f g

a

b

c1

d

g

f

c2

e1 e2

BSP-Trees