Friday 21 September 2012

Loading FBX Model and caching data

With the first steps taken we can start working in chunks and get a better understanding of what is going on while we load and use our imported model. These next steps will make a few changes to our Model class and create a new class, ModelCache, which will handle some information such as vertex and normal information. This will fix how we store our normals for the geometry and aid in the render process. The code is based on the View Scene Sample and have removed some elements that aren't needed for now. Here we have added a new method to begin caching some information take we will obtain from the model during the loading process with StorModelInformaiton being the entry point for that. The following methods are used to traverse the loaded scene node and also methods for unloaded the stored data when finished with the model. Our entry point for storing the data is after we set the scene to be imported and before the importer is destroyed. Here we use the StoreModelInformation as the entry point to start the recursion process in the scene. The traversal process is simple, we look at the node, check if it is a mesh type, grab our mesh data from that node and process it. We'll process it by creating a VBOMesh object which we will create later, this will store and handle the rendering of the mesh data for us.
Finally at the end of processing the node, check if it has any child nodes. If it does proceed with the same process with those. When finishing with our model we'll make sure to remove the data that we created in the form of our VBOMesh objects. Calling UnloadCachedData begins the unloading process. Here we have our modified drawing method. You'll notice that we now check to see if we have any objects stored as a VBOMesh object in our node before drawing. This gives us the opportunity to grab that and render the mesh with the stored data instead of how we processed it before, which we have as a fall back. Next we have our VBOMesh object that we use for storing our data. The main methods you'll note from earlier are the Initialise, which will handle storing the passed mesh data. BeginDraw which will set up our vertex buffer object for drawing, EndDraw which will end the meshes drawing process and finally Draw which pass our VBO for drawing. The VBOMesh for now will focus on storing vertex data and normal data. Last time we didn't use the normals that were supplied by the model, whereas this time we will store them like the vertex data and pass them when rendering. The initialise method is quite lengthy and you'll want to ensure that the data your storing is correct, otherwise you'll run into rendering upsets later. End result will be that our vertex buffer objects will be generated with normal data.

Tuesday 11 September 2012

Loading FBX Models and Rendering


Needing a format to work with on a project that was going to require everything from geometry and animation I went looking over the model formats available, ending up sticking with a format that I've used for a long time. Although I've used the FBX in engines like Unity3D and in XNA, I've never really delved into using them with OpenGL or DirectX and a current project that I'm on was in need of it. First port of call was finding out about the FBX SDK supplied by Autodesk. Luckily they have a sample written with OpenGL stepping through the initialization of the FBX SDK manager, importing your model, rendering, and handling animations. The downside to it was that it was quite lengthy, covering all the bases of an FBX import, memory allocation, geometry, lighting, and material caching which meant that it took a bit of pulling to get a simple FBX model loading. I felt that I could help by posting my findings up on loading FBX models starting of simple and then progressing to using animations as in the FBX SDK ViewScene sample.
Download the Autodesk FBX SDK from the Autodesk website. The version that I've used is the latest at the time of writing this at FBX SDK 2013.2. You can look up the help documentation as well for more info on the SDK itself.

If you are new to OpenGL and looking at using the steps I've set up here you may want to look at setting up a basic OpenGL window. The samples that I've made are using SDL, you can find some information on setting it up at SDL Tutorials, a great starting point if you havn't got one yet.

I'll start with the Model class I'm using. All I need is for it to handle the loading and rendering of the FBX information for now.


Loading the model from an FBX file.
The basic steps to loading any FBX file with the FBX SDK is to:

  1. Create the FBX manager for memory management.
  2. Create an FbxIOSettings object which can be configured to import information cush as meshes, lights, cameras or even custom properties.
  3. Initialize the FbxImporter with the newly created FbxIOSettings along with the filename of the file to be loaded
  4. Create or use an FbxScene object to load the Fbx model data to from the importer.
  5. Finish up by destroying the importer since we're finished with it.


The method to recurse over the FBX information and draw the mesh data. Looking at this code we see that we are mainly just concerned with the actual mesh data. The FBX SDK samples have a more robust solution that incorporates various other FbxNodeAttribute types. The main idea here is for the node that is passed to the method to get the global transform of the node, draw a mesh if it has any, and lastly check the for any children of this node which we will then process.


Helper method to get the offset of the actual mesh geometry.


Draw the actual mesh information. This is a quick way to get a mesh up and rendering with the methods from the FBX SDK and can be improved alot by caching information that can be found when we load the actual model. The FBX SDK ViewScene Sample does this quite well, storing a whole host of data, but also leaves it quite large amount of work when in cases like this you just want to get a model imported and rendering for now. Here the method fetches the vertices from the FbxMesh from the passed FbxNode and begins drawing them with OpenGL. If you want to use DirectX 9/10 it should be straight forward here if you use DirectX.

The call to actually render the model.

With the model class set up it's straight forward to create and load the model.

And finally render your model to the screen.
Finally the result I have here is the rendering of the humanoid.fbx model from the SDK the polygon mode enabled to show how the quick normals are working with basic lights in OpenGL.