GLM中的旋转计算

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

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)的角度出发,这是我在线性代数中从未做过的事情。

c++ opengl transformation glm-math
1个回答
0
投票

glm::rotate实际要做的是建立一个旋转矩阵,然后将输入矩阵乘以旋转。它以glm::rotate的含义计算m*r。它的运行方式与您在先前的问题之一GLSL Vector and Matrix Operations中针对glm::translate所讨论的方式相同。

[输入参数glm::translate(旋转角度)和How does GLM handle translation(旋转轴)定义了3x3旋转矩阵,如维基百科文章angle中所述:(对此公式的数学解释可能是v的问题)

Rotation matrix from axis and angle

此矩阵被隐式扩展为4x4矩阵。最后,将输入矩阵(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 | 并分配给m

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