使用glm的旋转功能时,预期的旋转方向是多少?

问题描述 投票:1回答:1

我正在编写一个静态函数,它使用GLM的rotate()函数围绕任意轴旋转矢量。

我写了一个简单的测试来检查我的工作,我发现旋转的方向与我预期的相反。

我以pi / 4为步长围绕X轴(1,0,0)旋转单位矢量(0,0,1)。我预计,由于OpenGL(和GLM?)使用右手坐标系,旋转将围绕X轴逆时针方向发生。相反,它们是顺时针方向发生的。

vec3& RotateVector(vec3& targetVector, float const& radians, vec3 const &axis)
{
    mat4 rotation = glm::rotate(mat4(1.0f), radians, axis);

    targetVector = (vec4(targetVector, 0.0f) * rotation).xyz();
    return targetVector;
}


vec3 test(0, 0, 1);
cout << "test initial vals: " << test.x << " " << test.y << " " << test.z << "\n";

RotateVector(test, 3.14f / 4.0f, vec3(1, 0, 0) );
cout << "Rotated test: " << test.x << " " << test.y << " " << test.z << "\n";

RotateVector(test, 3.14 /4.0f, vec3(1, 0, 0));
cout << "Rotated test: " << test.x << " " << test.y << " " << test.z << "\n";

RotateVector(test, 3.14 / 4.0f, vec3(1, 0, 0));
cout << "Rotated test: " << test.x << " " << test.y << " " << test.z << "\n";

RotateVector(test, 3.14 / 4.0f, vec3(1, 0, 0));
cout << "Rotated test: " << test.x << " " << test.y << " " << test.z << "\n";

当我运行上面的代码时,我得到以下输出:

test initial vals: 0 0 1
Rotated test: 0 0.706825 0.707388
Rotated test: 0 1 0.000796229
Rotated test: 0 0.707951 -0.706262
Rotated test: 0 0.00159246 -0.999999

输出显示旋转绕X轴顺时针移动。

这是为什么?我期望OpenGL的右手坐标系统会遵守右手规则吗?我错过了什么,还是我只是困惑?

c++ opengl rotation glm-math
1个回答
2
投票

您正在使用转置的矩阵,并且由于旋转矩阵是正交矩阵,因此具有使用这些矩阵的逆的效果:R^-1 = R^T

glm使用mat4 * vec4乘法顺序模拟经典GL约定,其中vec4是列向量。当你写vec4 * mat4时,vec4被解释为行向量,并且从(A*B)^T = B^T * A^T开始,你得到与transpose(mat4) * vec4相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.