Clipping and Culling Sung-Eui Yoon ( ) ( ) C Course URL: URL - - PowerPoint PPT Presentation

clipping and culling
SMART_READER_LITE
LIVE PREVIEW

Clipping and Culling Sung-Eui Yoon ( ) ( ) C Course URL: URL - - PowerPoint PPT Presentation

CS380: Computer Graphics p p Clipping and Culling Sung-Eui Yoon ( ) ( ) C Course URL: URL http://sglab.kaist.ac.kr/~sungeui/CG/ Class Objectives Class Objectives Understand clipping and culling Understand clipping


slide-1
SLIDE 1

CS380: Computer Graphics p p

Clipping and Culling

Sung-Eui Yoon (윤성의) (윤성의)

C URL Course URL: http://sglab.kaist.ac.kr/~sungeui/CG/

slide-2
SLIDE 2

Class Objectives Class Objectives

Understand clipping and culling

  • Understand clipping and culling
  • Understand view-frustum, back-face

culling and hierarchical culling methods culling, and hierarchical culling methods

  • Know various possibilities to perform

culling and clipping in the rendering culling and clipping in the rendering pipeline

2

slide-3
SLIDE 3

Culling and Clipping Culling and Clipping

Culling

  • Culling
  • Throws away entire objects and primitives that

cannot possibly be visible cannot possibly be visible

  • An important rendering optimization (esp. for

large models) g )

  • Clipping
  • “Clips off” the visible portion of a primitive

p p p

  • Simplifies rasterization
  • Also, used to create “cut-away” views of a

model

3

slide-4
SLIDE 4

Culling Example Culling Example

Power plant model

4

Power plant model (12 million triangles)

slide-5
SLIDE 5

Culling Example Culling Example

Full model View frustum culling Occulsion culling Full model 12 Mtris View frustum culling 10 Mtris Occulsion culling 1 Mtris

5

slide-6
SLIDE 6

Lines and Planes

I li it ti f li ( l )

Lines and Planes

  • I mplicit equation for line (plane):

ˆ n

x y

n x n y d    d

x y

x [n n d] y l p             (0,0)

x y

1      

  • I f is normalized then d gives the distance of the

line (plane) from the origin along ( , ) n  n 

6

slide-7
SLIDE 7

Lines and Planes

Li ( l ) titi 2D (3D)

Lines and Planes

  • Lines (planes) partition 2D (3D)

space:

  • Positive and negative half-spaces

ˆ n l

  • Positive and negative half spaces
  • The intersection of negative half-

spaces defines a convex region d l p    spaces defines a convex region (0,0) l p    ( , )

7

slide-8
SLIDE 8

Testing Objects for Containment Testing Objects for Containment

                 Outside Straddling Inside

8

slide-9
SLIDE 9

Conservative Testing Conservative Testing

r

c 

r

c  c

r

c 

Outside Indeterminate Inside

c

Ou s de

l c r    r l c r      l c r    

  • Use cheap conservative bounds for trivial cases
  • Use cheap, conservative bounds for trivial cases
  • Can use more accurate, more expensive tests for

ambiguous cases if needed

9

g

slide-10
SLIDE 10

Hierarchical Culling Hierarchical Culling

  • Bounding volume hierarchies accelerate culling by
  • Bounding volume hierarchies accelerate culling by

rejecting/ accepting entire sub-trees at a time

  • Bounding volume hierarchies (BVHs)
  • Object partitioning hierarchies
  • Object partitioning hierarchies
  • Uses axis-aligned bounding boxes

10

A BVH A BVH

slide-11
SLIDE 11

Hierarchical Culling Hierarchical Culling

  • Simple algorithm:
  • Simple algorithm:

while( node is indeterminate ) recurse on children

not visited Indeterminate not visited visited Inside Indeterminate Indeterminate Outside Inside Inside

11

slide-12
SLIDE 12

View Frustum Culling View Frustum Culling

Test objects against planes defining view

  • Test objects against planes defining view

frustum

  • How do you compute them?
  • How do you compute them?

1 M

  • 1

1

  • 1

l [1 0 1 ]   [ ]

12

  • Other planes can be computed similarly
slide-13
SLIDE 13

Back Face Culling Back-Face Culling

Special case of occlusion convex self

  • Special case of occlusion - convex self-
  • cclusion
  • For closed objects (has well-defined inside and
  • For closed objects (has well-defined inside and
  • utside) some parts of the surface must be

blocked by other parts of the surface

  • Specifically, the backside of the object is

not visible

13

slide-14
SLIDE 14

Face Plane Test Face Plane Test

Compute the plane for the face: v

  • Compute the plane for the face:

1 2

n (v v ) (v v )         

1

v 

2

v d n v   v  d n v  

  • Cull if eye point in the negative half-space

14

slide-15
SLIDE 15

Back Face Culling in OpenGL Back-Face Culling in OpenGL

C ll f t f b k f

  • Can cull front faces or back faces
  • Back-face culling can sometimes double

performance

if (cull): glFrontFace(GL_CCW) # define winding order lE bl (GL CULL FACE) # bl C lli

performance

glEnable(GL_CULL_FACE) # enable Culling glCullFace(GL_BACK) # which faces to cull else: glDisable(GL CULL FACE) glDisable(GL_CULL_FACE)

You can also do front-face culling!

15

g

slide-16
SLIDE 16

Clipping a Line Segment against a Line against a Line

First check endpoints against the plane

  • First check endpoints against the plane
  • I f they are on the same side, no clipping is

needed needed

  • I nterpolate to get new point

1

p  p 

1

p p t(p p )         l p    

1

l (p t(p p ))        p  p l

1

l (p t(p p )) ( l p ) t l (p p )         

  • Vertex attributes interpolated the same

l

1

l (p p ) 

16

way

slide-17
SLIDE 17

Clipping a Polygon against a Line Line

  • Traverse edges

K d th t

  • Keep edges that are

entirely inside Create new point when

  • Create new point when

we exit

  • Throw away edges
  • Throw away edges

entirely outside

  • Create new point and
  • Create new point and

new edge when we enter

17

slide-18
SLIDE 18

Clipping against a Convex Region Region

  • Sutherland Hodgman
  • Sutherland-Hodgman
  • Just clip against one edge at

a time a time

18

slide-19
SLIDE 19

Outcodes Outcodes

The Cohen Sutherland

  • The Cohen-Sutherland

clipping algorithm uses

  • utcodes to quickly
  • utcodes to quickly

determine the visibility of a primitive

  • An outcode is created for

each vertex

I t i bit t ith bit

  • I t is a bit vector with a bit

set for each plane the vertex is outside of is outside of

  • Works for any convex

region

19

g

slide-20
SLIDE 20

Outcode for Lines Outcode for Lines

( t d 1 OR t d 2) (outcode1 OR outcode2) == 0

line segment is inside

( t d 1 AND t d 2) ! (outcode1 AND outcode2) != 0

line segment is totally outside

( t d 1 t d 2) (outcode1 AND outcode2) == 0

line segment potentially crosses clip region at planes indicated by set bits in at planes indicated by set bits in

(outcode1 XOR outcode2)

  • False positive
  • False positive
  • Some line segments that are classified as potentially

crossing the clip region actually don’t

20

crossing the clip region actually don t

slide-21
SLIDE 21

Outcodes for Triangles Outcodes for Triangles

Combine outcodes from vertices Co b e outcodes

  • e t ces

(outcode1 OR outcode2 OR outcode3) == 0

triangle is inside

(outcode1 AND outcode2 AND outcode3) != 0

triangle is outside triangle is outside

(outcode1 AND outcode2 AND outcode3) == 0

triangle potentially crosses clip region

21

slide-22
SLIDE 22

Clipping in the Pipeline Clipping in the Pipeline

Clip space

22

Clip space

slide-23
SLIDE 23

View Frustum Clipping View Frustum Clipping

  • Points in projective space need to be clipped
  • Points in projective space need to be clipped

before projection

  • Primitives that straddle the z= 0 plane “flip”

Primitives that straddle the z 0 plane flip around infinity when projected view frustum

project then draw gives you

near plane view frustum

draw gives you this

p

we don’t want to see this part

eye z 

clipped point

23

slide-24
SLIDE 24

Clipping in the Clip Space Clipping in the Clip Space

  • NDC simplify view frustum clipping
  • NDC simplify view frustum clipping
  • Clip after applying projection matrix, but before

the divide by w y

  • clip coordinates

w w x w    l [1 1 0] 1 w x  w x  

x

l 

x i i i

l [1 1 0] v [x w 1 ]

 

  

T

v  w 1 

1 1

w x t (w x ) (w x )     

1

v 

  • 1

1 x

24 ● Easy in/ out test and interpolation

slide-25
SLIDE 25

Culling and Clipping in the Rendering Pipeline Rendering Pipeline

View frustum culling View frustum clipping and Back-face culling done in setup phase pp g back-face culling can be done here Back-face culling done in setup phase

  • f rasterization

25

slide-26
SLIDE 26

Class Objectives were: Class Objectives were:

Understand clipping and culling

  • Understand clipping and culling
  • Understand view-frustum, back-face

culling and hierarchical culling methods culling, and hierarchical culling methods

  • Know various possibilities to perform

culling and clipping in the rendering culling and clipping in the rendering pipeline

26

slide-27
SLIDE 27

Reading Assignment Reading Assignment

Read the chapter “Raster Algorithms”

  • Read the chapter “Raster Algorithms”

27

slide-28
SLIDE 28

Next Time Next Time

  • Triangulating a polygon
  • Triangulating a polygon
  • Rasterizing triangles

I t l ti t

  • I nterpolating parameters

28