c++ - 如何将欧拉角转换为前、上、右 vector

标签 c++ opengl 3d glm-math

我需要一个函数,它给定 Yaw、Pitch 和 Roll,可以在“世界坐标”中生成 Front(或 Looking At)、Right 和 Up vector 。

在我的特定世界空间中,从原点 (0,0,0) 开始,X 向左为正,Z 为正,远离观察者/原点,Y 为正向上。

例如,给定...(以度为单位的角度)

  • 偏航=0,俯仰=0,横滚=0, 预期的输出是:

    • 前面 = (0.0,0.0,1.0)
    • 右 = (-1.0,0.0,0.0)
    • 向上 = (0.0,1.0,0.0)
  • 偏航=90,俯仰=0,横滚=0, 预期的输出是:

    • 前面 = (1.0,0.0,0.0)
    • 右 = (0,0,0.0,1.0)
    • 向上 = (0.0,1.0,0.0)
  • 偏航=0,俯仰=90,横滚=0, 预期的输出是:

    • 前面 = (0.0,1.0,0.0)
    • 右 = (-1.0,0.0,0.0)
    • 向上 = (0.0,0.0,-1.0)
  • 偏航=0,俯仰=0,横滚=90, 预期的输出是:

    • 前面 = (0.0,0.0,1.0)
    • 右 = (0.0,1.0,0.0)
    • 向上 = (1.0,0.0,0.0)

我使用的语言是 C++,如果最有意义的话,我很乐意使用 glm 来解决这个问题。如果我可以通过四元数到达那里,我也可以使用该解决方案,因为我发现其他教程描述了如何从欧拉角获得四元数。

最佳答案

这是一个完整的工作示例。它不是很像 C++。您可能想要使用真正的矩阵类,但出于演示目的应该没问题。您的问题中不清楚的一件事是轮换顺序,但这很容易更改。

#include <iostream>
#include <cmath>
#include <cstdlib>

typedef float Float;
typedef Float Axis[3];
typedef Axis Axes[3];

static void copy(const Axes &from,Axes &to)
{
  for (size_t i=0; i!=3; ++i) {
    for (size_t j=0; j!=3; ++j) {
      to[i][j] = from[i][j];
    }
  }
}

static void mul(Axes &mat,Axes &b)
{
  Axes result;
  for (size_t i=0; i!=3; ++i) {
    for (size_t j=0; j!=3; ++j) {
      Float sum = 0;
      for (size_t k=0; k!=3; ++k) {
        sum += mat[i][k]*b[k][j];
      }
      result[i][j] = sum;
    }
  }
  copy(result,mat);
}

static void getAxes(Axes &result,Float yaw,Float pitch,Float roll)
{
  Float x = -pitch;
  Float y = yaw;
  Float z = -roll;
  Axes matX = {
    {1,     0,     0 },
    {0, cos(x),sin(x)},
    {0,-sin(x),cos(x)}
  };
  Axes matY = {
    {cos(y),0,-sin(y)},
    {     0,1,      0},
    {sin(y),0, cos(y)}
  };
  Axes matZ = {
    { cos(z),sin(z),0},
    {-sin(z),cos(z),0},
    {      0,     0,1}
  };
  Axes axes = {
    {1,0,0},
    {0,1,0},
    {0,0,1}
  };

  mul(axes,matX);
  mul(axes,matY);
  mul(axes,matZ);

  copy(axes,result);
}


static void showAxis(const char *desc,const Axis &axis,Float sign)
{
  std::cout << "  " << desc << " = (";
  for (size_t i=0; i!=3; ++i) {
    if (i!=0) {
      std::cout << ",";
    }
    std::cout << axis[i]*sign;
  }
  std::cout << ")\n";
}

static void showAxes(const char *desc,Axes &axes)
{
  std::cout << desc << ":\n";
  showAxis("front",axes[2],1);
  showAxis("right",axes[0],-1);
  showAxis("up",axes[1],1);
}

int main(int,char**)
{
  Axes axes;
  std::cout.setf(std::ios::fixed);
  std::cout.precision(1);
  getAxes(axes,0,0,0);
  showAxes("yaw=0, pitch=0, roll=0",axes);
  getAxes(axes,M_PI/2,0,0);
  showAxes("yaw=90, pitch=0, roll=0",axes);
  getAxes(axes,0,M_PI/2,0);
  showAxes("yaw=0, pitch=90, roll=0",axes);
  getAxes(axes,0,0,M_PI/2);
  showAxes("yaw=0, pitch=0, roll=90",axes);
  return 0;
}

关于c++ - 如何将欧拉角转换为前、上、右 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16074344/

相关文章:

opengl - 在我的代码 : Whats the licensing model? 中使用 NVidia FxAA

3d - 在3D游戏中飞行?

c++ - 在 C++ 中暂停程序执行 5 秒

c++ - std::logical_not 和 std::not1 之间的区别?

c++ - 在 OSX Big Sur 上使用 OpenGL 4.x 进行开发

Python:实时渲染的最佳 3D 格式?

c++ - 寻求对 constexpr 功能的澄清

c++ - 什么被认为是 C++ 中的小对象?

c++ - OpenGL:多种渲染方法..什么时候使用哪种?

c++ - Qt/QML/OpenGL : Z-ordering doesn't work with semi-transparent items with OpenGL under QML