【问题标题】:How to multiply 4x4 float matrices in C++?如何在 C++ 中乘以 4x4 浮点矩阵?
【发布时间】:2017-12-02 22:48:13
【问题描述】:

我有一个简单的结构“mat4”,由 float[4][4] 和一个用于乘以 4x4 矩阵的 *= 函数组成。它需要一个 const mat4& "rhs" 如下:

this->m[0][0] = this->m[0][0] * rhs[0][0] + this->m[0][1] * rhs[1][0] + this->m[0][2] * rhs[2][0] + this->m[0][3] * rhs[3][0];
this->m[0][1] = this->m[0][0] * rhs[0][1] + this->m[0][1] * rhs[1][1] + this->m[0][2] * rhs[2][1] + this->m[0][3] * rhs[3][1];
this->m[0][2] = this->m[0][0] * rhs[0][2] + this->m[0][1] * rhs[1][2] + this->m[0][2] * rhs[2][2] + this->m[0][3] * rhs[3][2];
this->m[0][3] = this->m[0][0] * rhs[0][3] + this->m[0][1] * rhs[1][3] + this->m[0][2] * rhs[2][3] + this->m[0][3] * rhs[3][3];

this->m[1][0] = this->m[1][0] * rhs[0][0] + this->m[1][1] * rhs[1][0] + this->m[1][2] * rhs[2][0] + this->m[1][3] * rhs[3][0];
this->m[1][1] = this->m[1][0] * rhs[0][1] + this->m[1][1] * rhs[1][1] + this->m[1][2] * rhs[2][1] + this->m[1][3] * rhs[3][1];
this->m[1][2] = this->m[1][0] * rhs[0][2] + this->m[1][1] * rhs[1][2] + this->m[1][2] * rhs[2][2] + this->m[1][3] * rhs[3][2];
this->m[1][3] = this->m[1][0] * rhs[0][3] + this->m[1][1] * rhs[1][3] + this->m[1][2] * rhs[2][3] + this->m[1][3] * rhs[3][3];

this->m[2][0] = this->m[2][0] * rhs[0][0] + this->m[2][1] * rhs[1][0] + this->m[2][2] * rhs[2][0] + this->m[2][3] * rhs[3][0];
this->m[2][1] = this->m[2][0] * rhs[0][1] + this->m[2][1] * rhs[1][1] + this->m[2][2] * rhs[2][1] + this->m[2][3] * rhs[3][1];
this->m[2][2] = this->m[2][0] * rhs[0][2] + this->m[2][1] * rhs[1][2] + this->m[2][2] * rhs[2][2] + this->m[2][3] * rhs[3][2];
this->m[2][3] = this->m[2][0] * rhs[0][3] + this->m[2][1] * rhs[1][3] + this->m[2][2] * rhs[2][3] + this->m[2][3] * rhs[3][3];

this->m[3][0] = this->m[3][0] * rhs[0][0] + this->m[3][1] * rhs[1][0] + this->m[3][2] * rhs[2][0] + this->m[3][3] * rhs[3][0];
this->m[3][1] = this->m[3][0] * rhs[0][1] + this->m[3][1] * rhs[1][1] + this->m[3][2] * rhs[2][1] + this->m[3][3] * rhs[3][1];
this->m[3][2] = this->m[3][0] * rhs[0][2] + this->m[3][1] * rhs[1][2] + this->m[3][2] * rhs[2][2] + this->m[3][3] * rhs[3][2];
this->m[3][3] = this->m[3][0] * rhs[0][3] + this->m[3][1] * rhs[1][3] + this->m[3][2] * rhs[2][3] + this->m[3][3] * rhs[3][3];

我只是想确认它是否正确 - 当我将两个矩阵相乘在 C++ 中(投影 * 视图矩阵)并将结果矩阵提供给着色器时,我什么也得不到屏幕出现。

但是,如果我分别给出着色器投影和视图矩阵,并将它们相乘在 GLSL 中 - 那么一切都很好,结果符合预期。

那么矩阵乘法函数一定有问题?

【问题讨论】:

  • 不要在原地做乘法,它不会正常工作。
  • 查看Understanding 4x4 homogenous transform matrices 并在代码中查找matrix_mul(但是矩阵是以一维数组OpenGL 风格的方式编码的)。您还可以为此利用点积(在行和列上或在行和转置的行上......)正如@Sneftel 建议的那样,因为您覆盖了计算其他单元格所需的值......您可以缓冲实际行然而,而不是整个矩阵......

标签: c++ math matrix-multiplication


【解决方案1】:

不应该

this->m[0][1] = this->m[0][0] * rhs[0][1] + this->m[0][1] * rhs[1][1] + this->m[0][2] * rhs[2][1] + this->m[0][3] * rhs[3][1];

乘以 rhs[1][0] .. rhs[1][3]?当您逐步浏览 this 的行时,您不会逐步浏览 rhs 的列。

【讨论】:

  • 嘿,谢谢,是的.. 结束了 for 循环,代码更少,更可靠.. 现在工作得很好!干杯
猜你喜欢
  • 1970-01-01
  • 2015-07-26
  • 2013-07-21
  • 2011-02-17
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多