【问题标题】:GLM multiplication orderGLM 乘法顺序
【发布时间】:2014-10-19 20:04:01
【问题描述】:

我有点惊讶。我已经调试代码好几个小时了,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

Temp 永远不会因为某种原因而改变。我不知道该怎么做,也不知道为什么会这样。我的程序编译并运行(我从上面的输出粘贴)。当然,这不是整个程序。这只是调试的步骤。但我有信心,这么多应该足以说明发生了什么。

【问题讨论】:

    标签: c++ opengl matrix glm-math


    【解决方案1】:

    您的displayMatrix 函数让您感到困惑,因为您打印的矩阵转换为您在纸上所期望的。 GLM 使用列主序,因此寻址为m[col][row]

    现在考虑到这一点,A*B 操作实际上是您所期望的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2020-03-25
      • 2012-02-05
      • 1970-01-01
      • 2016-06-19
      • 2015-08-21
      • 2018-05-27
      相关资源
      最近更新 更多