Frustum Culling in OpenGL Ref: MarkMoley.com Fall 2006 revised 1 Culling Techniques Fall 2006 revised 2 Outline Introduction View Frustum Plane Equation Frustum Plane Extraction Frustum/Point/Sphere Inclusion Tests Fall 2006 revised 3 Definition (View Frustum) the volume of space that includes everything visible from a given viewpoint defined by six planes arranged in the shape of a pyramid with the top chopped off If a point is inside this volume then it's in the frustum and it's visible, and vice versa Visible here mean potentially visible. It might be behind another point that obscures it, but it's still in the frustum. … the scope of occlusion culling Fall 2006 revised 4 View Frustum (cont) Fall 2006 revised 5 Definition (Plane) Divide the space into two halves; extends to infinity any given point is (in front of | behind | on) the plane. In R3, a plane is defined by four numbers: A plane equation: Ax + By + Cz +D = 0 Fall 2006 revised 6 Plane Equation : Ax By Cz D 0 Signed distancefrom( x0 , y0 , z0 ) to d Ax0 By0 Cz0 D A2 B 2 C 2 normalvector: A B C T 0 if ( x0 , y0 , z0 ) is on normalside d 0 if ( x0 , y0 , z0 ) is on 0 if ( x , y , z ) is behind normal 0 0 0 Fall 2006 revised 7 Normalized Plane Equation : Ax By Cz D 0 normalvector: A B C , A2 B 2 C 2 1 Signed distancefrom( x0 , y0 , z0 ) to T d Ax0 By0 Cz 0 D Signed distance to origin : D Fall 2006 revised 8 Frustum Culling Idea: If object is not in frustum, do not send it through pipeline (it is not visible) Wrap the object in a bounding volume; test the bounding volume against frustum (in world coordinate system) Sphere, bounding boxes, … Tasks: Frustum plane extraction Inclusion tests for different bounding volumes Fall 2006 revised 9 Frustum Plane Extraction The rendering pipeline: s M P t u s : object coordinate M : model view matrix u : normalizeddevice coordinate t PMs In frustum culling, s is usually a point on BV, in P : projectionmatrix world coordinate Fall 2006 revised 10 Rendering Pipeline (cont) s M Object coordinate: s s x t x c0 t c y 1 t z c2 t w c3 c4 c5 c8 c9 c6 c10 c7 c11 Clip coordinate: tx tw sy u t P sz sw T c : product of PM c12 s x c13 s y c14 s z c15 sw ty tw u tz T tw In OpenGL,clip volume is [-1,1]3 x uy Fall 2006 revised uz T 11 Frustum Plane Extraction (Left) 1 u x 1 tx tw uy t w 0 t w t x t x t w 0 t x c0 t c y 1 t z c2 t w c3 c 0, c4 c8 c5 c9 c6 c10 c7 c11 c12 s x c13 s y c14 s z c15 sw s c3, s 0 c0, c3, s 0 -1 1 ux Left where ci , is t hei th row in C Fall 2006 revised 12 Left Plane (cont) uy The left plane in clip coordinate: 1 ux The corresponding equation in world coordinate: all s x c y c0 c4 c8 c c c 9 C 1 5 c2 c6 c10 c3 c7 c11 c12 c13 c14 c15 z 1 thatsatisfy the equation 0 , c3, s 0 c0 , c3, x y z 1 0 -1 1 Left T c0 c3 x c4 c7 y c8 c11 z c12 c15 0 Fall 2006 revised 13 ux Frustum Plane Extraction (Right) ux 1 uy tx 1 tw tw 0 t x tw tw t x 0 t x c0 t c y 1 t z c2 t w c3 c4 c8 c5 c9 c6 c10 c7 c11 c12 s x c13 s y c14 s z c15 sw c 3, s c0 , s 0 c3, c0 , s 0 -1 1 ux Right where ci , is t hei th row in C Fall 2006 revised 14 Right Plane (cont) uy The right plane in clip coordinate: ux 1 The corresponding equation in world coordinate: all s x c y -1 Right ux 1 z 1 thatsatisfy the equation 3, c0 , s 0 c3, c0 , x y z 1 0 T c3 c0 x c7 c4 y c11 c8 z c15 c12 0 Fall 2006 revised 15 Summary c3 c0 x c7 c4 y c11 c8 x c15 c12 0 [left ] c3 c0 x c7 c4 y c11 c8 x c15 c12 0 [right ] c3 c1 x c7 c5 y c11 c9 x c15 c13 0 [bot t om] c3 c1 x c7 c5 y c11 c9 x c15 c13 0 [t op] c3 c2 x c7 c6 y c11 c10 x c15 c14 0 [near] c3 c2 x c7 c6 y c11 c10 x c15 c14 0 [far] c0 c C 1 c2 c3 c4 c8 c5 c9 c6 c10 c7 c11 Fall 2006 revised c12 c13 [Proj][MV] c14 c15 16 PointInFrustum Test A point in frustumin all 6 halfspaces The valid sides of all halfspaces are > 0 Fall 2006 revised 17 SphereInFrustum Test p p PointInOffsetFrustum Test Minkowski sum of sphere and frustum Fall 2006 revised 18 Minkowski Sum Fall 2006 revised Coordinate dependent! 19 Signed Distance to Left Plane Plane equations are further normalized to facilitate distance computation d ( x0 , y0 , z0 ) frustum[0]x0 frustum[1]y0 frustum[2]z0 frustum[3] d 0 if pointis outside frustum uy d<0 -1 Fall 2006 revised Left 1 ux 20 Remarks Use bounding spheres Can be conservative if the object is slender Use hierarchical bounding volume where appropriate (see next page) BoxInFrustum: complicated and expensive AABB: already so OBB: more so Fall 2006 revised 21 How can bounding volume hierarchies help? View-frustum culling Ray-tracing Collision detection Fall 2006 revised 22 How can bounding volume hierarchies help? View-frustum culling Ray-tracing Collision detection Fall 2006 revised 23 How can bounding volume hierarchies help? View-frustum culling Ray-tracing Collision detection Fall 2006 revised 24 How can bounding volume hierarchies help? View-frustum culling Ray-tracing Collision detection Fall 2006 revised 25 Example (Culling Off: FPS 15.5) Fall 2006 revised 26 Example (Culling On: FPS 31.0) Fall 2006 revised 27 Epilogue Portal rendering World coordinates?! Plane representation in R3 Fall 2006 revised 28 Indoor Scenes Similar to building walkthrough Occlusions in scene are common (culling important) Geometric database can be huge; preprocessing is required to facilitate smooth viewing Fall 2006 revised 29 Preprocessing for Indoor Scenes Potential visible set (PVS) only load the rooms that are visible from the current room Fall 2006 revised 30 1 M B 2 4 3 C 5 K M 8 Ki Portal Rendering B 1 E L 6 R 7 4 2 C 3 R E 7 6 5 L K Fall 2006 revised 8 Ki 31 Portal Rendering only render the other rooms in PVS if the “portal surfaces” are in sight Fall 2006 revised 32 Earlier, we said… s M But aren’t s local coordinates? P t u s : world coordinate u : clip coordinate t PMs Fall 2006 revised 33 Recall Pipeline… Modelview Matrix Model Transform Viewing Transform world coordinates Fall 2006 revised 34 Frustum Culling Scenario the scene is (mostly) static and specified in their world coordinate (the modeling transform is identity) Fall 2006 revised the viewer navigates around them, changing the viewing transform only 35 Therefore … Modelview Matrix Viewing Transform Model Transform I world coordinates s t u t = PMs M : viewing transform s : world coordinate Fall 2006 revised 36 Verification x–z+10 R –x–z+10 L X Z Fall 2006 revised 37 Supplement: Representing a Plane in R3 x yz20 Complet esolut ionof Ax b null space solut ion x yz 0 particularsolution x 2 y 0 x 1 1 y c 1 d 0 , c , d R z 0 z 0 1 completesolution x: pivot variable x 2 1 1 y 0 c 1 d 0 , c, d R z 0 0 1 Fall 2006 revised 38 Plane in R3 (cont) x yz20 completesolution x 2 1 1 y c 1 d 0 , c, d R 0 z 0 0 1 2 1 p 0 , n 1 x p n 0 0 1 Fall 2006 revised n x p 39 Summary Algebraic equation Parametric equation Vector equation Depending on the application, select the most suitable equation In/out test Projection … Fall 2006 revised 40 Useful in GPU-assisted collision detection applications Screen-Space Bounding Box (SSBB) Compute and display the screen-space bounding box of an AABB (axis-aligned bounding box) Convert world coordinates to clip coordinates, perspective-divide to get normalized device coordinate (and then window coordinates) Fall 2006 revised 41 Display Viewing Frustum Useful for projective texturing (showing where the projector is) Useful for illustrating frustum culling Given [-1,1]3 clip coordinates, determine their corresponding world coordinates to render Fall 2006 revised 42