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 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
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
Culling Example Culling Example Power plant model Power plant model (12 million triangles) 4
Culling Example Culling Example Full model Full model View frustum culling View frustum culling Occulsion culling Occulsion culling 12 Mtris 10 Mtris 1 Mtris 5
Lines and Planes Lines and Planes ● I mplicit equation for line (plane): I li it ti f li ( l ) ˆ n n x n y d 0 x y x d [n n d] y 0 l p 0 x x y y 1 (0,0) ( , ) ● I f is normalized then d gives the distance of the n n line (plane) from the origin along 6
Lines and Planes Lines and Planes ● Lines (planes) partition 2D (3D) Li ( l ) titi 2D (3D) ˆ n space: ● Positive and negative half spaces ● Positive and negative half-spaces l l p 0 0 ● The intersection of negative half- d spaces defines a convex region spaces defines a convex region l p 0 (0,0) ( , ) 7
Testing Objects for Containment Testing Objects for Containment Outside Straddling Inside 8
Conservative Testing Conservative Testing r r c c c r c c Indeterminate Inside Outside 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 g 9
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 A BVH A BVH 10
Hierarchical Culling Hierarchical Culling ● Simple algorithm: ● Simple algorithm: while( node is indeterminate ) recurse on children Indeterminate not visited not visited visited Inside Indeterminate Inside Inside Indeterminate Outside 11
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? M 1 -1 1 -1 l [1 0 [ 1 ] ] ● Other planes can be computed similarly 12
Back Face Culling Back-Face Culling ● Special case of occlusion - convex self- Special case of occlusion convex self occlusion ● For closed objects (has well-defined inside and ● 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 13
Face Plane Test Face Plane Test ● Compute the plane for the face: Compute the plane for the face: v v 2 v n (v v ) (v v ) 1 1 0 2 0 d d n v n v 0 v 0 ● Cull if eye point in the negative half-space 14
Back-Face Culling in OpenGL Back Face Culling in OpenGL ● Can cull front faces or back faces C ll f t f b k f ● Back-face culling can sometimes double performance performance if (cull): glFrontFace(GL_CCW) # define winding order glEnable(GL_CULL_FACE) lE bl (GL CULL FACE) # # enable Culling bl C lli glCullFace(GL_BACK) # which faces to cull else: glDisable(GL CULL FACE) glDisable(GL_CULL_FACE) You can also do front-face culling! g 15
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 p ● I nterpolate to get new point 1 p p t(p p ) l p 0 0 1 0 p l (p l (p t(p t(p p )) p )) 0 0 p 0 0 1 1 0 0 ( l p ) 0 t p l (p l (p p ) p ) 0 l l 1 0 ● Vertex attributes interpolated the same way 16
Clipping a Polygon against a Line Line ● Traverse edges ● Keep edges that are K d th t 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
Clipping against a Convex Region Region ● Sutherland Hodgman ● Sutherland-Hodgman ● Just clip against one edge at a time a time 18
Outcodes Outcodes ● The Cohen-Sutherland The Cohen Sutherland clipping algorithm uses outcodes to quickly outcodes to quickly determine the visibility of a primitive ● An outcode is created for each vertex ● I t is a bit vector with a bit I t i bit t ith bit set for each plane the vertex is outside of is outside of ● Works for any convex region g 19
Outcode for Lines Outcode for Lines (outcode1 OR outcode2) == 0 ( t d 1 OR t d 2) 0 line segment is inside ( (outcode1 AND outcode2) != 0 t d 1 AND t d 2) ! 0 line segment is totally outside (outcode1 AND outcode2) == 0 ( t d 1 t d 2) 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 crossing the clip region actually don t 20
Outcodes for Triangles Outcodes for Triangles Combine outcodes from vertices Co b e outcodes o 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
Clipping in the Pipeline Clipping in the Pipeline Clip space Clip space 22
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 project then view frustum view frustum draw gives you draw gives you this near plane p we don’t want to see this part clipped point z 0 eye 23
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 x w w l l [1 [1 1 0] 1 0] w x w x x v l 0 T x v [x w 1 ] i i i w 1 1 w x 0 0 t v (w x ) (w x ) 1 0 0 1 1 x -1 1 24 ● Easy in/ out test and interpolation
Culling and Clipping in the Rendering Pipeline Rendering Pipeline View frustum culling View frustum clipping and pp g back-face culling can be done here Back-face culling done in setup phase Back-face culling done in setup phase of rasterization 25
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
Reading Assignment Reading Assignment ● Read the chapter “Raster Algorithms” Read the chapter “Raster Algorithms” 27
Next Time Next Time ● Triangulating a polygon ● Triangulating a polygon ● Rasterizing triangles ● I nterpolating parameters I t l ti t 28
Recommend
More recommend