使用glm的四元数到矩阵

Vis*_*ali 8 c++ opengl glm-math

我试图将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;
}
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,它显示错误"错误:'四元数'尚未声明".

谁能帮我这个?

mee*_*pzh 10

添加包含:

#include <glm/gtx/quaternion.hpp>
Run Code Online (Sandbox Code Playgroud)

并修复名称空间toMat4:

glm::mat4 RotationMatrix = glm::toMat4(myQuat);
Run Code Online (Sandbox Code Playgroud)

glm::toMat4()存在于gtx/quaternion.hpp文件中,您只能看到glm命名空间.


另外作为旁注,从C++ 14开始,不允许嵌套命名空间(例如glm :: quaternion :: toMat4).

  • C++ 14中不允许使用嵌套的命名空间*定义*,但问题中没有定义...无论如何,它们在C++ 17中是允许的. (3认同)

mch*_*son 7

除了meepzh的答案外,还可以这样完成:

glm::mat4 RotationMatrix = glm::mat4_cast(myQuat);
Run Code Online (Sandbox Code Playgroud)

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