我有点惊讶。我已经调试了几个小时的代码,而GLM似乎放弃了我。我在以下2个实例中挣扎:
....
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);
...
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;
}
这是我得到的输出:
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
注意,在上面的代码中,矩阵乘法的顺序与您在纸上写的相反。换句话说,代码显示为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;
我现在得到的输出是:
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
由于某种原因,温度永远不会改变。我不知道该怎么办,或者为什么会这样。我的程序可以编译并运行(我从上面的输出中粘贴)。当然,这不是整个程序。这只是调试步骤。但是我有信心,这足以说明发生了什么。
最佳答案
您的displayMatrix
函数使您感到困惑,因为您打印的矩阵要换成您希望在纸上看到的矩阵。 GLM使用列主要顺序,因此寻址为m[col][row]
。
现在记住这一点,实际上是您应该期望的操作A*B
。
对于temp
向量,会出现相同的问题:将其乘以的第一个矩阵是恒等,因此不变。第二个矩阵是单位,除了最后一行是0 0.5 0 1
,因此x,y和z将保持不变,而新的w'将是0.5 * y + w。因为y首先是0,所以这里也没有任何改变。
关于c++ - GLM乘法顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26454838/