【问题标题】:c++ OpenGL FBX model not loaded properlyc ++ OpenGL FBX模型未正确加载
【发布时间】:2018-06-11 13:33:33
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 您的原始绘图将顶点数据视为三角形条带。第一个三角形使用索引 0、1 和 2。第二个三角形使用顶点 1、2 和 3。依此类推。我怀疑您的外部循环应该将“i”增加 3,或者您的内部循环应该将“i”乘以 3,就像您对法线所做的那样。
  • 感谢一百万,我已按照您的指示完成操作,并在外循环中将 'i' 增加了 3,得到了我想要的结果。

标签: c++ opengl fbx


【解决方案1】:

现在解决了

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();

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2023-03-03
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多