OpenGL矩阵乘法C++
Posted
技术标签:
【中文标题】OpenGL矩阵乘法C++【英文标题】:OpenGL Matrix Multiplication C++ 【发布时间】:2015-03-23 12:57:57 【问题描述】:所以我试图将旋转和平移矩阵相乘,但我无法弄清楚出了什么问题。
如果在程序中我将一个平移矩阵乘以一个旋转矩阵,然后将该矩阵作为一个统一的矩阵发送到我的着色器程序,我最终将对象变为 2D,然后在它旋转时再次变为 3D [https://a.pomf.se/xvvrsg.mp4](对象在右边)。
shader.setUniformMat4("model_matrix", Matrix4::translation(Vector3(10.0f, 0.0f, 0.0f)) * Matrix4::rotation(rotation, Vector3(0.0f, 1.0f, 0.0f)));
(顶点着色器)
#version 330 core
layout (location = 0) in vec4 in_position;
layout (location = 1) in vec4 in_normal;
uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 model_matrix = mat4(1.0);
out vec4 pos;
out vec4 normal;
void main()
pos = pr_matrix * vw_matrix * model_matrix * in_position;
normal = in_normal;
gl_Position = pos;
但是,如果我将单独的平移和旋转矩阵作为单独的制服发送,然后在着色器中将它们相乘以创建我的模型矩阵,它会按预期工作 [https://a.pomf.se/jyxpnb.mp4](右侧的对象)。
shader.setUniformMat4("translation_matrix", Matrix4::translation(Vector3(10.0f, 0.0f, 0.0f)));
shader.setUniformMat4("rotation_matrix", Matrix4::rotation(rotation, Vector3(0.0f, 1.0f, 0.0f)));
shader.setUniformMat4("scale_matrix", Matrix4::identity());
(顶点着色器)
#version 330 core
layout (location = 0) in vec4 in_position;
layout (location = 1) in vec4 in_normal;
uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 translation_matrix = mat4(1.0);
uniform mat4 rotation_matrix = mat4(1.0);
uniform mat4 scale_matrix = mat4(1.0);
out vec4 pos;
out vec4 normal;
void main()
mat4 model_matrix = translation_matrix * rotation_matrix * scale_matrix;
pos = pr_matrix * vw_matrix * model_matrix * in_position;
normal = in_normal;
gl_Position = pos;
这让我相信我的矩阵乘法一定有错误,这就是我目前的做法:
Matrix4 &Matrix4::multiply(const Matrix4 &other)
elements[0] = elements[0] * other.elements[0] + elements[4] * other.elements[1] + elements[8] * other.elements[2] + elements[12] * other.elements[3];
elements[1] = elements[1] * other.elements[0] + elements[5] * other.elements[1] + elements[9] * other.elements[2] + elements[13] * other.elements[3];
elements[2] = elements[2] * other.elements[0] + elements[6] * other.elements[1] + elements[10] * other.elements[2] + elements[14] * other.elements[3];
elements[3] = elements[3] * other.elements[0] + elements[7] * other.elements[1] + elements[11] * other.elements[2] + elements[15] * other.elements[3];
elements[4] = elements[0] * other.elements[4] + elements[4] * other.elements[5] + elements[8] * other.elements[6] + elements[12] * other.elements[7];
elements[5] = elements[1] * other.elements[4] + elements[5] * other.elements[5] + elements[9] * other.elements[6] + elements[13] * other.elements[7];
elements[6] = elements[2] * other.elements[4] + elements[6] * other.elements[5] + elements[10] * other.elements[6] + elements[14] * other.elements[7];
elements[7] = elements[3] * other.elements[4] + elements[7] * other.elements[5] + elements[11] * other.elements[6] + elements[15] * other.elements[7];
elements[8] = elements[0] * other.elements[8] + elements[4] * other.elements[9] + elements[8] * other.elements[10] + elements[12] * other.elements[11];
elements[9] = elements[1] * other.elements[8] + elements[5] * other.elements[9] + elements[9] * other.elements[10] + elements[13] * other.elements[11];
elements[10] = elements[2] * other.elements[8] + elements[6] * other.elements[9] + elements[10] * other.elements[10] + elements[14] * other.elements[11];
elements[11] = elements[3] * other.elements[8] + elements[7] * other.elements[9] + elements[11] * other.elements[10] + elements[15] * other.elements[11];
elements[12] = elements[0] * other.elements[12] + elements[4] * other.elements[13] + elements[8] * other.elements[14] + elements[12] * other.elements[15];
elements[13] = elements[1] * other.elements[12] + elements[5] * other.elements[13] + elements[9] * other.elements[14] + elements[13] * other.elements[15];
elements[14] = elements[2] * other.elements[12] + elements[6] * other.elements[13] + elements[10] * other.elements[14] + elements[14] * other.elements[15];
elements[15] = elements[3] * other.elements[12] + elements[7] * other.elements[13] + elements[11] * other.elements[14] + elements[15] * other.elements[15];
return *this;
我确实有一个嵌套循环来执行此操作,但我最终在尝试找出此问题的答案时将其全部写出来。请记住,矩阵在主要列中,不会被 OpenGL 转置
旋转和平移矩阵如下,但我认为它们没有任何问题:
Matrix4 Matrix4::translation(const Vector3 &translation)
Matrix4 result(1.0f);
result.elements[0 + 3 * 4] = translation.x;
result.elements[1 + 3 * 4] = translation.y;
result.elements[2 + 3 * 4] = translation.z;
return result;
Matrix4 Matrix4::rotation(float angle, const Vector3 &axis)
Matrix4 result(1.0f);
float r = toRadians(angle);
float c = (float)cos(r);
float s = (float)sin(r);
float cFlip = 1.0f - c;
result.elements[0 + 0 * 4] = axis.x * cFlip + c;
result.elements[1 + 0 * 4] = axis.y * axis.x * cFlip + axis.z * s;
result.elements[2 + 0 * 4] = axis.x * axis.z * cFlip - axis.y * s;
result.elements[0 + 1 * 4] = axis.x * axis.y * cFlip - axis.z * s;
result.elements[1 + 1 * 4] = axis.y * cFlip + c;
result.elements[2 + 1 * 4] = axis.y * axis.z * cFlip + axis.x * s;
result.elements[0 + 2 * 4] = axis.x * axis.y * cFlip + axis.y * s;
result.elements[1 + 2 * 4] = axis.y * axis.z * cFlip - axis.x * s;
result.elements[2 + 2 * 4] = axis.z * cFlip + c;
return result;
任何关于这里的问题可能是什么或如何解决它的想法将不胜感激:^)
【问题讨论】:
您向我们展示了函数multiply
,但使用operator*
来进行实际的乘法运算,这只是输入错误吗?
运算符重载,我的错误是没有添加它:)
【参考方案1】:
在你的乘法函数中,你写道:
elements[0] = elements[0] * other.elements[0] ...
...
请注意,element[0]
有时会实现其内容,然后您会这样做:
elements[8] = elements[0] * other.elements[8] ...
它使用新值而不是原始值。我猜,你想在做这个乘法之前复制你的原始矩阵
【讨论】:
是的,这确实是问题所在,我希望我能将多个答案标记为正确,谢谢!【参考方案2】:opsss !我刚看到!!!
在你的multiply
中,你的输出矩阵是第一个输入矩阵,所以最新的操作是用乘法矩阵的系数计算的! :
elements[0] = elements[0] * .....
....
elements[4] = elements[0] * ..... /* here element[ 0 ] is the top left
element of the multiplied matix */
此外,operator*
不应该修改(也不返回)他的操作数之一,operator*=
就是为了这个
【讨论】:
对不起,我不太明白,您能否详细解释一下,我的操作员是这样的,但是pastebin,我是不是用了不正确的操作员?跨度> 没关系,我明白你指出了什么,这已经解决了我的问题,谢谢! 好吧,我必须承认,另一个答案更清楚,酷你提高了它以上是关于OpenGL矩阵乘法C++的主要内容,如果未能解决你的问题,请参考以下文章