gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:43:31 25 4
gpt4 key购买 nike

上周末我一直遇到这个非常烦人的问题。我必须能够读入包含从 Maya 导出的网格的三角剖分 2014 FBX 文件,并读入要传递给渲染器 (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

最佳答案

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

    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 应用程序使用左手 View 坐标。 XNA Game Studio 和大多数 OpenGL 应用程序使用右手 View 坐标。

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

关于c++ - 如何正确读取 DirectX 的 FBX 2014 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com