Jump to content
  • Advertisement
Sign in to follow this  

Scene graph FBX and stuff..

This topic is 2197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys. I've tried to post an article about scene importing with fbx sdk. the article was later discarded by the moderator and I have no time to polish it, so I've decided to post it like a topic in the forums.

The main goal of the article is to introduce the reader to the fbx sdk(wich pretty str.fwd. but there are some pitfalls)


Keep in mind that my English isn't the greatest in the world. Type down mine mistakes and I will try to correct them as soon as I can.


And here it is :


This article will introduce you to the Autodesk® FBX® SDK and how can we use it to import scenes.
Some words about the code:
- The code is not bulletproof, there are mistakes and I will do my best to correct them
- The code is not efficient, you will need to optimize it by yourself
Some words about me:
- I don’t speak fluent English, so please describe mine mistakes I will try to correct them

Let’s GO!
What we will import!?
Well we will import a simple scene exported form Max/Maya/Blender/W.Ever via the FBX SDK.
Autodesk® FBX® SDK (FBXSDK) can import .fbx/3ds/obj/… file formats, so we don’t need to know anything about the structure of those formats, the FBXSDK will handle those things for us. Later we can use the loaded information to render a scene, or (the more preferable) to export the imported (what the hell….) scene to our custom scene fromat.

STEP 1: Setting up the FBXSDK
All object initialization and file operations are done via the FbxManager. Out first step is to initialize that object
FbxManager* m_pSdkManager = FbxManager::Create();
After that we must tell the FbxManager what we want to import. For the sake of simplicity we will import everything
FbxIOSettings * m_pDefaultIOSettings = FbxIOSettings::Create(m_pSdkManager, “randomNameHere”);
m_pSdkManager->SetIOSettings( m_pDefaultIOSettings );//register the settings to the manager

I prefer to put all that functionality in a class or a singleton or whatever.

class MyFbxManager: public TSingleton< MyFbxManager >
public :  
 const FbxManager*   GetManager() const  { return m_pSdkManager; }
 FbxManager*    GetManager()   { return m_pSdkManager; }
private :
 SGEFbxManager()  { Initialize(); }
 void Initialize(); //that function will create the FbxManager and the default IOSettings
 FbxManager*  m_pSdkManager;
 FbxIOSettings* m_pDefaultIOSettings;
STEP 2:Importing a file:
//create the importer object
FbxImporter* pImporter = FbxImporter::Create(pFbxManager, "testImporter");

//load the scene
FbxScene* pScene = FbxScene::Create(pFbxManager, "testScene");
pImporter->Destroy();//destroy the importer object we don’t need it anymore

//That code here is only used for animations later
const int numAnimStacks = pFbxScene->GetSrcObjectCount<fbxanimstack>(FBX_TYPE(FbxAnimStack));
FbxAnimStack* lAnimStack = lAnimStack = FbxCast<fbxanimstack>(FbxScene->GetSrcObject(FBX_TYPE(FbxAnimStack), 0));
const int nbAnimLayers = (lAnimStack) ? lAnimStack->GetMemberCount(FBX_TYPE(FbxAnimLayer)) : 0;
FbxAnimLayer* pAnimLayer = (nbAnimLayers > 0) ? lAnimStack->GetMember(FBX_TYPE(FbxAnimLayer), 0) : nullptr;

A little description: Each FbxScene contains scene nodes. Scene nodes are organized in a hierarchy
(tree). Each imported scene will contain at least one node called “RootNode”. Each node contains an object – called attribute (mesh, camera, curve, bone, dummy, light).

So what we will simply do is we will traverse trough all nodes get and load their attributes and that it!

//create recursive function that will traverse trough nodes
std::function<void (FbxNode*) > NodeTraverse = [&](FbxNode* pFbxNode)
//Load the node name
Const char* pName =    pFbxNode->GetName();
//Load the default node transform (aka World transform at the binding moment)
defaultTranslation =  pFbxNode->LclTranslation.Get();
defaultRotation =  pFbxNode->LclRotation.Get();
defaultScaling =   pFbxNode->LclScaling.Get();
//you may want to load some user defined props. Actually I prefer to describe the node material(aka textures, colors, fog ect) in here and keep the materialObject in the edior clean for the artists)
FbxProperty userProperty = pFbxNode->GetFirstProperty();
const char* pProperyName = userProperty.GetNameAsCStr();
 void* pUserData = userProperty.GetUserDataPtr();
    //Keep in mind that there are some properties that are exported by the scene editor
    //you can use pProperyName to determinate that
 prevUserProperty = userProperty;
 userProperty = pFbxNode->GetNextProperty(prevUserProperty);
//Let's get the node attribute
FbxNodeAttribute* pFbxNodeAttrib = pFbxNode->GetNodeAttribute();
const FbxNodeAttribute::EType fbxAttriuteType = (pFbxNodeAttrib) ? pFbxNodeAttrib->GetAttributeType() : FbxNodeAttribute::eUnknown;
//the painful switch
 //if the current attribute is mesh load the mesh and attach it the node
case FbxNodeAttribute::eMesh :
  //load the mesh later covered
  FbxMesh* pMesh = pFbxNode->GetMesh();//Get the attached mesh object
CreateFromFbxMesh(pMesh);//load the mesh object
 CaseÃÃâÃÃæ //All other cases are similar
//Traverse trough all child nodes
const int numChildNodes = pFbxNode->GetChildCount();
for(int t = 0; t < numChildNodes; ++t)
 FbxNode* pChild = pFbxNode->GetChild(t);
 const char* pChildName = pChild->GetName();
 //add child to the current node
 AddChild( pChildName );
 //traverse all child nodes
 NodeTraverse( pChild );

In the previous section I’ve skipped the animation loading. Here I will provide the code just to keep the code simple:

//the node may have some animation that (note If you prefer you can skip that part if the node attribute is != form Bone)
//Note Bones in FBXSDK are called Clusters
 //get the animation layers
 const FbxAnimCurve* pTranslationLayer = pFbxNode->LclTranslation.GetCurve(pAnimLayer, false);
const FbxAnimCurve* pRotationLayer = pFbxNode->LclRotation.GetCurve(pAnimLayer, false);
 const FbxAnimCurve* pScalingLayer = pFbxNode->LclScaling.GetCurve(pAnimLayer, false);
 //get count for each layer
 const int numTranslationKeys  = (pTranslationLayer) ? pTranslationLayer->KeyGetCount() : 0;
 const int numRotationKeys  = (pRotationLayer) ? pRotationLayer->KeyGetCount() : 0;
const int numScalingKeys  = (pScalingLayer) ? pScalingLayer->KeyGetCount() : 0;

// load all keyframes (NOTE that code really sucks but it is working pretty well)
std::map<long long,FbxTime> allKeyTimes;
 const auto addKeyLambda = [&allKeyTimes](const FbxAnimCurve* pCurve)
  const int numKeys = (pCurve) ? pCurve->KeyGetCount() : 0;
  for(int t = 0; t < numKeys; ++t)
   const FbxTime time = pCurve->KeyGet(t).GetTime();
   const long long timeMS  = time.GetMilliSeconds();//get the keyframe time in miliseconds
   allKeyTimes[timeMS] = time;
 //load the keyframes

//The FbxAnimEvaluator will flatten all transformation (aka InverseKinematics ect) for us
FbxAnimEvaluator* pEvaluator =  m_pFbxScene->GetEvaluator();
std::for_each(std::begin(allKeyTimes), std::end(allKeyTimes), [&allKeyTimes, &newNode, &pEvaluator, &pFbxNode](const std::pair<long long,FbxTime>& time)
 KeyFrame importedKeyFrame;
 //load the node transformation at the specific frame
 importedKeyFrame.translation  = pEvaluator->GetNodeLocalTranslation(pFbxNode, time.second);
 importedKeyFrame.rotation = pEvaluator->GetNodeLocalRotation(pFbxNode, time.second);
 importedKeyFrame.scaling  = pEvaluator->GetNodeLocalScaling(pFbxNode, time.second);
 importedKeyFrame.frame = time.second.GetMilliSeconds();

STEP 3:Loading a Mesh

bool FbxImportedMesh::CreateFromFbxMesh(const FbxMesh* pMesh)
//give it a name :)
m_name = pMesh->GetNode()->GetName();
//get the vertices
const int numControlPoints = pMesh->GetControlPointsCount();
const FbxVector4* arrControlPoints = pMesh->GetControlPoints();
//get the indices(NOTE you can change the vertex topology currently im using triangle lists)
const int numMeshIndices = pMesh->GetPolygonVertexCount();
const int numTriangles = pMesh->GetPolygonCount();
const int* pSrcIndices = pMesh->GetPolygonVertices();
//Load all layered data like Normals, Texcoords ects... they are stored in some sort of layers I will skip that for now
//I will provide only the skinning information, If you want to load morph animation data please refer to the FBXSDK documentation
//get the number of skins
const int numSkinDeformers = pMesh->GetDeformerCount(FbxDeformer::eSkin);
for(int t = 0; t < numSkinDeformers; ++t)
 const FbxSkin* pSkin = (FbxSkin*)(pMesh->GetDeformer(0, FbxDeformer::eSkin));
 const int numClusters = pSkin->GetClusterCount();
 //make the skin info
 MeshSkinInfo skinInfo;
 //loop thought all clusters(BONES) in the current skin
 //bones are simply nodes we already know evrything about their keyframes and transformations
 for(int clusterIdx = 0; clusterIdx < numClusters; ++clusterIdx)
  const FbxCluster* pCluster = pSkin->GetCluster(clusterIdx);
  //obtain the cluster(bone) info
  const char* pClusterName   = pCluster->GetLink()->GetName();
  const int numAffectedVertices   = pCluster->GetControlPointIndicesCount();
  const int* arrControlPointsIndices  = pCluster->GetControlPointIndices();
  const double* arrWeigths   = pCluster->GetControlPointWeights();

 //add the skin info

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!