c ++ OpenGL FBX模型未正确加载

时间:2018-01-01 15:32:22

标签: c++ opengl fbx

我试图使用OpenGL加载FBX模型,我让导入器工作并读取网格数据,以便我可以绘制它 but some parts are not displayed correctly.

这是我使用的

FbxMesh*mesh = pNode->GetMesh();
    //================= Get Vertices ====================================
    int numVerts = mesh->GetControlPointsCount();
    for(int j = 0; j < numVerts; j++)
    {
        FbxVector4 vert = mesh->GetControlPointAt(j);
        vertices[numVertices].x=(float)vert.mData[0];
        vertices[numVertices].y=(float)vert.mData[1];
        vertices[numVertices++].z=(float)vert.mData[2];
        printf("MeshVert: x: %f y: %f z: %f\n", vertices[numVertices-1].x, vertices[numVertices-1].y, vertices[numVertices-1].z);
    }
    //================= Get Indices ====================================
    numIndices = mesh->GetPolygonVertexCount();
    int triangleCount = numIndices / 3;
    indices = new int[numIndices];
    indices = mesh->GetPolygonVertices();
    printf("numIndices: %i\n", numIndices);
    printf("TriangleCount: %i\n", triangleCount);
    //================= Get Normals ====================================
    FbxGeometryElementNormal*normalEl = mesh->GetElementNormal();
    if(normalEl)
    {
        int numNormals = mesh->GetPolygonCount()*3;
        normals = new float[numNormals*3];
        int vertexCounter = 0;
        for(int polyCounter = 0 ; polyCounter<mesh->GetPolygonCount(); polyCounter++)
        {
            for(int k = 0; k < 3; k++)
            {
                FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter);
                normals[vertexCounter*3+0] = (float)normal[0];
                normals[vertexCounter*3+1] = (float)normal[1];
                normals[vertexCounter*3+2] = (float)normal[2];
                //cout<<"\n"<<normals[vertexCounter*3+0]<<" "<<normals[vertexCounter*3+1]<<" "<<normals[vertexCounter*3+2];
                vertexCounter++;
            }
        }
    }

要绘制原语

for(int i = 0; i < numIndices - 3; i++)
{
    glBegin(GL_TRIANGLES);
    glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]); 
    for(int j = i; j <= i + 2; j++)
    {
        glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
        glColor3f(0.3f, 0.3f, 0.3f);
    }
    glEnd();
}

只是想知道是否有人可以帮助这项工作。

我的其他项目代码位于https://github.com/buttburger/FBXEssentials/tree/master/OpenGL2

1 个答案:

答案 0 :(得分:0)

立即修复

for(int i = 0; i < numIndices - 3; i+=3)
{
    glBegin(GL_TRIANGLES);
    glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
    for(int j = i; j <= i + 2; j++)
    {
        glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
        glColor3f(1.0f, 1.0f, 1.0f);
    }
}
glEnd();
glFlush();
glutSwapBuffers();