关闭。这个问题需要debugging details .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
1年前关闭。
Improve this question
我最近一直在创建一个基本的 gui,我试图让用户能够旋转形状/图像/ Sprite 。但是,当我使用 glm::rotate 尝试更改模型矩阵时,我得到了一些意想不到的结果。变形后的形状改变了尺寸,旋转得太远了,而且还绕着一个看似随机的点旋转。
着色器代码:
#version 330 core
layout(location = 0) in vec2 pos;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
void main()
{
gl_Position = projection * view * model * vec4(pos.x, pos.y, 1.0f, 1.0f);
}
轮换代码:m_modelMatrix = glm::mat4(1.0f);
m_modelMatrix = glm::rotate(m_modelMatrix, 45.f, glm::vec3(0, 0, 1);
View 和投影设置glm::mat4 proj = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
proj = glm::perspective(glm::radians(53.f), 1.f, 0.1f, 100.f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
一切都是二维的。
最佳答案
欢迎来到堆栈溢出! :)
我假设您已经有了一个带有投影和 View 矩阵的设置,就像您在评论中指出的那样。它应该看起来像这样,
#version 330 core
layout(location = 0) in vec2 pos;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main()
{
gl_Position = projection * view * model * vec4(pos.x, pos.y, 1.0f, 1.0f);
}
投影矩阵应该是用glm::ortho( ... );
或者glm::perspective( ... );
并且应该已经创建了 View 矩阵glm::lookAt( ... );
如 https://glm.g-truc.net/0.9.2/api/a00245.html 中所述.如果需要进一步解释,我建议您查看 https://learnopengl.com/Getting-started/Coordinate-Systems作为引用。
我还假设问题源于以前的转换以及如何使用这种旋转来修改现有的模型矩阵。例如。仅当您指定了除单位矩阵以外的模型矩阵时,才会出现此问题。
创建模型矩阵时,必须确保使用正确的乘法顺序:
glm::mat4 translation = ... ;
glm::mat4 rotation = ... ;
glm::mat4 scale = ... ;
glm::mat4 model = translation * rotation * scale;
由于您将从右侧的 vector 乘以模型矩阵,因此此顺序可确保您不会发生失真。 vector 将首先缩放,然后旋转,最后平移。当您尝试旋转模型矩阵时,
glm::rotate(model, angle, axis);
您基本上从右侧乘以旋转矩阵并打破模型矩阵的乘法顺序。model * rotation_from_user_input
= translation * rotation * scale * rotation_from_user_input
您必须像这样更改模型矩阵的旋转rotation *= rotation_from_user_input;
然后更新模型矩阵本身以修复失真。model = translation * rotation * scale;
关于c++ - 旋转矩阵的问题(openGL/glm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62784696/