7. A MINIMAL BSP COMPILER
 
In this section we are going to show you how a minimal BSP compiler can be built using BspLib. The resulting "program" will read a file called test.wrl, compile the contained scene, and write the resulting output to test.bsp. 

First, let's state that we want to use BspLib's classes without prefixing them with BspLib:: every time: 

using namespace BspLib; 

As first real action, we have to create an (initially empty) object of class BspObjectList, which will contain our entire input scene: 

BspObjectList testscene; 

Next, we have to create an object of class InputData3D, which reads and parses our input file, and builds a scene representation in memory (that is, it fills our testscene object), all during its construction: 

InputData3D inputdata( testscene, "test.wrl" ); 

Now, if everything went alright (if not, BspLib will already have exited with an appropriate error message; to avoid this, error response must be overridden/configured) the entire scene specified within the VRML file is in memory, contained in BspLib's 3-D data structures. Given this, we are going to perform some functions to get ready for BSP compilation: 

testscene.ProcessObjects( BspObjectList::CALC_PLANE_NORMALS ); 
testscene.ProcessObjects( BspObjectList::CHECK_PLANES ); 

This is necessary to ensure plane information (plane normal and distance to origin) is available for every polygon and all polygons are indeed planar. (These two function calls could be combined into one by OR'ing the two flags together, avoiding the overhead of scanning the BspObjectList twice.) 

Next, we will compile an object BSP tree for all objects contained in the scene (see Object BSP trees and class ObjectBSPTree): 

ObjectBSPTree objbsptree; 
testscene.PrepareObjectBSPTree( objbsptree ); 

Now, we get to something really interesting: BSP compilation! A polygon BSP tree will be compiled for each object (see Polygon BSP trees): 

testscene.ProcessObjects( BspObjectList::BUILD_BSP_WITH_CHECKS ); 

At this stage, the object BSP tree is valid and contains polygon BSP trees for each of the objects. We, however, want a unified BSP tree in order to be able to write a single object to the output file: 

testscene.MergeObjectBSPTree( objbsptree ); 

Well, we are nearly finished! Let's quickly write our (only) object to the output file using an object of class OutputData3D: 

OutputData3D outputdata( inputdata, IOData3D::BSP_FORMAT_1_1 ); 

That's it! The output file will be written when our outputdata object goes out of scope (and therefore its destructor is invoked). 

Note that, since we have used BspLib's default options we didn't have to configure them. Note also that all that object BSP tree stuff is only necessary since a VRML file may contain multiple objects. If we would have read an AOD file, or a VRML file containing only one object, we could have shortened this process even more!

 

back to main page.
BspLib documentation copyright (c) by Markus Hadwiger, 1998. ALL RIGHTS RESERVED.