【问题标题】:Quaternion to Matrix using glm使用 glm 将四元数转换为矩阵
【发布时间】:2016-07-01 12:08:04
【问题描述】:

我正在尝试将 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;
}

当我运行程序时,它显示错误“错误:‘四元数’尚未声明”。

谁能帮我解决这个问题?

【问题讨论】:

  • quaternion::toMat4 必须是glm::quaternion::toMat4吗?

标签: c++ opengl glm-math


【解决方案1】:

添加包含:

#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

【讨论】:

  • C++14 中不允许嵌套命名空间定义,但问题中没有定义......无论如何,它们在 C++17 中是允许的。
  • 在cmets中提到过,但事后我应该在那里回应。
【解决方案2】:

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

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

哪个不需要需要#include &lt;glm/gtx/quaternion.hpp&gt;

【讨论】:

  • 在 Fedora-32 桌面 (Linux) 上确实需要上述包含。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 2012-08-16
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多