FBX节点变换计算

无处01

最近,尝试使用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);
    }
}

我得到的结果看起来像这样:在此处输入图片说明相对于:在此处输入图片说明

网格

无处01

不正确的部分在这里:

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导出窗口中将毫米设置为单位,所有转换都是正确的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

fbx网格荷载几何变换

来自分类Dev

如何计算离散傅立叶变换?

来自分类Dev

节点流:拉n行,变换并继续

来自分类Dev

从Maya C ++ API中的变换节点获取网格节点

来自分类Dev

根据平面方程计算变换矩阵

来自分类Dev

计算“透视变换”目标图像的宽高比

来自分类Dev

您如何计算相机的变换矩阵?

来自分类Dev

根据平面方程计算变换矩阵

来自分类Dev

从2组点计算仿射变换

来自分类Dev

如何计算多个点的仿射变换?

来自分类Dev

什么是openStack计算节点?

来自分类Dev

计算多个节点集

来自分类Dev

使用Function.bind时的节点变换流怪癖

来自分类Dev

Maya变换节点未出现在列表中

来自分类Dev

在python(OpenCV)中加速与傅立叶相关的变换计算

来自分类Dev

傅里叶变换:计算标度作为向量长度的函数

来自分类Dev

如何对均匀变换矩阵/张量进行矢量化计算?

来自分类Dev

使用Numpy(坐标变换)计算长表达式?

来自分类Dev

Javascript:ParseInt计算出的变换矩阵值返回NaN

来自分类Dev

使用边界框坐标计算旋转矩形变换

来自分类Dev

如何在 CPU 上计算向量变换?

来自分类Dev

Maya 变换节点“黑匣子”——从大纲中隐藏节点层次

来自分类Dev

如何从fbx文件中获取所有节点或多边形的名称?

来自分类Dev

使用XPATH计算子节点

来自分类Dev

图算法计算节点度

来自分类Dev

大量设备/节点的距离计算

来自分类Dev

计算每个节点的Strahler数

来自分类Dev

计算路径中的不同节点

来自分类Dev

XSLT总和基于节点的计算?