GLM :: Rotate似乎引起错误的旋转? [英] GLM::Rotate seems to cause wrong rotation?

查看:607
本文介绍了GLM :: Rotate似乎引起错误的旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我正在学习OpenGL,并开始学习有关变换矩阵的知识.下面是我当前的代码:

Simply put, I'm learning OpenGL and am starting to learn about transform matrices. Below is my current code:

glm::vec4 myPosition(1.0f, 1.0f, 1.0f, 1.0f);

glm::vec3 rotationAxis(0.0f, 1.0f, 0.0f);

glm::mat4 scalar = glm::scale(glm::vec3(1.0f, 1.0f, 1.0f));
glm::mat4 rotator = glm::rotate(360.0f, rotationAxis);
glm::mat4 translator = glm::translate(glm::vec3(1.0f, 1.0f, 1.0f));

glm::mat4 transform = translator * rotator * scalar;

myPosition = transform * myPosition;

据我所知,我以正确的顺序执行此操作:缩放->旋转->翻译.因此,我将缩放比例设置为不执行任何操作,因为我实际上不希望它在任何地方缩放(为简单起见).

As far as I can tell, I'm doing this in the correct order: Scale -> Rotate -> Translate. So, I have the scale set to do nothing because I don't actually want it to scale anywhere (for simplicity sake).

接下来,我将Y轴的旋转设置为360.0f(如果我输入错了,请更正).这应该回到原始点,至少我是想绕奇异轴旋转360度.

Next, I set rotate to 360.0f on (correct me if I'm wrong) the Y axis. This should return to the original point, at least that's what I would think from a 360 degree rotation around a singular axis.

然后,我将其设置为在每个方向上平移1个单位,以确保其移动.

Then, I set it to translate 1 unit in every direction to make sure it moves.

完成所有操作后,我已注释掉旋转器线,即使更改了比例尺,它也很棒.但是,每当我添加旋转线时,最终位置不是正常的360度旋转吗?

After finishing this all, I have commented out the rotator line, and it works fantastic, even if I change the scale. However, whenever I add in the rotate line the final position is not a normal 360 degree rotation?

我已经将程序配置为在变换之前和之后都输出位置矢量.

I have configured my program to output the position vector both before transforms and after.

之前的位置是(1、1、1)

The before position is (1, 1, 1)

后位置为(1.67522,2,-0.242607)

The after position is (1.67522, 2, -0.242607).

实际上,我整天都在努力寻找错误,因此,如果有人可以帮助我找到我在做错的事情,将不胜感激!

I have been struggling to find my error, literally all day so if anyone can help me find what I'm doing wrong, it would be greatly appreciated!!

推荐答案

根据 http://glm.g-truc.net/0.9.7/api/a00236.html (适用于当前最新发布的版本),glm :: rotate(..)可以接收以度表示的角度.

According to the documentation at http://glm.g-truc.net/0.9.7/api/a00236.html (for the latest released version right now), glm::rotate(..) takes in an angle expressed in degrees.

但是,更改旋转矩阵线

glm::mat4 rotator = glm::rotate(360.0f, rotationAxis);

glm::mat4 rotator = glm::rotate(3.141592f * 2.0f, rotationAxis);

只有2 * PI可以解决此问题.

which is just 2*PI fixes this.

这意味着角度应该以弧度而不是度为单位.在我的机器上使用GLM 0.9.7.1-1进行了测试.这要么是文档中的错误,要么是GLM代码本身中的错误.

This means that the angle should be in radians rather than in degrees. Tested on my machine with GLM 0.9.7.1-1. This is either a mistake in the documentation or in the GLM code itself.

根据我对GLM的经验(前一段时间,可能是更早的版本),这些类型的函数默认情况下应成一个角度,而#define GLM_FORCE_RADIANS宏就是使它们以弧度计算的原因.作者可能将其设置为默认行为,却忘记了更新文档.

From my experience with GLM (some time ago, might've been an earlier version) these kinds of functions should take a degrees angle by default, and the #define GLM_FORCE_RADIANS macro is what makes them calculate in radians. It is possible that the author made this the default behaviour and forgot to update the docs.

另一方面,您可能不应该使用scalar作为glm :: mat4值的名称,因为数学中的标量只是一个实数而不是矩阵:

On a side note, you should probably not be using scalar as the name for a glm::mat4 value, since a scalar in mathematics is just a single real number rather than a matrix: https://en.wikipedia.org/wiki/Scalar_%28mathematics%29

这篇关于GLM :: Rotate似乎引起错误的旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆