使用 glm 的旋转功能时预期的旋转方向是什么?
What is the expected rotation direction when using glm's rotate function?
我正在编写一个静态函数,它使用 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 的右手坐标系遵守右手定则?我是不是遗漏了什么,或者我只是感到困惑?
您正在使用矩阵转置,并且由于旋转矩阵是正交矩阵,这具有使用这些矩阵的逆矩阵的效果:R^-1 = R^T
。
glm 模仿具有 mat4 * vec4
乘法顺序的经典 GL 约定,其中 vec4
是列向量。
当您编写 vec4 * mat4
时,vec4
被解释为行向量,并且由于 (A*B)^T = B^T * A^T
,您得到与 transpose(mat4) * vec4
.
相同的结果
我正在编写一个静态函数,它使用 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 的右手坐标系遵守右手定则?我是不是遗漏了什么,或者我只是感到困惑?
您正在使用矩阵转置,并且由于旋转矩阵是正交矩阵,这具有使用这些矩阵的逆矩阵的效果:R^-1 = R^T
。
glm 模仿具有 mat4 * vec4
乘法顺序的经典 GL 约定,其中 vec4
是列向量。
当您编写 vec4 * mat4
时,vec4
被解释为行向量,并且由于 (A*B)^T = B^T * A^T
,您得到与 transpose(mat4) * vec4
.