2 4x4矩阵之间的插值

问题描述 投票:0回答:1

对于使用colladas的骨架动画,我需要在2个矩阵之间进行线性插值。我在某处看到我可以使用四元数在矩阵之间进行插值,但这只适用于旋转组件,我也需要保留变换。这是我的代码,除了翻译部分外,它有效:

float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]) * 100.0;
float progress = orderedBones[i]->Animation->accumTime - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1] * 100.0;
float interpolation = progress / total;

glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]);
glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]);
glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation);

orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat);

有什么方法可以做到这一点吗?

matrix interpolation quaternions
1个回答
4
投票

我最后通过更多网上冲浪解决了我的问题。为了将来的参考,继承人如何做到这一点。

转换组件存储在4x4矩阵中,如下所示:

r r r t
r r r t
r r r t
0 0 0 1

其中r是旋转分量,t是平移分量。因此,我们可以将翻译组件表示为向量。 2矢量可以线性插值,因此我们插入这两个矢量,然后在完成后将它们推回旋转矩阵。继续最后的代码,但它有点凌乱:

float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]) * ANIMATION_MULTIPLICATION_CONST;
float progress = orderedBones[i]->Animation->accumTime - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1] * ANIMATION_MULTIPLICATION_CONST;
float interpolation = progress / total;

glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]);
glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]);
glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation);

orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat);

glm::vec4 transformComp1 = glm::vec4(
    orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][0][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][1][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][2][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][3][3]);
glm::vec4 transformComp2 = glm::vec4(
    orderedBones[i]->Animation->Matrices[nextKeyFrame][0][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame][1][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame][2][3],
    orderedBones[i]->Animation->Matrices[nextKeyFrame][3][3]);

glm::vec4 finalTrans = (float)(1.0 - interpolation) * transformComp1 + transformComp2 * interpolation;

// good for now, although in future the 2 transformation components need to be interpolated
orderedBones[i]->Animation->interpoltaedMatrix[0][3] = finalTrans.x;
orderedBones[i]->Animation->interpoltaedMatrix[1][3] = finalTrans.y;
orderedBones[i]->Animation->interpoltaedMatrix[2][3] = finalTrans.z;
orderedBones[i]->Animation->interpoltaedMatrix[3][3] = finalTrans.w;

希望能回答别人的问题:)

© www.soinside.com 2019 - 2024. All rights reserved.