gpt4 book ai didi

c++ - OpenGL 矩阵乘法 C++

转载 作者:行者123 更新时间:2023-11-28 06:21:40 25 4
gpt4 key购买 nike

所以我正在尝试将旋转和平移矩阵相乘,但我不太清楚出了什么问题。

如果在程序中我将一个平移矩阵乘以一个旋转矩阵,然后将该矩阵作为统一发送到我的着色器程序,我最终会在对象旋转时变成 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;
}

我确实有一个嵌套循环来执行此操作,但我最终在尝试找出该问题的答案时将其全部写了出来。请记住,矩阵位于 major 列中,不会被 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;
}

任何关于这里的问题可能是什么或如何解决它的想法将不胜感激:^)

最佳答案

在你的乘法函数中,你写道:

elements[0] = elements[0]  * other.elements[0] ...
...

请注意 element[0] 会立即实现其内容,然后您会执行以下操作:

elements[8] = elements[0]  * other.elements[8] ...

使用新值而不是原始值。我想,您想在执行此乘法之前复制原始矩阵

关于c++ - OpenGL 矩阵乘法 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com