GLM 中的旋转计算
rotation calculation in GLM
GLM 旋转的源代码是这样完成的:
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER mat<4, 4, T, Q> rotate(mat<4, 4, T, Q> const& m, T angle, vec<3, T, Q> const& v)
{
T const a = angle;
T const c = cos(a);
T const s = sin(a);
vec<3, T, Q> axis(normalize(v));
vec<3, T, Q> temp((T(1) - c) * axis);
mat<4, 4, T, Q> Rotate;
Rotate[0][0] = c + temp[0] * axis[0];
Rotate[0][1] = temp[0] * axis[1] + s * axis[2];
Rotate[0][2] = temp[0] * axis[2] - s * axis[1];
Rotate[1][0] = temp[1] * axis[0] - s * axis[2];
Rotate[1][1] = c + temp[1] * axis[1];
Rotate[1][2] = temp[1] * axis[2] + s * axis[0];
Rotate[2][0] = temp[2] * axis[0] + s * axis[1];
Rotate[2][1] = temp[2] * axis[1] - s * axis[0];
Rotate[2][2] = c + temp[2] * axis[2];
mat<4, 4, T, Q> Result;
Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
Result[3] = m[3];
return Result;
}
有人知道这里是如何计算旋转的吗?具体使用了哪种技术?
由于不使用绕轴心点的旋转,因此不需要转换,但计算绕任意轴旋转的一般形式如下所示:
我不知道是不是上面的情况。特别是我没有从定义 temp((T(1)-c)*axis)
的地方得到,这是我在线性代数中从未做过的事情。
什么glm::rotate
actually does is to set up a rotation matrix and multiply the input matrix by the roation. It computes m*r
in the meaning of GLSL Vector and Matrix Operations.
它的运作方式与针对 glm::translate
in one of your previous questions: .
所讨论的方式相同
输入参数angle
(旋转角度)和v
(旋转轴),定义一个3x3的旋转矩阵,如维基百科文章Rotation matrix from axis and angle:
(这个公式的数学解释是 Mathematics 的问题)
c = cos(angle); s = sin(angle)
x = v.x; y = v.y; z = v.z
| x*x*(1-c)+c x*y*(1-c)-z*s x*z*(1-c)+y*s |
Rotate = | y*x*(1-c)+z*s y*y*(1-c)+c y*z*(1-c)-x*s |
| z*x*(1-c)-y*s z*y*(1-c)+x*s z*z*(1-c)+c |
此矩阵隐式扩展为 4x4 矩阵。最后将输入矩阵 (m
) 乘以 Roatate
并分配给 Result
:
Result = m * Roatate
GLM 旋转的源代码是这样完成的:
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER mat<4, 4, T, Q> rotate(mat<4, 4, T, Q> const& m, T angle, vec<3, T, Q> const& v)
{
T const a = angle;
T const c = cos(a);
T const s = sin(a);
vec<3, T, Q> axis(normalize(v));
vec<3, T, Q> temp((T(1) - c) * axis);
mat<4, 4, T, Q> Rotate;
Rotate[0][0] = c + temp[0] * axis[0];
Rotate[0][1] = temp[0] * axis[1] + s * axis[2];
Rotate[0][2] = temp[0] * axis[2] - s * axis[1];
Rotate[1][0] = temp[1] * axis[0] - s * axis[2];
Rotate[1][1] = c + temp[1] * axis[1];
Rotate[1][2] = temp[1] * axis[2] + s * axis[0];
Rotate[2][0] = temp[2] * axis[0] + s * axis[1];
Rotate[2][1] = temp[2] * axis[1] - s * axis[0];
Rotate[2][2] = c + temp[2] * axis[2];
mat<4, 4, T, Q> Result;
Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
Result[3] = m[3];
return Result;
}
有人知道这里是如何计算旋转的吗?具体使用了哪种技术?
由于不使用绕轴心点的旋转,因此不需要转换,但计算绕任意轴旋转的一般形式如下所示:
我不知道是不是上面的情况。特别是我没有从定义 temp((T(1)-c)*axis)
的地方得到,这是我在线性代数中从未做过的事情。
什么glm::rotate
actually does is to set up a rotation matrix and multiply the input matrix by the roation. It computes m*r
in the meaning of GLSL Vector and Matrix Operations.
它的运作方式与针对 glm::translate
in one of your previous questions:
输入参数angle
(旋转角度)和v
(旋转轴),定义一个3x3的旋转矩阵,如维基百科文章Rotation matrix from axis and angle:
(这个公式的数学解释是 Mathematics 的问题)
c = cos(angle); s = sin(angle)
x = v.x; y = v.y; z = v.z
| x*x*(1-c)+c x*y*(1-c)-z*s x*z*(1-c)+y*s |
Rotate = | y*x*(1-c)+z*s y*y*(1-c)+c y*z*(1-c)-x*s |
| z*x*(1-c)-y*s z*y*(1-c)+x*s z*z*(1-c)+c |
此矩阵隐式扩展为 4x4 矩阵。最后将输入矩阵 (m
) 乘以 Roatate
并分配给 Result
:
Result = m * Roatate