Posts Tagged ‘boundingspheres’

Tutorial: Watch Your XNA Bounding Volume

2009/08/30 3 comments

UPDATE: Thank-you for making this the most popular of my posts 🙂

When the number of models in your scene reaches a certain value you will notice a dramatic drop in rendering performance.  This is true regardless whether it is online or offline rendering and the same is true for XNA. The problem is that by default your 3D engine is rendering objects that you can not see – objects that are behind you for example.

Luckily there is something known as a bounding volume.  Now you may have heard of bounding volumes with regards to collision detection but bounding volumes can also be used to determine whether to draw a model.  The bounding volume is sort of like an invisible object that is just big enough to encompass the model it is assigned to. In computer graphics, bounding volumes can be many things including:

  • spheres
  • axis-aligned boxes
  • model-aligned boxes
  • a combination of the above

A lot of work went into bounding volumes in the early days of offline renderers, particularly ray tracers. The idea is that for each model in your scene you enclose it with at least one bounding volume. To really reap the benefits it is recommended to assign a hierarchy of bounding volumes to your model depending upon the complexity – particularly for collision detection.  Then when it is time to render the scene, it is a rather trivial exercise to determine whether to draw the model by testing whether the model’s bounding volume intersects the camera viewing frustum (below). [1][2]

In the figure below we can see that the orange and green boxes will be rendered because their bounding volumes (in this case spheres) are inside the viewing frustum (the trapezium defined between the p1 and p2 near and far clipping planes respectively).  The pentagon and baby blue square will be culled since they are above or behind the eye.


In this example all the bounding volumes are spheres, which while adequate for most of the shapes below is perhaps not quite sufficient for the orange box.  There is excess lateral space which will lead to premature culling and object collision.  This is demonstrated in this dramatic video clip below where you can see buildings popping in and out of existence!  All the buildings in this scene are each enclosed with a bounding sphere.  You will note the buildings prematurely vanishing at the bottom of the window.

In XNA, the default shape for a bounding volume is the bounding sphere, which as I mentioned is quite sufficient so long as your model’s x:y:z ratio is equivalent or that there are no major extremities – such as arms and legs.  A better approach for my building example above is to use a bounding box rather than a bounding sphere. Unlike spheres, boxes are subject to alignment issues, but since my building is already axis-aligned I got away with a bounding box whose alignment matches the word axis.  If say my building was tipping over because an invisible brontosaurus bumped into it, then I would need to use model-aligned bounding-boxes – a touch more expensive computation wise.

So in this example, I chose to use bounding boxes over spheres.  Note my buildings during this run remain in the universe much to the relief of my tenants. Or was it that there were no brontosauruses?

Strangely, XNA has no default support for bounding boxes for your models only spheres.  If you want the bounding box you will need to calculate it yourself by enumerating the model’s meshes.  Luckily, jwatte has published this XNA c# source code for calculating the bounding box volume in the XNA forums.  Thank-you thank-you!

Depending on your model and needs, spheres might be sufficient for you so long as you are modelling the creature from the film Dark Star.  For anything else, give bounding boxes a try!   Bounding boxes go well with brontosauruses too!



[1] Arvo, J., Kirk, D., A Survey of Ray Tracing Acceleration Techniques, An Introduction to Ray Tracing, Academic Press, 1989.

[2] Pharr, M., Humphreys, G., Primitives and Intersection Acceleration, Physically Based Rendering, Elsevier, Morgan Kaufmann Publishers, 2004