c++ - glm::decompose 然后再次组合

标签 c++ matrix linear-algebra glm-math

我以类似于以下的方式使用 glm::decompose ( https://glm.g-truc.net/0.9.6/api/a00204.html):

glm::mat4 matrix;
// ...
glm::vec3 scale;
glm::quat rotation;
glm::vec3 translation;
glm::vec3 skew;
glm::vec4 perspective;
glm::decompose(matrix, scale, rotation, translation, skew, perspective);

现在我想使用上述所有属性再次组合矩阵。如果矩阵中只有缩放、旋转和平移(glm::scaleglm::rotateglm::translate) 但我最感兴趣的是“偏斜”属性。我怎样才能将所有变换应用到一个新矩阵,以便在计算之后我能再次得到“矩阵”?

最佳答案

如评论中所述,答案在源代码中,文件中的最后一个函数b , 引用于 a

带来功能重构

void TransformationMatrix::recompose(const DecomposedType& decomp)
{
    makeIdentity();
    
    // first apply perspective
    m_matrix[0][3] = (float) decomp.perspectiveX;
    m_matrix[1][3] = (float) decomp.perspectiveY;
    m_matrix[2][3] = (float) decomp.perspectiveZ;
    m_matrix[3][3] = (float) decomp.perspectiveW;
    
    // now translate
    translate3d((float) decomp.translateX, 
                (float) decomp.translateY, 
                (float) decomp.translateZ);
    
    // apply rotation
    double xx = decomp.quaternionX * decomp.quaternionX;
    double xy = decomp.quaternionX * decomp.quaternionY;
    double xz = decomp.quaternionX * decomp.quaternionZ;
    double xw = decomp.quaternionX * decomp.quaternionW;
    double yy = decomp.quaternionY * decomp.quaternionY;
    double yz = decomp.quaternionY * decomp.quaternionZ;
    double yw = decomp.quaternionY * decomp.quaternionW;
    double zz = decomp.quaternionZ * decomp.quaternionZ;
    double zw = decomp.quaternionZ * decomp.quaternionW;
    
    // Construct a composite rotation matrix from the quaternion values
    TransformationMatrix rotationMatrix(
      1 - 2 * (yy + zz), 2 * (xy - zw)    , 2 * (xz + yw)    , 0, 
      2 * (xy + zw)    , 1 - 2 * (xx + zz), 2 * (yz - xw)    , 0,
      2 * (xz - yw)    , 2 * (yz + xw)    , 1 - 2 * (xx + yy), 0,
      0                , 0                , 0                , 1);
    
    multLeft(rotationMatrix);
    //////////////////////////////////////////
    // THIS IS WHAT YOU ARE INTERESTED      //
    //////////////////////////////////////////
    // now apply skew
    if (decomp.skewYZ) {
        TransformationMatrix tmp;
        tmp.setM32((float) decomp.skewYZ);
        multLeft(tmp);
    }
    
    if (decomp.skewXZ) {
        TransformationMatrix tmp;
        tmp.setM31((float) decomp.skewXZ);
        multLeft(tmp);
    }
    
    if (decomp.skewXY) {
        TransformationMatrix tmp;
        tmp.setM21((float) decomp.skewXY);
        multLeft(tmp);
    }
    
    // finally, apply scale
    scale3d((float) decomp.scaleX, 
            (float) decomp.scaleY, 
            (float) decomp.scaleZ);

}

关于c++ - glm::decompose 然后再次组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54043989/

相关文章:

android - 矩阵操作矩形的 HitTest

scala - Scala 中的特征分解

c++ - 为什么添加两个 std::vector 比从 new[] 中添加原始数组慢?

c++ - C++ 中缓存对齐内存使用的类模板

c++ - 将指向未知实例(但已知类)的已知成员的指针转换为拥有实例

c++ - 使用 constexpr C 字符串作为编译器错误消息

matlab - 如何在matlab中找到矩阵中的唯一行,其中行中数字的顺序并不重要?

c++ - ping 本地网络地址的最快方法

c - 方阵置换

algorithm - 了解方阵的奇异值分解算法