GLM乘法顺序

sar*_*ora 0 c++ opengl matrix glm-math

我有点惊讶.我一直在调试我的代码几个小时,而GLM似乎放弃了我.我正在努力完成以下两个实例:

....
cout << "multiplying A:" << endl;
displayMatrix(node->wMatrix);

cout << "and B:" << endl;
displayMatrix((node->children)[i]->wMatrix);

//switch order!
mat4 temp = (node->children)[i]->wMatrix * node->wMatrix;

cout << "Get result as:" << endl;
displayMatrix(temp);
...
Run Code Online (Sandbox Code Playgroud)

displayMatrix方法如下:

void displayMatrix(mat4 &m)
{
    cout << m[0][0] << "  " << m[0][1] << "  " << m[0][2] << "  " << m[0][3] << endl;
    cout << m[1][0] << "  " << m[1][1] << "  " << m[1][2] << "  " << m[1][3] << endl;
    cout << m[2][0] << "  " << m[2][1] << "  " << m[2][2] << "  " << m[2][3] << endl;
    cout << m[3][0] << "  " << m[3][1] << "  " << m[3][2] << "  " << m[3][3] << endl;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

multiplying A:
1  0  0  0
0  1  0  0.5
0  0  1  0
0  0  0  1
and B:
0.540302  -0.841471  0  0
0.841471  0.540302  0  -0.5
0  0  1  0
0  0  0  1
Get result as:
0.540302  -0.841471  0  0
0.841471  0.540302  0  0
0  0  1  0
0  0  0  1
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的代码中,矩阵乘法顺序与您在纸上书写的顺序相反.换句话说,代码说B*A.我被这个抛弃了.

第二个例子:

cout << "temp:" << endl;
cout << temp.x << "  " << temp.y << "  " << temp.z << "  " << temp.w << endl;

cout << "binding matrix inverse: " << endl;
displayMatrix(bindingInvs.at(jIndex));

temp = bindingInvs.at(jIndex) * temp;
cout << "now temp:" << endl;
cout << temp.x << "  " << temp.y << "  " << temp.z << "  " << temp.w << endl;

cout << "joint world matrix: " << endl;
displayMatrix(joints.at(jIndex)->wMatrix);
temp = (joints.at(jIndex)->wMatrix) * temp;
cout << "now temp:" << endl;
cout << temp.x << "  " << temp.y << "  " << temp.z << "  " << temp.w << endl;

cout << "weight: " << jWeight << endl;
temp = jWeight * temp;

cout << "now temp:" << endl;
cout << temp.x << "  " << temp.y << "  " << temp.z << "  " << temp.w << endl;
Run Code Online (Sandbox Code Playgroud)

我现在得到的输出是:

temp:
0.087  0  -0.05  1
binding matrix inverse:
1  -0  0  -0
-0  1  -0  0
0  -0  1  -0
-0  0  -0  1
now temp:
0.087  0  -0.05  1
joint world matrix:
1  0  0  0
0  1  0  0.5
0  0  1  0
0  0  0  1
now temp:
0.087  0  -0.05  1
weight: 1
now temp:
0.087  0  -0.05  1
Run Code Online (Sandbox Code Playgroud)

由于某种原因,温度永远不会改变.我不知道该怎么做,或者为什么会这样.我的程序编译并运行(我从上面的输出粘贴).当然,这不是整个计划.这只是调试的步骤.但我相信这应该足以说明发生了什么.

der*_*ass 5

您的displayMatrix功能让您感到困惑,因为您打印的矩阵转换为您在纸上的预期.GLM使用列主要排序,因此寻址是m[col][row].

现在考虑到这一点,操作A*B实际上是您应该期待的.

对于temp向量,出现了同样的问题:你乘以它的第一个矩阵是身份,所以它没有变化.第二个矩阵是同一性,除了最后一行是0 0.5 0 1,所以x,y和z将保持不变,新的w'将是0.5*y + w.由于y从0开始,所以这里也没有任何改变.