c++ - 如何使用 assimp 在 C++ 中旋转蒙皮模型的骨骼?

标签 c++ opengl matrix rotation assimp

我可以使用 assimp 通过在关键帧之间进行插值来加载蒙皮模型的动画。现在,我一直在尝试从用户定义的变换矩阵中定位或定位骨骼,而不是仅仅从动画文件中加载它。比如,将 ARM 旋转一定角度,该角度由用户指定。我将模型加载到它的绑定(bind)姿势中:

void recursion(aiNode* gNode)
{
 std::string gNodeName(gNode->mName.data);
 if(boneMapping.find(gNodeName) != boneMapping.end())
 {
  //if node corresponds to a bone,
  Matrix4f boneMatrix = IdentityMatrix;
  aiNode* tempNode = gNode;
     //find combined transform of a bone
     while(tempNode != NULL)
     {
        Matrix4f NodeMatrix(tempNode->mTransformation);
        boneMatrix =  NodeMatrix * boneMatrix;
        tempNode = tempNode->mParent;
     }

     pBoneData[boneId].FinalTransform = GlobalInverseTransform * boneMatrix * pBoneData[boneId].OffsetMatrix;
  }
 for(int i = 0; i < gNode->mNumChildren; i++)
 { //repeat this process for child nodes
    recursion(gNode->mChildren[i]);
 }
}

为了使用变换矩阵定位模型的其中一个网格,我尝试搜索与网格的父骨骼相对应的骨骼名称,然后将其节点矩阵替换为所需的矩阵。然而,这根本不起作用,因为它使骨骼在不同的网格中变形。

enter image description here

右边的模型是 T 姿势,我打算通过将颈部的骨骼旋转 45 度角来修改它,但它保持不变,腿部变形,如左图所示。因此,指向现有文章或答案的任何链接都可能非常有用。

最佳答案

我认为这一行乘法矩阵的顺序是错误的:

boneMatrix =  NodeMatrix * boneMatrix;

应该是:

boneMatrix = boneMatrix * NodeMatrix;

在 OpenGL 中,乘法的顺序是相反的。

虽然这种方法对我来说听起来不对:

while(tempNode != NULL)
{
    Matrix4f NodeMatrix(tempNode->mTransformation);
    boneMatrix =  NodeMatrix * boneMatrix;
    tempNode = tempNode->mParent;
}

您正在将局部变换乘以骨骼空间中的骨骼矩阵,而不是世界空间中的骨骼矩阵。这就是为什么你的角色看起来变形了。

我使用 assimp 和 OpenGL 为我的动画项目解决了这个问题。我使用了一种稍微不同的方法,将所有 assimp 信息保存到我自己的数据结构中。 (骨架和骨头)。通过查看您的代码和您使用的数据结构,我猜您的代码基于此 tutorial .

这是我使用的代码,我相信您可以用它来与您的实现进行比较:

glm::mat4 getParentTransform()
{
    if (this->parent)
        return parent->globalTransform;
    else 
        return glm::mat4(1.0f);
}

void updateSkeleton(Bone* bone = NULL)
{ 
    bone->globalTransform =  bone->getParentTransform() // This retrieve the transformation one level above in the tree
    * bone->transform //bone->transform is the assimp matrix assimp_node->mTransformation
    * bone->localTransform;  //this is your T * R matrix

    bone->finalTransform = inverseGlobal // which is scene->mRootNode->mTransformation from assimp
        * bone->globalTransform  //defined above
        * bone->boneOffset;  //which is ai_mesh->mBones[i]->mOffsetMatrix


    for (int i = 0; i < bone->children.size(); i++) {
        updateSkeleton (&bone->children[i]);
    }
}

编辑:

对于从 Assimp 到 glm 的矩阵转换,我使用了这个函数:

inline glm::mat4 aiMatrix4x4ToGlm(const aiMatrix4x4* from)
{
    glm::mat4 to;


    to[0][0] = (GLfloat)from->a1; to[0][1] = (GLfloat)from->b1;  to[0][2] = (GLfloat)from->c1; to[0][3] = (GLfloat)from->d1;
    to[1][0] = (GLfloat)from->a2; to[1][1] = (GLfloat)from->b2;  to[1][2] = (GLfloat)from->c2; to[1][3] = (GLfloat)from->d2;
    to[2][0] = (GLfloat)from->a3; to[2][1] = (GLfloat)from->b3;  to[2][2] = (GLfloat)from->c3; to[2][3] = (GLfloat)from->d3;
    to[3][0] = (GLfloat)from->a4; to[3][1] = (GLfloat)from->b4;  to[3][2] = (GLfloat)from->c4; to[3][3] = (GLfloat)from->d4;

    return to;
}

希望对您有所帮助。

关于c++ - 如何使用 assimp 在 C++ 中旋转蒙皮模型的骨骼?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29184311/

相关文章:

c++ - 如果某些类继承了抽象基类,则确定何时在某些类上使用函数

c++ - 为什么 glDrawElements 给我 GL_OUT_OF_MEMORY?

c - 如何在C中转置矩阵? - 错误

OpenGL 闪烁的像素伪像

opengl - 查明 GL_TEXTURE_2D 是否在着色器中处于事件状态

python - np.dot() 与 Python 广播

c - 高效的微型 boolean 矩阵乘法

C++ 在对象的对象内调用成员函数

c++ - QMessageLogger : Singleton for a Logger?

c++ - 模块机器类型和目标机器类型 Visual Studio 2013