2014-10-19 103 views
0

我有点惊讶。我一直在调试我的代码几个小时,GLM似乎放弃了我。我有以下2个实例挣扎: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); 
... 

的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 

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

回答

4

您的displayMatrix功能令您感到困惑,因为您打印的纸张转换为您期望的纸张。 GLM使用列主要排序,所以地址是m[col][row]

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

对于temp向量,同样的问题出现了:您乘上的第一个矩阵是identity,所以它不会改变。第二个矩阵是标识,除了最后一行是0 0.5 0 1,所以x,y和z将保持不变,新的w'将为0.5 * y + w。由于y是0开头,所以这里也没有什么改变。

+0

谢谢!学过的知识! – sarora 2014-10-19 20:41:58