gpu换肤的矩阵计算 [英] Matrix calculations for gpu skinning

查看:279
本文介绍了gpu换肤的矩阵计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Assimp 作为我的模型导入库,在 OpenGL 中执行骨架动画。

I'm trying to do skeletal animation in OpenGL using Assimp as my model import library.

我需要什么,与骨骼的 offsetMatrix 变量?我需要乘以?

What exactly do I need to the with the bones' offsetMatrix variable? What do I need to multiply it by?

推荐答案

让我们来看看这个代码,我用来动画游戏中的角色我工作。我也使用Assimp来加载骨骼信息,我自己读了Nico已经指出的OGL教程。

Let's take for instance this code, which I used to animate characters in a game I worked. I used Assimp too, to load bone information and I read myself the OGL tutorial already pointed out by Nico.

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

void updateSkeleton(Bone* bone = NULL)
{ 
    bone->nodeTransform =  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->nodeTransform  //defined above
        * bone->boneOffset;  //which is ai_mesh->mBones[i]->mOffsetMatrix


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

本质上 GlobalTransform ,如教程 Assimp中的骨骼动画或适当地根节点的变换 scene-> mRootNode-> mTransformation 是从局部空间到全局空间的变换。为了给你一个例子,当在一个3D建模器(例如我们选择Blender)中创建你的网格或你加载你的角色,它通常被定位(默认)在笛卡尔平面的原点,它的旋转设置为

Essentially the GlobalTransform as it is referred in the tutorial Skeletal Animation with Assimp or properly the transform of the root node scene->mRootNode->mTransformation is the transformation from local space to global space. To give you an example, when in a 3D modeler (let's pick Blender for instance) you create your mesh or you load your character, it is usually positioned (by default) at the origin of the Cartesian plane and its rotation is set to the identity quaternion.

但是,你可以从原点(0,0,0)到其他地方,并且在单个场景中甚至具有不同位置的多个网格。当你加载它们,特别是如果你做骨架动画,它是强制性的翻译他们回到本地空间(即回到原点 0,0,0 ),这是你必须将所有的东西乘以 InverseGlobal (这将你的网格带回到局部空间)。

However you can translate/rotate your mesh/character from the origin (0,0,0) to somewhere else and have in a single scene even multiple meshes with different positions. When you load them, especially if you do skeletal animation, it is mandatory to translate them back in local space (i.e. back at the origin 0,0,0 ) and this is the reason why you have to multiply everything by the InverseGlobal (which brings back your mesh to local space).

之后,你需要乘以节点变换,它是 parentTransform 的乘法(在树中的变换一级,这是整体变换) transform (以前是 assimp_node-> mTransformation ,它只是骨骼相对于节点父节点的变换)变换(任意T * R):正向运动,逆运动或关键帧插值。

After that you need to multiply it by the node transform which is the multiplication of the parentTransform (the transformation one level up in the tree, this is the overall transform) the transform (formerly the assimp_node->mTransformation which is just the transformation of the bone relative to the node's parent) and your local transformation (any T * R) you want to apply to do: forward kinematic, inverse kinematic or key-frame interpolation.

最终有boneOffset( ai_mesh-> mBones [i] - > mOffsetMatrix

Eventually there is the boneOffset (ai_mesh->mBones[i]->mOffsetMatrix) that transforms from mesh space to bone space in bind pose as stated in the documentation.

这里有一个指向 GitHub 如果你想看看我的Skeleton类的整个代码。

Here there is a link to GitHub if you want to look at the whole code for my Skeleton class.

希望它有帮助。

这篇关于gpu换肤的矩阵计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆