Click here to load reader

Space Partitioning

  • View
    32

  • Download
    6

Embed Size (px)

DESCRIPTION

Space Partitioning. Computer Graphics. What is Space Partitioning?. A division of 3D space into distinct regions Specifically, a tree data structure is used to divide up the world in a hierarchical manner The tree leaf nodes usually contain pieces (triangles) of the world - PowerPoint PPT Presentation

Text of Space Partitioning

  • Space PartitioningComputer Graphics

  • What is Space Partitioning?A division of 3D space into distinct regionsSpecifically, a tree data structure is used to divide up the world in a hierarchical mannerThe tree leaf nodes usually contain pieces (triangles) of the worldEach leaf holds onto the polygons that are in its sub-piece of the world

  • Why Perform Space Partitioning?SpeedFor example, an accurate model of a Boeing-777 contains over 500M trianglesYou do not want to send 500M tri down the rendering pipelineWe have seen various mapping techniques to make the model look good with less trianglesBut we can also cut down on the number of triangles by only sending triangles down the pipeline that are in the field of view of the cameraBy dividing the world into pieces we can cull/clip on large scale rather than per triangle as the rendering pipeline does

  • Types of Space PartitioningThe two main types we will explore are:OctreesBinary Space Partition (BSP) trees

    However, in the process we will also cover:Frustum cullingBounding Volumes (BVs)Potentially Visible Sets (PVS)Scene Graphs

  • Frustum CullingIn the classic rendering pipeline front-facing triangles are clipped against the FrustumAll 3 vertices outside the volume implies the triangle is culled (not sent down the pipeline)The problem with this technique is that it is triangle-based and doesnt take into account spatial coherenceAll the triangles for a given object lie in about the same position in space

  • Frustum CullingWhy is spatial coherence important?If an object is out of the Frustum then so are all its trianglesIf we could perform the Frustum check per object then we could cull all the objects triangles at the same time with a single testNote that this is a software test, but only 1 is made rather than 10s/100s/1000s of hardware tests

  • Frustum CullingHow do we perform the test in software?First we need to find the planes of the FrustumRecall that there are 6 planes (near, far, left, right, top, bottom)

  • Frustum CullingIf we can find the plane equation for each then we can test to see which side of the plane a point liesRecall the plane eqn is: Ax + By + Cz + D = 0Where (A, B, C) is the normal of the planeIf we have a point (x, y, z) and we plug it into this equation, we get:Positive on the normal side of the plane (in front)Zero on the planeNegative on the non-normal side of the plane (behind)A point must be in front of all planes for it to be in the Frustum

  • Frustum CullingSo, how do you find the Frustum planes?After setting up the Camera, get the modelview matrix and the projection matrixglGetFloatv( GL_MODELVIEW_MATRIX, mod ); glGetFloatv( GL_PROJECTION_MATRIX, proj );Note that you need both because the modelview sets up the camera position and orientation whereas the projection sets up the shape of the FrustumThen multiply the two matrices togetherclip = (mod) x (proj)

  • Frustum CullingThe A, B, C, and D values can then be extracted from the clip matrixAs an example the right plane is:A = clip[ 3] - clip[ 0] B = clip[ 7] - clip[ 4] C = clip[11] - clip[ 8] D = clip[15] - clip[12]These plane values are then normalized so +/- distances from the plane are correctEach component (A-D) is divided by sqrt(A2 + B2 + C2)

  • Frustum CullingThis sounds like a lot of workNot coding, but execution time workBut keep a couple of things in mind:The Frustum plane equations only need to be recalculated whenever the camera movesWe are going to test an entire object rather than a single pointIn particular, we will test sphere and cube objects

  • Frustum CullingWhy just spheres and cubes?They make nice Bounding Volumes (BVs)A Bounding Volume is a simplified shape that encloses an entire complex objectIf the Frustum test against the BV fails then the entire complex object in the BV is not in the FrustumThe advantage is that tests against simple shapes are computational much easier to perform than tests against complex shapes

  • Frustum CullingSphere testingA sphere is defined by a center point and a radiusPart of the sphere is still in front of the plane (and thus, it cant be culled) even if the center point is up to radius units on the back side of the plane Recall that Ax + By + Cz + D is the distance from the point (x, y, z) to the planeThus, we keep the sphere if the sphere center plugged into this equation produces values > -radius for all 6 plane equationsThe point test is the same, but with > 0

  • Frustum CullingCube testA cube consists of 8 verticesUsually pass in as a center location and a sizeWe could test to see if any of the 8 cube vertices are within the Frustum (i.e. test each vertex against all 6 planes)However, this leads to problems when the cube is bigger than the Frustum all 8 corners are outside the Frustum, but the cube contents should be drawnFalse negatives cube culled when it shouldnt be

  • Frustum CullingInstead we reverse the order and test all 8 points against a single planeIf all are to the back side of the plane then the cube is not visible and we can cull itIf any are in front, then we move onto the next planeIf it passes all 6 planes then we keep itHowever, this can lead to false positives (see figure)We let it though and simply let the card cull it on a triangle by triangle basis

  • Frustum CullingSo what sort of speed up do you get?It depends on several factors:How big the Frustum is (especially the far plane)How many object are in the scene and how they are distributedHow complex each particular object isCheck out gametutorials Frustum culling tutorial

  • OctreesOctrees are a space partitioning data structureWe saw Octrees before in the context of modelingIn modeling the leaf nodes were either filled or emptyIn space partitioning the leaf nodes contain a list of triangles that are in that sub-section of spaceThe process:A single cube is placed around the entire worldIf the cube contains too many triangles, then the cube is sub-divided into 8 smaller axis-aligned cubesRecurse until we hit the stopping condition

  • Octrees

  • OctreesWhen do you stop sub-dividing?When the cube contains less than a specified number of trianglesWhen the sub-division depth has reached a maximumWhen the total number of cubes in my octree has reached a maximum number

  • OctreesWhat happens to triangles that cross a cube boundary?Place the object in the largest box that fully contains itNot efficient in that a small object can be placed in a large bounding box if it lies directly on the boundaryImplies that not all triangles are stored in the leaf nodesSplit the triangleIncreases the number of primitives in the scenePlace the triangle in both sub-divisionsAs the triangles from each sub-division are sent to the renderer we need to mark them so we dont draw them twiceMakes Octree editing, such as deletion, is more difficult

  • OctreesSo how to Octrees help us?We can combine Octrees with Frustum cullingThe Octree cubes are Bounding Volumes for the triangles in their region of spaceWe only need to send the triangles contained in the cubes that pass the Frustum test to the rendering pipeline

  • OctreesThis leads to a process called Hierarchical Frustum CullingStarting with the root node of the Octree, test the Octree cube against the FrustumIf not in frustum, then prune childrenIf in frustum, thenSend any contained triangles to rendererRecurse on each of the childrenDemo Octree2 and Octree3 from gametutorials

  • BSP TreesThe idea is much the same as with OctreesHowever, the space is repeatedly split into 2 parts (hence the name binary space partition) by a given splitting planeOctrees always use cubesThere are two variantsAxis-AlignedPolygon Aligned

  • Axis-Aligned BSP TreesStarting with a bounding box around the entire world, split it into 2 parts with an xy, xz, or yz splitting planeDivide all the triangles between the two half-spacesTriangles that intersect the splitting plane can be dealt with the same way we did with OctreesRecursively continue the subdivision process until a stopping criteria is reachedNote that each successive subdivision only splits its own half-space

  • Axis-Aligned BSP TreesThe interesting part is how to decide what axis and where along that axis to splitSuggestions on how?Hint: how does all this relate to what you learned in cs255 about merge sort vs. quick sort

  • Polygon-Aligned BSP TreesIn this version, a polygon is chosen as the divider polygonThis polygon is part of a plane and it is this plane that is used as the splitting plane

  • Polygon-Aligned BSP TreesJust as with Axis-Aligned BSP trees the most important choice is the selection of the polygon dividerThere are two main issues to consider:How balanced is the treeHow many extra triangles did we have to make because of triangle splittingIt is a debatable question as to which is better:A balanced tree with lots of extra trianglesA unbalanced tree with fewer extra trianglesHowever most people pick the unbalanced tree with fewer splits of triangles

  • Polygon-Aligned BSP TreesGenerating an optimal BSP tree requires trying every possible splitting combinationThe problem is that this is a O(n!) algorithmRecall that 20! = 2,432,902,008,176,640,000The next best option is to pick the best splitter at each levelDivide-and-Conquer with a Greedy selection of the splitter at each levelThis is an O(n2) algorithmThis is the option that most games useHowever, it is still pre-computed and saved in a file

  • BSP TreesHierarchical Frustum Culling can be used fairly easily with Axis-Aligned BSP treesVery similar to how it was used for Octrees since each region is rectangularHowever, for Polygon-Aligned BSP trees testing is more complex

Search related