使用glm的四元数到矩阵

Quaternion to Matrix using glm

本文关键字:四元数 glm 使用      更新时间:2023-10-16

我正在尝试将quat在glm中转换为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被声明"。

有谁能帮我一下吗?

添加include:

#include <glm/gtx/quaternion.hpp>

修复toMat4的命名空间:

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

glm::toMat4()存在于"gtx/quaternion.hpp"文件中,可以看到该文件中只有"glm"命名空间。


还有一个旁注,从c++ 14开始,不允许嵌套命名空间(例如glm::quaternion::toMat4)。

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

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

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