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.

1 comment: