glm::d ecompose,然后重新组合



我使用glm::d ecompose (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 中的最后一个函数,在

引入函数重构

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);
}

根据 Bob 的回答,glm::recompose()看起来像这样(带有问题中的变量名称):

    glm::mat4 m = glm::mat4(1.f);
    m[0][3] = perspective.x;
    m[1][3] = perspective.y;
    m[2][3] = perspective.z;
    m[3][3] = perspective.w;
    m *= glm::translate(translation);
    m *= glm::mat4_cast(rotation);
    if (skew.x) {
        glm::mat4 tmp { 1.f };
        tmp[2][1] = skew.x;
        m *= tmp;
    }
    if (skew.y) {
        glm::mat4 tmp { 1.f };
        tmp[2][0] = skew.y;
        m *= tmp;
    }
    if (skew.z) {
        glm::mat4 tmp { 1.f };
        tmp[1][0] = skew.z;
        m *= tmp;
    }
    m *= glm::scale(scale);

相关内容

  • 没有找到相关文章

最新更新