Search code examples
iosiphoneopengl-esindicesfbx

FBX parse for openGL Display


I want to parse FBX file and display it via openGL on iPhone. currently try to parse *.fbx format using FBX SDK from Autodesk.

Found also this link useful, but still have some misunderstanding - i guess that indices that i receive is incorrect. I create simple cube in blender and export that file in to *.fbx. If I open this file via FBX Review app - all displayed perfect, but when i try to draw this cube - looks like that some data that i parse is incorrect/or partial.

To get Indices i use next code

int numIndices = pMesh->GetPolygonVertexCount();
int *indices = new int [numIndices];
indices = pMesh->GetPolygonVertices();

int polygonCount = pMesh->GetPolygonCount();
int totalObjSize = 0;
for (int i = 0; i < polygonCount; ++i) {
    totalObjSize += pMesh->GetPolygonSize(i);
}

Result:

----Indices----24---: 0 1 2 3 4 7 6 5 0 4 5 1 1 5 6 2 2 6 7 3 4 0 3 7

----Coordinates(Vertises)----72
1 1 -1 
1 -1 -1 
-1 -1 -1 
-1 1 -1  
1 0.999999 1 
-1 1 1 
-1 -1 1 
0.999999 -1 1 
1 1 -1  
1 0.999999 1 
0.999999 -1 1 
1 -1 -1 
1 -1 -1 
0.999999 -1 1 
-1 -1 1 
-1 -1 -1 
-1 -1 -1 
-1 -1 1 
-1 1 1 
-1 1 -1  
 1 0.999999 1 
 1 1 -1 
-1 1 -1 
-1 1 1 

----Texture UV----48
0.499999 9.7692e-05
0.499999 0.333364
0.250049 0.333364
0.250049 9.78112e-05
0.250049 0.666633
0.499999 0.666633
0.499999 0.9999
0.250049 0.9999
0.74995 0.333366
0.74995 0.666633
0.5 0.666633
0.5 0.333367
0.499998 0.333366
0.499998 0.666632
0.250048 0.666633
0.250048 0.333366
0.25005 0.333367
0.25005 0.666633
0.000100091 0.666633
0.000100024 0.333367
0.749953 0.666639
0.749953 0.333373
0.999903 0.333373
0.999903 0.666639

----Normal----72
0 0 -1 
0 0 -1 
0 0 -1 
0 0 -1 
0 0 1 
0 0 1 
0 0 1 
0 0 1 
1 0 0 
1 0 0 
1 0 0 
1 0 0 
0 -1 0 
0 -1 0 
0 -1 0 
0 -1 0 
-1 0 0 
-1 0 0 
-1 0 0 
-1 0 0 
0 1 0 
0 1 0 
0 1 0 
0 1 0 

Also i able to parse vertices (coordinates) and textures UV and Normals - all looks like correct (i compare value that i get with values that can be obtained via sample app ImportScene available via link.

So when i try to use all data in the openGL i got some unexpected obj.

The question is - am I correctly parse indices from FBX file?

Also this is related my question regarding drawing obj in openGL.

---UPDATE---

I also find that this indices incorrect (miss ordered and not full), so the questions how to generate correct indices order from FBX file?


Solution

  • In case someone face with same problem i post own solutions (maybe there is another way to do this, this is more likely workaround than real solutions, so if any find another way - it's will be great)

        int indicessCount = index;
        int *testIndices = new int [indicessCount]; 
            index = 0;
        pairCounter = 0;
        int indexElement = 0;
        for (int i = 0; i < polygonCount; i++) {
            int lPolygonSize = pMesh->GetPolygonSize(i);
            for (int j = 0; j< lPolygonSize; j++) {
                _displayModel.indises[index++] = indexElement + j;
                if (j==2) {
                    if ((pairCounter % 1)) {
                        int firstIndex = index-3;
                        int secondIndex = index-1;
                        _displayModel.indises[index++] = _displayModel.indises[secondIndex];
                        _displayModel.indises[index++] = _displayModel.indises[firstIndex];
                        pairCounter = 0;
                    }
                    pairCounter++;
                }
            }
        indexElement += lPolygonSize;
    }
    

    also trinagulate your scene

    FbxGeometryConverter converter(_fbxManager);
    converter.Triangulate(_fbxScene, true);