【发布时间】:2019-08-14 14:42:36
【问题描述】:
我正在编写一个静态函数,它使用 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