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

How to read in FBX 2014 indices properly for DirectX?

上周末我一直遇到这个非常烦人的问题。我必须能够读入包含从 Maya 导出的网格的三角化 2014 FBX 文件,并读入要传递给 DrawIndexed 的渲染器 (DirectX) 的顶点和索引打电话。

所以我设置它的方式是有一个 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 加载到 CMeshclass 中。其中包含网格的顶点和索引。

 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 调用时,索引完全乱序,在图形调试器中看起来像这样:

如果有人想知道,索引如下:

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

您在应用程序中使用的是左手视图坐标还是右手视图坐标(参见 link)?如果您使用的是左手视图坐标,则需要翻转索引缓冲区中每个三角形的缠绕。

    assert( (indices.size() % 3) == 0 );
    for( auto it = indices.begin(); it != indices.end(); it += 3 )
    {
        std::swap( *it, *(it+2) );
    }

您可能还需要翻转纹理坐标:

    for( auto it = vertices.begin(); it != vertices.end(); ++it )
    {
        it->textureCoordinate.x = ( 1.f - it->textureCoordinate.x );
    }

历史上大多数 DirectX 应用程序使用左手视图坐标。 XNA Game Studio 和大多数 OpenGL 应用程序使用右手视图坐标。

顺便说一句,这就是为什么 -fliptriangles+-invertvtexcoord+ 在 SDKMESH 的 Samples Content Exporter 中默认启用的原因。