我是否使用矩阵正确旋转模型?

发布于 2025-02-08 17:13:52 字数 1179 浏览 1 评论 0原文

试图旋转基本立方体时,我一直在变得出乎意料的行为。知道翻译多维数据集在y和z方向上正常工作可能会有所帮助。但是,沿X轴翻译是向后的(我仅否定X以获得适当的结果),我无法弄清楚原因。

此外,旋转立方体是一团糟。没有任何形式的变换,立方体出现正确。一旦我添加旋转转换,直到我将x,y,z旋转值从0更改为0(将所有值都放回0使其再次消失)之前,该立方体不会显示。一旦出现,立方体就不会绕哪个x,y,z平面旋转,除非我更改两个或多个坐标。它在旋转时也围绕其起源摇摆。

以下是我认为数学不正确的代码段。

/* Here's how I setup the matrices for a mvp matrix*/

proj = glm::perspective(glm::radians(90.0f), (960.0f / 540.0f), 0.1f, 400.0f);
view = glm::lookAt(glm::vec3(0, 0, -200), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
glm::mat4 model = glm::mat4(1.0f);


/* Here's how I transform the model matrix, note 
   translating works properly once the cube is visible*/

model = glm::translate(model, glm::vec3(-translation[0], translation[1], translation[2])); //negative x value
model = glm::rotate(model, 30.0f, rotation);
glm::mat4 mvp = proj * view * model;
shader->Bind();
shader->SetUniformMat4f("MVP", mvp);
renderer.Draw(*c_VAO, *c_EBO, *shader);


/* Here's how I use these values in my vertex shader */

layout(location = 0) in vec4 position;
... 
uniform mat4 MVP;
...
void main()
{
    gl_Position = u_MVP * position;
    ....
};

我已经检查了翻译和旋转矢量值,它们的预期是,但我仍然疯狂地试图找出这个问题。

I have been getting unexpected behavior while trying to rotate a basic cube. It may be helpful to know that translating the cube works correctly in the y and z direction. However, translating along the x axis is backwards(I negate only x for proper results) which I haven't been able to figure out why.

Furthermore, rotating the cube has been a mess. Without any sort of transform the cube appears correctly. Once I add a rotation transformation the cube is not displayed until I change one of the x,y,z rotation values from 0(Putting all values back to 0 makes it disappear again). Once it appears the cube won't rotate around whichever x,y,z plane I first changed unless I change two or more of the coordinates. It also wobbles around its origin when rotating.

Below is a snippets of my code I believe has incorrect math.

/* Here's how I setup the matrices for a mvp matrix*/

proj = glm::perspective(glm::radians(90.0f), (960.0f / 540.0f), 0.1f, 400.0f);
view = glm::lookAt(glm::vec3(0, 0, -200), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
glm::mat4 model = glm::mat4(1.0f);


/* Here's how I transform the model matrix, note 
   translating works properly once the cube is visible*/

model = glm::translate(model, glm::vec3(-translation[0], translation[1], translation[2])); //negative x value
model = glm::rotate(model, 30.0f, rotation);
glm::mat4 mvp = proj * view * model;
shader->Bind();
shader->SetUniformMat4f("MVP", mvp);
renderer.Draw(*c_VAO, *c_EBO, *shader);


/* Here's how I use these values in my vertex shader */

layout(location = 0) in vec4 position;
... 
uniform mat4 MVP;
...
void main()
{
    gl_Position = u_MVP * position;
    ....
};

I've checked both the translation and rotation vectors values and they are as expected but I am still going mad trying to figure out this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2025-02-15 17:13:53

是弧度。使用将表格度转换为弧度:

model = glm ::旋转(型号,30.0f,旋转);

model = glm::rotate(model, glm::radians(30.0f), rotation);

The unit of the angle of glm::rotate is radians. Use glm::radians to convert form degrees to radians:

model = glm::rotate(model, 30.0f, rotation);

model = glm::rotate(model, glm::radians(30.0f), rotation);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文