Transform using the glm library

// Rotation

/* Matrix to quaternion */
    glm::quat q_from_matrix3x3{ glm::mat3{1} }; // cast operator will handle this

    glm::quat q_from_matrix4x4{ glm::mat4{1} }; // cast operator will handle this

/* Matrix to Euler rotation */
    float x, y, z; // radians
    glm::extractEulerAngleXYZ(glm::mat4{ 1 }, x, y, z);

    glm::extractEulerAngleZYX(glm::mat4{ 1 }, z, y, x); // pay attention to the parameter order, z->y->x

/* Quaternion to matrix */
    glm::mat3 matrix3x3_from_quaternion{ glm::quat{1.0f, 0, 0, 0} }; // cast operator will handle this

    glm::mat4 matrix4x4_from_quaternion{ glm::quat{1.0f, 0, 0, 0} }; // cast operator will handle this

/* Quaternion to Euler rotation */
    glm::vec3 euler_angles_zyx = glm::eulerAngles(glm::quat{ 1.0f, 0, 0, 0 }); // rotation matrices do not commute in multiplication, and the rotation around x->y->z order means z->y->x matrices order

/* Euler rotation to matrix */
    glm::mat3 matrix3x3_from_euler = glm::eulerAngleXYZ(0.0f, 0.0f, 0.0f); // rotation order is z axis -> y axis -> x axis, the equal matirces multiplication order is x->y->z

    glm::mat3 matrix4x4_from_euler = glm::eulerAngleZYX(0.0f, 0.0f, 0.0f); // rotation order is x axis -> y axis -> z axis, the equal matirces multiplication order is z->y->x

/* Euler rotation to quaternion */
    float euler_x, euler_y, euler_z; // radians
    euler_x = euler_y = euler_z = 0.0f;
    glm::quat quaternion_from_euler{ glm::vec3{euler_x, euler_y, euler_z} }; // the original rotation order is x axis ->  y axis -> z axis, matrices multip
原文地址:https://www.cnblogs.com/wangpei0522/p/12894752.html