如何将Euler角度转换为Front,Up,Right向量

时间:2013-04-18 04:11:44

标签: c++ opengl 3d glm-math

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

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

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

  • yaw = 0,pitch = 0,roll = 0, 预期的产出是:

    • front =(0.0,0.0,1.0)
    • right =( - 1.0,0.0,0.0)
    • up =(0.0,1.0,0.0)
  • yaw = 90,pitch = 0,roll = 0, 预期的产出是:

    • front =(1.0,0.0,0.0)
    • right =(0,0,0.0,1.0)
    • up =(0.0,1.0,0.0)
  • yaw = 0,pitch = 90,roll = 0, 预期的产出是:

    • front =(0.0,1.0,0.0)
    • right =( - 1.0,0.0,0.0)
    • up =(0.0,0.0,-1.0)
  • yaw = 0,pitch = 0,roll = 90, 预期的产出是:

    • front =(0.0,0.0,1.0)
    • right =(0.0,1.0,0.0)
    • up =(1.0,0.0,0.0)

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

1 个答案:

答案 0 :(得分:2)

这是一个完整的工作示例。它不像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;
}