c++ - GLM 中的旋转计算

标签 c++ opengl transformation glm-math

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;
    }  

有人知道这里如何计算旋转的任何解释吗?特别使用哪种技术?

由于不使用围绕枢轴点的旋转,因此不需要平移,但计算围绕任意轴的旋转的一般形式如下:

enter image description here

我不知道是不是上面的情况。特别是我不知道 temp((T(1)-c)*axis) 的定义点,这是我在线性代数中从未做过的事情。

最佳答案

什么glm::rotate实际上就是建立一个旋转矩阵并将输入矩阵乘以旋转。它以 GLSL Vector and Matrix Operations 的含义计算 m*r .
它的操作方式与 glm::translate 中讨论的方式相同。在您之前的问题之一中:How does GLM handle translation .

输入参数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

关于c++ - GLM 中的旋转计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59908345/

相关文章:

C++ 流与 C 风格的 IO?

c++ - OpenGL OBJ 加载器错误 : EXC_BAD_ACCESS code = 1

c++ - 边界框视锥体渲染 - 距离渲染 - OpenGL

R - 数据框切换行和列

python - 将按 pandas 数据框(多个但不是所有列)分组的数据从长转换为宽

JSON 到 JSON 的转换(最好在 Apache Camel 中)

c++ - IEEE 754-2008(密集十进制)的可用实现

c++ - 使用线程处理信号和槽

c++ - 为什么我的 C++ 程序在执行 tcmalloc heap-checker 或 heap-profile 时使用大量内存

c++ - 渲染到 FBO 不起作用,gDEBugger 另有说法