C++ GLM乘法顺序
我有点惊讶。我已经调试代码好几个小时了,GLM似乎要放弃我了。我正在努力解决以下两个问题:C++ GLM乘法顺序,c++,opengl,matrix,glm-math,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 = (
....
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);
...
请注意,在上面的代码中,矩阵乘法顺序与您在纸上写的相反。换句话说,代码上写着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;
由于某种原因,温度永远不会改变。我不知道该怎么办,也不知道为什么会这样。我的程序编译并运行(我从上面的输出粘贴)。当然,这不是整个计划。这只是调试的步骤。但我相信,这足够说明发生了什么。您的
displayMatrix
功能让您感到困惑,因为您将矩阵转置为您希望在纸上打印的矩阵。GLM使用列主顺序,因此寻址为m[col][row]
现在记住这一点,操作A*B
实际上是您应该期望的
对于
temp
向量,同样的问题也会出现:将其乘以的第一个矩阵是恒等式,因此它是不变的。第二个矩阵是恒等式,除了最后一行是0.5 0 1
,因此x、y和z将保持不变,新的w'将是0.5*y+w。由于y的开头是0,因此此处也没有任何更改。您的displayMatrix
函数让您感到困惑,因为您将矩阵转置为您希望在纸上打印的矩阵。GLM使用列主顺序,因此寻址为m[col][row]
现在记住这一点,操作A*B
实际上是您应该期望的
对于temp
向量,同样的问题也会出现:将其乘以的第一个矩阵是恒等式,因此它是不变的。第二个矩阵是恒等式,除了最后一行是0.5 0 1
,因此x、y和z将保持不变,新的w'将是0.5*y+w。因为y一开始是0,所以这里也没有任何变化
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
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