Portfolio
Culling Demo
posted on July 22, 2010This was the third of three mini-projects that comprised the practical part of the Advanced 3D Programming module for the final year, (and purposefully the easiest one of the three, to help spread the load across the year).
It is built upon Frank Lunas DirectX 9 framework. The demo starts by generating a terrain and placing 5000 trees randomly around the scene (Hence the large loading time) it then uses a variety of techniques to attempt to cull the most trees possible, hoping to increase the frame-rate (Not always successfully).
First on the optimising techniques is the classic octree, the terrain and trees are slit into nodes which are culled by the camera before rendering, which usually results in a large boost to frame-rate (which you can see by pressing the ‘T’ key to toggle between using the octree or ‘whole’ where the terrain is not split and the camera culling is not performed)
Next is occlusion culling, which is a bit of a double edged sword. Firstly all the trees bounding boxes are rendered to a texture, then, one by one the trees are rendered again, this time querying how many pixels were actually rendered, trees that do have some pixels rendered in that pass, then have their actual meshes rendered to the back-buffer. Due to the slowness of querying the graphics card to discover the number of pixels drawn for each tree, I actually use the result from a few frames back. The steps are as follows:
- If the tree’s query is finished, start a new one and render the tree
- If not, use the result from the last successful query
This works well as a optimisation when the scene is running at a high frame-rate, however at low frame-rates if you move the camera quickly you can see the ‘popping’ caused by the occlusion results being a few frames behind.
Even with that optimisation the occlusion culling is still slower than not using it, but implementing it was considered more of an academic exercise. (Toggle occlusion culling with the ‘O’ key)
Last up is LODding, when the tree mesh is loaded, I create several versions of the mesh with different numbers of faces and store them in an array, I then calculate the distance from each tree to the player and use this distance squared to choose a mesh so that trees near the player are rendered with the ‘normal’ mesh, and trees further away are rendered with the meshes that have less triangles.
Feel free to Download the executable.
