如何正确读取DirectX的FBX 2014索引?

时间:2015-05-11 14:36:04

标签: c++ directx mesh indices fbx

上周末我一直有这个非常讨厌的问题。我必须能够读取一个三角形的2014 FBX文件,其中包含从Maya导出的网格,并读入它的顶点和索引以传递给渲染器(DirectX)以获取 DrawIndexed 打电话。

所以我设置它的方式是有一个 TUniqueVertex 结构

struct TUniqueVertex
{
    XMFLOAT3 m_vPosition,   // Vertex positions
    XMFLOAT3 m_vNormal,     // Vertex normals
    XMFLOAT2 m_UVs          // Vertex UVs
    int m_nControlPointIndex, // Control Point Index
 }

此处的代码将在FbxMesh中加载到 CMesh 类中。其中包含网格的顶点和索引。

 class CMesh
 {
     ...
    vector<TUniqueVertex> m_vVertices;
    vector<unsigned int> m_vIndices;
 }

要加载到 CMesh

的代码
bool LoadMesh(FbxMesh* pMesh, CMesh cMesh)
{
    int polygonCount = pMesh->GetPolygonCount();        
    FbxVector4* controlPoints = pMesh->GetControlPoints();
    int controlPointCount = pMesh->GetControlPointsCount();

    int vertexID = 0;

    for (int polygon = 0; polygon < polygonCount; polygon++)
    {
        int polyVertCount = pMesh->GetPolygonSize(polygon);

    for (int polyVert = 0; polyVert < polyVertCount; polyVert++)
    {
        CMesh::TUniqueVertex uniqueVert;

        int cpIndex = pMesh->GetPolygonVertex(polygon, polyVert);

        // Grab our CP index as well our position information
        uniqueVert.m_nControlPointIndex = cpIndex;
        uniqueVert.m_vPosition = 
        XMFLOAT3((float)controlPoints[cpIndex].mData[0],
                 (float)controlPoints[cpIndex].mData[1],
                 (float)controlPoints[cpIndex].mData[2]);

        // Grab UVs
        int uvElementCount = pMesh->GetElementUVCount();

        for (int uvElement = 0; uvElement < uvElementCount; uvElement++)
        {
            FbxGeometryElementUV* geomElementUV = pMesh>GetElementUV(uvElement);

            FbxLayerElement::EMappingMode mapMode = geomElementUV->GetMappingMode();
            FbxLayerElement::EReferenceMode refMode = geomElementUV->GetReferenceMode();

            int directIndex = -1;

            if (FbxGeometryElement::eByControlPoint == mapMode)
            {
                if (FbxGeometryElement::eDirect == refMode)
                {
                    directIndex = cpIndex;
                }
                else if (FbxGeometryElement::eIndexToDirect == refMode)
                {
                    directIndex = geomElementUV->GetIndexArray().GetAt(cpIndex);
                }
            }
            else if (FbxGeometryElement::eByPolygonVertex == mapMode)
            {
                if (FbxGeometryElement::eDirect == refMode || FbxGeometryElement::eIndexToDirect == refMode)
                {
                    directIndex = pMesh->GetTextureUVIndex(polygon, polyVert);
                }

            }

            // If we got a UV index
            if (directIndex != -1)
            {
                FbxVector2 uv = geomElementUV->GetDirectArray().GetAt(directIndex);

                uniqueVert.m_UVs = XMFLOAT2( (float)uv.mData[0],
                                             (float)uv.mData[1]);
            }
        }

        // Grab normals
        int normElementCount = pMesh->GetElementNormalCount();

        for (int normElement = 0; normElement < normElementCount; normElement++)
        {
            FbxGeometryElementNormal* geomElementNormal = pMesh->GetElementNormal(normElement);

            FbxLayerElement::EMappingMode mapMode = geomElementNormal->GetMappingMode();
            FbxLayerElement::EReferenceMode refMode = geomElementNormal->GetReferenceMode();

            int directIndex = -1;

            if (FbxGeometryElement::eByPolygonVertex == mapMode)
            {
                if (FbxGeometryElement::eDirect == refMode)
                {
                    directIndex = vertexID;
                }
                else if (FbxGeometryElement::eIndexToDirect == refMode)
                {
                    directIndex = geomElementNormal->GetIndexArray().GetAt(vertexID);
                }
            }

            // If we got an index
            if (directIndex != 1)
            {
                FbxVector4 norm = geomElementNormal->GetDirectArray().GetAt(directIndex);

                uniqueVert.m_vNormal = XMFLOAT3((float)norm.mData[0],
                                                (float)norm.mData[1],
                                                (float)norm.mData[2]);

            }
        }

        // Unique Vertices stuff
        vector<CMesh::TUniqueVertex>& uniqueVerts = cMesh.GetVertices();

        size_t size = uniqueVerts.size();
        size_t i;

        for (i = 0; i < size; i++)
        {
            if (uniqueVert == uniqueVerts[i])
            {
                break;
            }
        }

        if (i == size)
        {
            uniqueVerts.push_back(uniqueVert);
        }

        cMesh.GetIndices().push_back(i);
        ++vertexID;
    }
}

return true;
}

执行此方法可正确加载顶点,法线和UV。但是,在索引中加载并将cMesh.GetIndices()计数传递给 DrawIndexed 调用时,索引完全无序,在图形调试器中如下所示:

Graphic Debugger

如果有人想知道,指数如下:

012,023,456,657,891,081,011,121,314,121,415,161,718,171,918,202,122,212,023

以下是我尝试从以下网址加载信息的测试.fbx:

Cube.fbx direct download

如果有人可以帮我解决这个问题,我会非常感激,因为这个问题给我带来了太大的压力:D

0 个答案:

没有答案