c++ - FBX节点变换计算

标签 c++ opengl 3d fbx

最近,尝试使用 FBX sdk 导入用 3dmax 制作的 3D 模型,我几乎立即遇到了转换问题。一个非常简单的网格(一个分成两半的球体)由两个节点组成,无论如何,其中一个节点都会偏移。我尝试了最新 SDK 文档提供的几种(相当模糊的)计算变换的方法...但结果是相同的。我将提供代码和网格,以防任何人指出任何错误。

辅助函数:

FbxAMatrix MeshManager::GetGlobalPosition(FbxNode* pNode, const FbxTime& pTime, FbxPose* pPose, FbxAMatrix* pParentGlobalPosition)
{
    FbxAMatrix lGlobalPosition;
    bool        lPositionFound = false;

    if (pPose)
    {
        int lNodeIndex = pPose->Find(pNode);

        if (lNodeIndex > -1)
        {
            // The bind pose is always a global matrix.
            // If we have a rest pose, we need to check if it is
            // stored in global or local space.
            if (pPose->IsBindPose() || !pPose->IsLocalMatrix(lNodeIndex))
            {
                lGlobalPosition = GetPoseMatrix(pPose, lNodeIndex);
            }
            else
            {
                // We have a local matrix, we need to convert it to
                // a global space matrix.
                FbxAMatrix lParentGlobalPosition;

                if (pParentGlobalPosition)
                {
                    lParentGlobalPosition = *pParentGlobalPosition;
                }
                else
                {
                    if (pNode->GetParent())
                    {
                        lParentGlobalPosition = GetGlobalPosition(pNode->GetParent(), pTime, pPose);
                    }
                }

                FbxAMatrix lLocalPosition = GetPoseMatrix(pPose, lNodeIndex);
                lGlobalPosition = lParentGlobalPosition * lLocalPosition;
            }

            lPositionFound = true;
        }
    }

    if (!lPositionFound)
    {
        // There is no pose entry for that node, get the current global position instead.

        // Ideally this would use parent global position and local position to compute the global position.
        // Unfortunately the equation 
        //    lGlobalPosition = pParentGlobalPosition * lLocalPosition
        // does not hold when inheritance type is other than "Parent" (RSrs).
        // To compute the parent rotation and scaling is tricky in the RrSs and Rrs cases.
        lGlobalPosition = pNode->EvaluateGlobalTransform(pTime);
    }

    return lGlobalPosition;
}

// Get the matrix of the given pose
FbxAMatrix MeshManager::GetPoseMatrix(FbxPose* pPose, int pNodeIndex)
{
    FbxAMatrix lPoseMatrix;
    FbxMatrix lMatrix = pPose->GetMatrix(pNodeIndex);

    memcpy((double*)lPoseMatrix, (double*)lMatrix, sizeof(lMatrix.mData));

    return lPoseMatrix;
}

// Get the geometry offset to a node. It is never inherited by the children.
FbxAMatrix MeshManager::GetGeometry(FbxNode* pNode)
{
    const FbxVector4 lT = pNode->GetGeometricTranslation(FbxNode::eSourcePivot);
    const FbxVector4 lR = pNode->GetGeometricRotation(FbxNode::eSourcePivot);
    const FbxVector4 lS = pNode->GetGeometricScaling(FbxNode::eSourcePivot);

    return FbxAMatrix(lT, lR, lS);
}

mat4 FbxMatToGlm(const FbxAMatrix& mat) {
    dvec4 c0 = glm::make_vec4((double*)mat.GetColumn(0).Buffer());
    dvec4 c1 = glm::make_vec4((double*)mat.GetColumn(1).Buffer());
    dvec4 c2 = glm::make_vec4((double*)mat.GetColumn(2).Buffer());
    dvec4 c3 = glm::make_vec4((double*)mat.GetColumn(3).Buffer());
    glm::mat4 convertMatr = mat4(c0, c1, c2, c3);
    return inverse(convertMatr);
}

网格提取:

void MeshManager::extractMeshRecursive(FbxScene* mScene, FbxNode* pNode, FbxAMatrix& pParentGlobalPosition, shared_ptr<Mesh> mesh, unsigned &currentNode) {
    // Find out what type of node this is
    FbxNodeAttribute* lNodeAttribute = pNode->GetNodeAttribute();

    FbxAMatrix lGlobalPosition = GetGlobalPosition(pNode, 1, mScene->GetPose(-1) , &pParentGlobalPosition);
    FbxAMatrix lGeometryOffset = GetGeometry(pNode);
    FbxAMatrix lGlobalOffsetPosition = lGlobalPosition * lGeometryOffset;

    if (lNodeAttribute)
    {
        // Get the actual node mesh data if it is a mesh this time
        // (You could use this like the sample where they draw other nodes like cameras)
        if (lNodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh)
        {
            // Draw the actual mesh data
            FbxMesh* lMesh = pNode->GetMesh();

            if (lMesh->IsTriangleMesh() == false) {
                FbxGeometryConverter conv(mFbxManager);
                conv.Triangulate(lNodeAttribute, true);
            }

            const uint lVertexCount = lMesh->GetControlPointsCount();
            const uint lTriangleCount = lMesh->GetPolygonCount();

            // May not have any vertex data
            if (lVertexCount == 0) return;

            mesh->nodes.push_back(MeshNode());

            FbxVector4* pControlPoints = lMesh->GetControlPoints();
            for (uint i = 0; i < lVertexCount; i++)
            {
                mesh->nodes[currentNode].vertices.push_back(vec3((float)pControlPoints[i].mData[0], (float)pControlPoints[i].mData[1], (float)pControlPoints[i].mData[2]));
            }

            mesh->nodes[currentNode].localTransform = FbxMatToGlm(lGlobalOffsetPosition);
         }
         currentNode++;
     }
... Extracting other vertex attributes and materials ...

// Now check if this node has any children attached
    const int lChildCount = pNode->GetChildCount();
    for (int lChildIndex = 0; lChildIndex < lChildCount; ++lChildIndex)
    {
        // Draw this child
        extractMeshRecursive(mScene, pNode->GetChild(lChildIndex), lGlobalPosition, mesh, currentNode);
    }
}

我得到的结果如下所示:enter image description here 相对于:enter image description here

A Mesh

最佳答案

这里有不正确的部分:

mat4 FbxMatToGlm(const FbxAMatrix& mat) {
    dvec4 c0 = glm::make_vec4((double*)mat.GetColumn(0).Buffer());
    dvec4 c1 = glm::make_vec4((double*)mat.GetColumn(1).Buffer());
    dvec4 c2 = glm::make_vec4((double*)mat.GetColumn(2).Buffer());
    dvec4 c3 = glm::make_vec4((double*)mat.GetColumn(3).Buffer());
    glm::mat4 convertMatr = mat4(c0, c1, c2, c3);
    return inverse(convertMatr); // <---  Incorrect
}

无需对所得矩阵求逆。它应该被转置代替。我一开始就是这样做的,但是未调整的网格比例太大了,我在渲染器中看不到它,我开始修改它。在 3D Studio 的 FBX 导出窗口中将毫米作为单位后,所有转换都是正确的。

关于c++ - FBX节点变换计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245433/

相关文章:

c++ - 为 map vector 容器的元素赋值时发生访问冲突

c++ - OpenGL 三角形未渲染

python - 有没有办法使用 matplotlib 来制作 "3D cloud plot"?

3d - 用于样条拟合的 RANSAC

javascript - threejs 混合 json 导出 + 无法读取未定义的属性 'type'

c++ - 我可以在 1 行中使用非默认数据类型作为函数的参数吗?

c++ - 定时一次标志

java - 事件多次触发 - LWJGL

C++ OpenGL 在 GL_QUAD、glVertex2f 中加载图像

c++ - 将指针传递给由 exec() 生成的进程