【问题标题】:Interpolation Between 2 4x4 Matrices2 个 4x4 矩阵之间的插值
【发布时间】:2015-01-03 04:04:39
【问题描述】:

对于使用 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);

有什么办法可以做到吗?

【问题讨论】:

  • 我建议添加标签quaternion(单数)
  • @SeverinPappadeux 添加:)
  • @SeverinPappadeux collada 是一种能够存储骨骼动画数据的 3D 文件格式。它相当无关紧要,但我想我还是会提到它。
  • 什么样的矩阵?对于一般矩阵,线性插值就是 (1-t)A+tB。
  • @user612313 它们是 4x4 变换矩阵。我已经解决了问题并发布并回答以供将来参考。

标签: matrix interpolation quaternions


【解决方案1】:

我最终通过更多的网上冲浪解决了我的问题。为了将来参考,这里是如何做到的。

转换组件存储在一个 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;

希望能回答其他人的问题:)

【讨论】:

  • 在现代 glm 中访问翻译组件使用矩阵[3],即在上面的代码中:1)glm::vec4 transformComp1 = orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][3]2)glm::vec4 transformComp2 = orderedBones[i]->Animation->Matrices[nextKeyFrame][3]3)orderedBones[i]->Animation->interpoltaedMatrix[3] = finalTrans
【解决方案2】:

这个功能对我有用:

glm::mat4 interpolate(glm::mat4& _mat1, glm::mat4& _mat2, float _time)
{
    glm::quat rot0 = glm::quat_cast(_mat1);
    glm::quat rot1= glm::quat_cast(_mat2);

    glm::quat finalRot = glm::slerp(rot0, rot1, _time);

    glm::mat4 finalMat = glm::mat4_cast(finalRot);

    finalMat[3] = _mat1[3] * (1 - _time) + _mat2[3] * _time;
    
    return finalMat;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多