culling and clipping - computer scienceblloyd/comp770/lecture07.pdfculling and clipping computer...

30
2/05/07 1 Culling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd

Upload: vuque

Post on 12-Apr-2018

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 1

Culling and Clipping

Computer GraphicsCOMP 770 (236)Spring 2007

Instructor: Brandon Lloyd

Page 2: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 2

From last time…■ Representing 3D objects with a mesh

■ Loading objects in the OBJ file format

■ Interaction° pan, dolly, trackball

■ Picking and selection

■ Transformation hierarchies

Page 3: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 3

Today’s topics■ Lines and planes

■ Culling ° View frustum culling

° Back-face culling

° Occlusion culling

° Hierarchical culling

■ Clipping

Page 4: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 4

What are culling and clipping?

■Culling° Throws away entire objects and primitives that

cannot possibly be visible

° An important rendering optimization (esp. for large models)

■Clipping° “Clips off” the visible portion of a primitive

° Simplifies rasterization

° Used to create “cut-away” views of a model

Page 5: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 5

Culling example

■ Power plant model° 13 M triangles

° 1.7 M triangles - gutted version show here with no internal pipes

Page 6: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 6

Culling example

Full model1.7 Mtris

View frustum culling1.4 Mtris

Occulsion culling89 Ktris

Page 7: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 7

Lines and planes■ Implicit equation for line (plane):

■ If is normalized then d gives the distance of the line (plane) from the origin along

■ Transforming points and lines with transformation M:points: lines:

d

(0,0)

x y

x y

n x n y d 0

x

[n n d] y 0 l p 0

1

+ − =

⎡ ⎤⎢ ⎥− = ⇒ ⋅ =⎢ ⎥⎢ ⎥⎣ ⎦

p p′ =M 1l l −′ = M

l ⋅ p =

0 ⋅

nn

Page 8: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 8

Lines and planes

d

(0,0)

l p 0⋅ >

l p 0⋅ <

l ⋅ p =

0 ⋅

■ Lines (planes) partition 2D (3D)space:° positive and negative half spaces

■ The union of negative half-spacesdefines a convex region

Page 9: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 9

Testing objects for containment

+

+

+

+

+

−−

+

+

+

Outside Straddling Inside

Page 10: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 10

Conservative testing

Insidel c r⋅ < −

r

c

Indeterminater l c r− < ⋅ <

r

cr

c

Outsidel c r⋅ >

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

cases if needed

Page 11: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 11

View frustum culling■ Test objects against planes defining view frustum

■ How do you compute them?

■ Other planes can be computed similarly

-1 1

1

-1

M

l [1 0 1]= −t

c1 c3l l (m m )′ = = −Mcolumn 1- column 3

Page 12: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 12

Hierachical culling■ Culling needs to be cheap!

■ Bounding volume hierarchies accelerate culling by trivially rejecting/accepting entire sub-trees at a time

■ Simple algorithm:while( node is indeterminate ) recurse on children

not visited

visited

Inside Indeterminate

Indeterminate

Indeterminate OutsideInsideInside

Page 13: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 13

Back-face culling■ Special case of occlusion - convex self-occlusion

° for closed objects (has well-defined inside and outside) some parts of the surface must be blocked by other parts of the surface

■ Specifically, the backside of the object is not visible

Page 14: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 14

Manifold■ Back-face culling can be applied to any orientable two-

manifold.

■ Orientable two-manifolds have the following properties.1. All points on the surface are locally like a plane. No holes, cracks, or self-

intersections.

2. Boundary partitions 3D space into interior and exterior regions

■ In our case, manifolds will be composite objects made of many primitives, generally triangles.

■ Back-face culling eliminates a subset of these primitives. ° Assumes that you are outside of all objects.

Page 15: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 15

Face plane test■ Compute the plane for the face:

■ Cull if eye point in the negative half-space

1 0 2 0n (v v ) (v v )= − × −

0v

1v

2v

0d n v= ⋅

Page 16: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 16

Winding order test

■ Back-faces have a clockwise vertex ordering when viewed from outside.

■ Typically used by graphics hardware during triangle setup

Page 17: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 17

Back-face culling in OpenGL

if (cull):glFrontFace(GL_CCW) # define winding orderglEnable(GL_CULL_FACE) # enable CullingglCullFace(GL_BACK) # which faces to cull

else:glDisable(GL_CULL_FACE)

■ Can cull front faces or back faces

■ Back-face can sometimes double performance

front-face culling

Page 18: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 18

Occlusion culling with occlusion queries■ Render objects visible in previous frame (occlusion

representation)

Page 19: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 19

Occlusion culling with occlusion queries■ Turn off color and depth writes.

■ Render object bounding boxes with occlusion queries.° An occlusion query returns the number of visible pixels

newly visible

Page 20: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 20

Occlusion culling with occlusion queries■ Re-enable color writes

■ Render newly visible objects

Page 21: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 21

Clipping a line segment against a line■ First check endpoints against the plane. If they are

on the same side, no clipping is needed

■ Interpolate to get new point

■ Vertex attributes interpolated the same way

0p

1p

p′

l

0 1 0p p t(p p )′ = + −

l p 0′⋅ =

0 1 0

0

1 0

l (p t(p p )) 0

( l p )t

l (p p )

⋅ + − =

− ⋅=

⋅ −

Page 22: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 22

Clipping a polygon against a line

■ Traverse edges

■ Keep edges that are entirely inside

■ Create new point when we exit

■ Throw away edges entirely outside

■ Create new point and new edge when we enter

Page 23: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 23

Clipping against a convex region

■ Sutherland-Hodgman° Just clip against one edge at

a time

Page 24: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 24

Outcodes■ The Cohen-Sutherland clipping

algorithm uses outcodes to quickly determine the visibility of a primitive.

■ An outcode is created for each vertex. It is a bit vector with a bit set for each plane the vertex is outside of

■ Works for any convex region

Page 25: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 25

Outcode for lines(outcode1 OR outcode2) == 0

line segment is inside

(outcode1 AND outcode2) != 0line segment is totally outside

(outcode1 AND outcode2) == 0line segment potentially crosses clip region

at planes indicated by set bits in

(outcode1 XOR outcode2)

■ Some line segments that are classified as potentially crossing the clip region actually don’t.

Page 26: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 26

Outcodes for trianglesCombine outcodes from vertices

(outcode1 OR outcode2 OR outcode3) == 0triangle is inside

(outcode1 AND outcode2 AND outcode3) != 0triangle is outside

(outcode1 AND outcode2 AND outcode3) == 0triangle potentially crosses clip region

Page 27: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 27

View frustum clipping■ Points in projective space need to be clipped before projection

■ Primitives that straddle the z=0 plane “flip” around infinity when projected

eyez 0=

near plane

view frustumproject then

draw gives youthis

we don’t wantto see this part

clipped point

Page 28: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 28

Homogeneous clipping■ NDC simplify view frustum clipping

■ Clip after applying projection matrix, but before the divide by w° clip coordinates

■ Easy in/out test and interpolation. Other coordinates treated exactly the same way

-1 1x

w

w 1=

w x=w x= −w x w− < <

xl +x

i i i

0 0

0 0 1 1

l [1 1 0]

v [x w 1]

w xt

(w x ) (w x )

+ = −

=−

=− − −

T0v

1v

Add extra coordinateso we can use our lineequation

Page 29: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 29

Culling and clipping in therendering pipeline

Culling done by application

Eye coordinates. View frustum clipping andback-face culling can be done here

View frustum clipping on clip coordinates.Back-face culling done in setup phaseof rasterization.

Page 30: Culling and Clipping - Computer Scienceblloyd/comp770/Lecture07.pdfCulling and Clipping Computer Graphics COMP 770 (236) Spring 2007 Instructor: Brandon Lloyd. ... Lines (planes) partition

2/05/07 30

Next time■ Triangulating a polygon

■ Rasterizing triangles

■ Interpolating parameters