• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Scene graph FBX and stuff..

0 posts in this topic

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0