Loading and displaying .X files without DirectX
IntroductionI like the .X file format: its organisation and structure suits me. But why would I need to load and display X files without DirectX? My needs were simple:
The code samples use Freeglut and were written using DevCpp 5.0 (4.9.9.1) and minGw 3.4.2. This article is designed to be read while referencing the sample source code. 1. Loading and displaying the Mesh from an X FileBefore getting into any kind of development, we need to define first what a mesh is and how it is stored within the X file. We will then derive and implement a design. 1.1. Design1.1.1. DescriptionsA polygonal mesh is a structured list of points (called vertices) connected together to describe a surface. In our case, the mesh will define the surface of our model. To texture the polygonal mesh, we associate texture coordinates with each vertex to know which part of an associated bitmap to draw on the mesh (e.g.: drawing tiny's jacket on the mesh part modelling the jacket). Each face of a mesh can also be associated with a material. A material describes the colour reflected by an illuminated model. The colour is often defined as a set of Red, Green, Blue and Alpha components. Let's now look into the X File format. The .X File format can be either in plain text or in binary. This is given in the header of the file. We will look into the text format. The binary format will be addressed in the fifth chapter. The X file format is structured into blocks describing elements of the model. You can browse [MSDN] to find descriptions of all the different blocks. A pair of braces delimits each block. An opening brace must always be paired with a closing brace. There is a hierarchical relationship between the blocks since some may contain others. The table below will outline the hierarchies non-exhaustively:
We are interested in the Mesh Block. This block can be either directly accessible or embedded within a frame. We may find many meshes within the same X file. For example, there are two meshes in tiny_4anim.x. The mesh block holds all the necessary information for its display:
Since we want to have a working demo, we need to support the following:
Whew! Though that list is small, there is much work to do before we are able to display a mesh. 1.1.2. Code DesignThe graph below sums up the class hierarchy for the Model object derived from the descriptions above: The Model class has a single method to concatenate meshes. The Mesh class maintains static lists of vertices, texture coordinates and normal vectors. The Material class holds a colour and the Bitmap texture. Wait a minute! How are we going to draw the model? We could use the Model Object and add a Draw method. But doing this will cause some problems down the road: when we get into mesh animation, we will calculate a new set of vertices from the original mesh vertices at each animation frame. If we want to display the same model but with different poses (say like in a mob of monsters), we would have to calculate the model mesh for each monster. Instead we will create an Object3D class which will be used to perform all calculations on a model mesh. This Object3D class will be initiated with a Model class and will contain the methods to draw and calculate the bounding box of the mesh. Why don't we have a Load method within the Model object? There is a simple answer. There are many 3D-model file formats. We would need to implement a load method for each existing file format plus a function to get the correct loading function from the file extension. This would transform our code into some ungainly spaghetti. We will use a loading interface for ease of implementing future loading functionality. From this loading interface, we will derive our X File loading class. 1.2. ImplementationAt last! We can now begin coding. But before diving into the implementation of our design, I will quickly describe the framework into which our code will be embedded. You can find the framework in the file Sample0.zip. 1.2.1. Quick description of the framework (file Sample0.zip)This framework is built on top of Glut. All the screen manipulations are encapsulated within a Screen object. This Screen object is also responsible for Font manipulation and texture loading and registration under OpenGL. The Sample0 example shows how the OpenGL logo texture is loaded and selected before being displayed on the screen. There is also a timer class used to calculate the elapsed time since the last call. This class is based on the Win32 function GetTickCount. This can easily be replaced by the function glutGet(GLUT ELAPSED TIME)). Finally, there are some tracing macros defined in ToolBox\MyTrace.h.:
All code modifications explained in this article will be signalled within the source by the following tags: /*********************************************** NEW-NEW- NEW- NEW- NEW- NEW- NEW*/ … Code modification … /***END***************************************/ All right! Let's first look at the code in the file Sample1.zip. 1.2.2. Parsing the file (file Sample1.zip)The loading interface is defined in framework\Frm_IO.h. This is a template interface with two protected methods to help users to convert text to floating point numbers and to remove all occurrences of a character from a string. The X File loading class is defined in files ToolBox\IOModel_x.h and cpp. This is what happens in pseudo-code:
Check the file Header Grab a reference to the Model Object to fill in Enter the main processing loop:
Read the block name (ProcessBlock)
Else avoid the block (AvoidTemplate) The file Header is checked by comparing the value read from the file with macros defined in the file XfileStructs.h and given by Microsoft in [MSDN]. These macros are important since they can also be used to process binary files. There are two main utility functions:
These utility functions are invaluable to process a text X File since they help us narrow down the blocks we want to process. If a block is contained within another one like in the Frame structure, it will suffice to duplicate the processing loop inside a specialised processing function for the frame structure (see the function ProcessBone(void)). We found a mesh! Hallelujah! Now we have to process it. This is the task of the specialised function ProcessMesh. Here is what happens in pseudo-code:
Read in the name of the mesh. If there is no name, assign a name to that mesh. Read in the number of vertices. Load the list of vertices into the Mesh object. Read in the number of faces Load the list of faces into the Mesh object. Enter the mesh processing loop:
Read the block name (ProcessBlock)
Else avoid the block (AvoidTemplate) Why do we need a mesh name? The X file format either declares the mesh or only references the mesh name within the block that is concerned by it. To be able to trace what happens and check that the mesh is correctly associated, we need a unique mesh name. If there are no names, we need to create a unique name (this is done by the utility function SetUID). Next we process the Mesh block data (see [MSDN] for the description of that data). Then we enter a loop to process all embedded blocks. The Texture Coordinates block is very simple to process: we read in the number of texture coordinates, and then we load in the list of texture coordinates. The block is processed. The Mesh Normal Vectors block isn't any more difficult. We read in the number of vectors, and then we load in the list of vectors. Next we load in the list of vector indices per face: this gives us the vertex normals for each face allowing for correct illumination of the model. Material list blocks are a little trickier. Here is the pseudo-code:
Read in the material index for each face Enter the material list processing loop: While we have not reached the end of the block
Else avoid the block (AvoidTemplate) All that is left is to process each Material description block within the Material list. Here we go:
Read in the emissive power. Read in the specular colour. Read in the emissive colour. Enter the material description processing loop While we have not reached the end of the block
Else avoid the block (AvoidTemplate) And that's it! All the meshes are loaded into the model object. 1.2.3. Concatenating meshes and creating subsetsWe will now look into the files framework\Frm_Mesh.h and cpp. We can't yet display the meshes loaded within the model. First, we want to concatenate the meshes because:
If you look closely at the Mesh block parsing code, you see at the beginning the initialisation of a series of values for the mesh: these values are the sum of the previous meshes indexes (number of vertices, number of faces, … ). These values will be used:
Now let's have a look at the pseudo-code:
Check the new mesh dimensions and resolve all discrepancies. Create all the new mesh arrays. Process each mesh from the model list
Copy each mesh data into the new mesh. Move each mesh material into the new mesh material list. Add to the model mesh list the new concatenated one. When we calculate the new mesh dimensions, we need to take care of differences between mesh descriptions. One mesh may use textures and thus have texture coordinates while another may just be coloured and have no texture coordinates. To solve that problem, we duplicate the vertex array size to initialise the texture coordinates array. If we didn't do that, the face list would be divided between indexed faces with colour information and indexed faces with texture coordinates. Now that we have concatenated our meshes, there is one step left: we need to create subsets. Let me explain: we have created a mesh with multiple materials. We want to divide our array of faces into a list of faces for each material used. The aim is to have only one call to set a material before drawing our mesh subset. The code is very straightforward:
Initialise the subset For each occurrence of that material in the face material list copy the face data to the subset 1.2.4. Displaying the resultAt last, we have parsed our X File, we have concatenated our Meshes and our Model Object is ready for display. Only one part of our design is left for implementation: the Object3D class that will be in charge of all the calculations based on the original mesh. Let's look at the file Sample1.cpp. During the initialization of the Glut demo, we call our specialised loader object to parse the file tiny_4anim.x into our Model instance. If the Model was successfully loaded, we concatenate the meshes. We load up into OpenGL all bitmap textures declared within the Meshes material list. Now we enter the meat of our subject: we initialise an instance of Object3D with our loaded Model. This initialisation keeps a pointer to the Model, gets a pointer to the first mesh of the Model Mesh list and initialises an empty vertex array with the same size as the Model Vertex Array. Then we call the Object3D Update method, which copies the Model Vertex Array into its own array. Last but not least we compute the bounding box coordinates and deduce the centre of the bounding sphere. Let's display our Object3D! First, we calculate our camera position with the centre of the bounding sphere, and then we call the draw method with our Screen object as a parameter. This draw method will parse the mesh material and subset lists. It will set up each material and draw the corresponding subset until there are no more materials to process. In the Idle function, we clear the Object3D vertex array and call back the Update function. That's all there is to it. Whew! We finally made it. Let's increase the complexity. Time for us to look into tiny's skeleton and skin her. |
|