我以类似于以下的方式使用 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::scale
、glm::rotate
、glm::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/