首页 > 解决方案 > 在 FBX 中,如何知道哪些顶点索引对应于哪些控制点索引?

问题描述

我目前正在尝试加载用于 DirectX 的 FBX 网格,但我的 FBX 文件的 UV 由顶点索引存储,法线由控制点索引存储。我如何知道哪些顶点具有哪些控制点的值?

我的加载位置、uvs 和法线的代码是直接从 fbx 示例代码中提取的,但如果需要,我可以发布它。

编辑:根据要求,这是我正在谈论的代码部分。

UV 代码将通过 vert index 进入 if 语句进行映射模式,而普通代码通过 ctrl 点设置为映射模式

//load uvs
if (lUVElement->GetMappingMode() == FbxGeometryElement::eByControlPoint)
                {
                    for (int lPolyIndex = 0; lPolyIndex < lPolyCount; ++lPolyIndex)
                    {
                        // build the max index array that we need to pass into MakePoly
                        const int lPolySize = mesh->GetPolygonSize(lPolyIndex);
                        for (int lVertIndex = 0; lVertIndex < lPolySize; ++lVertIndex)
                        {

                            //get the index of the current vertex in control points array
                            int lPolyVertIndex = mesh->GetPolygonVertex(lPolyIndex, lVertIndex);

                            //the UV index depends on the reference mode
                            int lUVIndex = lUseIndex ? lUVElement->GetIndexArray().GetAt(lPolyVertIndex) : lPolyVertIndex;

                            lUVValue = lUVElement->GetDirectArray().GetAt(lUVIndex);

                            _floatVec->push_back((float)lUVValue.mData[0]);
                            _floatVec->push_back((float)lUVValue.mData[1]);
                        }
                    }
                }
                else if (lUVElement->GetMappingMode() == FbxGeometryElement::eByPolygonVertex)
                {
                    int lPolyIndexCounter = 0;
                    for (int lPolyIndex = 0; lPolyIndex < lPolyCount; ++lPolyIndex)
                    {
                        // build the max index array that we need to pass into MakePoly
                        const int lPolySize = mesh->GetPolygonSize(lPolyIndex);
                        for (int lVertIndex = 0; lVertIndex < lPolySize; ++lVertIndex)
                        {
                            if (lPolyIndexCounter < lIndexCount)
                            {

                                //the UV index depends on the reference mode
                                int lUVIndex = lUseIndex ? lUVElement->GetIndexArray().GetAt(lPolyIndexCounter) : lPolyIndexCounter;

                                lUVValue = lUVElement->GetDirectArray().GetAt(lUVIndex);

                                _floatVec->push_back((float)lUVValue.mData[0]);
                                _floatVec->push_back((float)lUVValue.mData[1]);

                                lPolyIndexCounter++;
                            }
                        }
                    }
                }

//and now normals
if (lNormalElement->GetMappingMode() == FbxGeometryElement::eByControlPoint)
                {
                    //Let's get normals of each vertex, since the mapping mode of normal element is by control point
                    for (int lVertexIndex = 0; lVertexIndex < mesh->GetControlPointsCount(); lVertexIndex++)
                    {
                        int test = mesh->GetControlPointsCount();
                        int lNormalIndex = 0;
                        //reference mode is direct, the normal index is same as vertex index.
                        //get normals by the index of control vertex
                        if (lNormalElement->GetReferenceMode() == FbxGeometryElement::eDirect)
                            lNormalIndex = lVertexIndex;

                        //reference mode is index-to-direct, get normals by the index-to-direct
                        if (lNormalElement->GetReferenceMode() == FbxGeometryElement::eIndexToDirect)
                            lNormalIndex = lNormalElement->GetIndexArray().GetAt(lVertexIndex);

                        //Got normals of each vertex.
                        FbxVector4 lNormal = lNormalElement->GetDirectArray().GetAt(lNormalIndex);

                        _floatVec->push_back((float)lNormal[0]);
                        _floatVec->push_back((float)lNormal[1]);
                        _floatVec->push_back((float)lNormal[2]);

                    }
                }
                else if (lNormalElement->GetMappingMode() == FbxGeometryElement::eByPolygonVertex)
                {

                            //etc... code wont go here
                        }
                    }
                }
            }

那么我怎么知道哪些顶点会有哪些法线呢?

标签: c++directx-11fbx

解决方案


推荐阅读