Why does my vector not rotate correctly OpenGL/GLM?

时间:2021-06-13 15:48:36

I am trying to learn how to do some transformations on 3d points in OpenGL. Using this cheat sheet I believe that I have the correct matrix to multiply to my vector which I want to rotate. However, when I multiply and print the new coord, I believe that it is incorrect. (Rotating 1,0,0 90deg cc should result in 0,1,0 correct?) Why is this not working?

我正在尝试学习如何在OpenGL中对3d点进行一些转换。使用这个备忘单我相信我有正确的矩阵乘以我想要旋转的矢量。但是,当我繁殖并打印新的coord时,我认为它是不正确的。 (旋转1,0,0 90deg cc会导致0,1,0正确吗?)为什么这不起作用?

My code:

glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f); 
glm::mat4 trans = {
    1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, cos(glm::radians(90.0f)), -sin(glm::radians(90.0f)), 0.0f,
    0.0f, sin(glm::radians(90.0f)), cos(glm::radians(90.0f)), 0,
    0.0f, 0.0f, 0.0f, 1.0f 
};
vec = trans * vec; //I should get 0.0, 1.0, 0.0 right?
std::cout << vec.x << ", " << vec.y << ", " << vec.z << std::endl;

The above prints 1.0, 0.0, 0.0 indicating that there was no change at all?

以上打印1.0,0.0,0.0表示根本没有变化?

I also tried using the rotate function in GLM to generate my matrix rather then manually specifying but I still did not get what I think should be correct (I got a different wrong answer).

我也尝试使用GLM中的旋转功能生成我的矩阵而不是手动指定,但我仍然没有得到我认为应该是正确的(我有一个不同的错误答案)。

glm::mat4 trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0));  //EDIT: my bad, should've been z axis not x, still not working

The above prints: -2.14..e+08, -2.14..e+08, -2.14..e+08

以上打印:-2.14..e + 08,-2.14..e + 08,-2.14..e + 08

(PS: I just took Geometry in the previous school year, my apologies if the math is incorrect. I have a basic understanding of matrices and matrix multiplication that I picked up today to learn OpenGL transformations but other then that I'm sort of a noob at this)

(PS:我上一学年刚学了几何,如果数学不正确,我很抱歉。我对矩阵和矩阵乘法有了基本的了解,我今天学习了它来学习OpenGL转换,但除此之外,我有点像noob在这)

2 个解决方案

#1


4  

In your code, you're rotating a unit vector on the x-axis around the x-axis and that doesn't change the vector (imagine rotating a pencil around itself, the direction doesn't change at all).

在你的代码中,你在x轴上围绕x轴旋转一个单位向量,并且不会改变向量(想象一下自己旋转一个铅笔,方向根本不会改变)。

To achieve what you previously wanted, you should rotate the vector around the z-axis using the matrix like this:

要实现您之前想要的效果,您应该使用矩阵围绕z轴旋转矢量,如下所示:

glm::mat4 trans = {
    cos(glm::radians(90.0f)), -sin(glm::radians(90.0f)), 0.0f, 0.0f,
    sin(glm::radians(90.0f)), cos(glm::radians(90.0f)), 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f
};

Besides that, glm::mat4 trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0)); isn't returning the desired result because trans need to be initialized before passing it to glm::rotate. Try writing it like this:

除此之外,glm :: mat4 trans = glm :: rotate(trans,90.0f,glm :: vec3(0.0,0.0,1.0));没有返回所需的结果,因为trans需要在传递给glm :: rotate之前进行初始化。试着像这样写:

glm::mat4 trans; // Default constructor called, trans is an identity matrix
trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));

Still, you might not get the expected vector (0.0f, 1.0f, 0.0f) due to precision loss when using glm::radians and cos/sen. In my test, I got the vector (-4.37113883e-008f, 1.0f, 0.0f) and -4.37113883e-008 is really -0.0000000437113883 which is a number very close to 0, the expected result.

但是,当使用glm :: radians和cos / sen时,由于精度损失,您可能无法得到预期的向量(0.0f,1.0f,0.0f)。在我的测试中,我得到了向量(-4.37113883e-008f,1.0f,0.0f)和-4.37113883e-008实际上是-0.0000000437113883,这是一个非常接近0的数字,预期结果。

#2


2  

The reason why your own rotation matrix is not changing the input is simple: your rotation only affects y and z coordinates and since those are zero the result is exactly the same as the input. X coordinate has a multiplier of 1 into the output x coordinate so that stays the same.

你自己的旋转矩阵没有改变输入的原因很简单:你的旋转只影响y和z坐标,因为它们为零,结果与输入完全相同。 X坐标在输出x坐标中的乘数为1,因此保持不变。

You can make the vector 1.0, 2.0, 3.0, 1.0 for example and then you will see changes.

例如,您可以制作矢量1.0,2.0,3.0,1.0,然后您将看到更改。

As for the glm version, can't say why it would give strange result, I have never had issues with them but haven't used much.

至于glm版本,不能说为什么它会给出奇怪的结果,我从来没有遇到过它们的问题但是没用多少。

#1


4  

In your code, you're rotating a unit vector on the x-axis around the x-axis and that doesn't change the vector (imagine rotating a pencil around itself, the direction doesn't change at all).

在你的代码中,你在x轴上围绕x轴旋转一个单位向量,并且不会改变向量(想象一下自己旋转一个铅笔,方向根本不会改变)。

To achieve what you previously wanted, you should rotate the vector around the z-axis using the matrix like this:

要实现您之前想要的效果,您应该使用矩阵围绕z轴旋转矢量,如下所示:

glm::mat4 trans = {
    cos(glm::radians(90.0f)), -sin(glm::radians(90.0f)), 0.0f, 0.0f,
    sin(glm::radians(90.0f)), cos(glm::radians(90.0f)), 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f
};

Besides that, glm::mat4 trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0)); isn't returning the desired result because trans need to be initialized before passing it to glm::rotate. Try writing it like this:

除此之外,glm :: mat4 trans = glm :: rotate(trans,90.0f,glm :: vec3(0.0,0.0,1.0));没有返回所需的结果,因为trans需要在传递给glm :: rotate之前进行初始化。试着像这样写:

glm::mat4 trans; // Default constructor called, trans is an identity matrix
trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));

Still, you might not get the expected vector (0.0f, 1.0f, 0.0f) due to precision loss when using glm::radians and cos/sen. In my test, I got the vector (-4.37113883e-008f, 1.0f, 0.0f) and -4.37113883e-008 is really -0.0000000437113883 which is a number very close to 0, the expected result.

但是,当使用glm :: radians和cos / sen时,由于精度损失,您可能无法得到预期的向量(0.0f,1.0f,0.0f)。在我的测试中,我得到了向量(-4.37113883e-008f,1.0f,0.0f)和-4.37113883e-008实际上是-0.0000000437113883,这是一个非常接近0的数字,预期结果。

#2


2  

The reason why your own rotation matrix is not changing the input is simple: your rotation only affects y and z coordinates and since those are zero the result is exactly the same as the input. X coordinate has a multiplier of 1 into the output x coordinate so that stays the same.

你自己的旋转矩阵没有改变输入的原因很简单:你的旋转只影响y和z坐标,因为它们为零,结果与输入完全相同。 X坐标在输出x坐标中的乘数为1,因此保持不变。

You can make the vector 1.0, 2.0, 3.0, 1.0 for example and then you will see changes.

例如,您可以制作矢量1.0,2.0,3.0,1.0,然后您将看到更改。

As for the glm version, can't say why it would give strange result, I have never had issues with them but haven't used much.

至于glm版本,不能说为什么它会给出奇怪的结果,我从来没有遇到过它们的问题但是没用多少。