使用 glm 四元数到矩阵

Quaternion to Matrix using glm

我正在尝试将 glm 中的 quat 转换为 mat4。

我的代码是:

#include <iostream>
#include<glm/glm.hpp>
#include<glm/gtc/quaternion.hpp>
#include<glm/common.hpp>
using namespace std;


int main()
{
    glm::mat4 MyMatrix=glm::mat4();
    glm::quat myQuat;

    myQuat=glm::quat(0.707107,0.707107,0.00,0.000);
    glm::mat4 RotationMatrix = quaternion::toMat4(myQuat);

    for(int i=0;i<4;++i)
    {
        for(int j=0;j<4;++j)
        {
            cout<<RotationMatrix[i][j]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}

当我 运行 程序显示错误 "error: ‘quaternion’ has not been declared"。

谁能帮我解决这个问题?

添加包含:

#include <glm/gtx/quaternion.hpp>

并修复 toMat4 的命名空间:

glm::mat4 RotationMatrix = glm::toMat4(myQuat);

glm::toMat4()存在于gtx/quaternion.hpp文件中,you can see只有glm命名空间。


另请注意,自 C++14 起,嵌套名称空间(例如 glm::quaternion::toMat4)are not allowed.

除了meepzh的回答,还可以这样:

glm::mat4 RotationMatrix = glm::mat4_cast(myQuat);

不需要 #include <glm/gtx/quaternion.hpp>