2014-10-19 101 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