opengl - How to convert Euler Angles to Front, Up, Right vectors -


i need function given yaw, pitch, , roll, can produce front (or looking at), right, , vectors in "world coordinates".

in particular world space, starting origin (0,0,0), x positive left, z positive going away viewer/origin, , y positive going up.

for example, given... (angles in degrees)

  • yaw=0, pitch=0, roll=0, expected output is:

    • 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, expected output is:

    • 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, expected output is:

    • 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, expected output is:

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

the language i'm working in c++, , gladly use glm solve problem if makes sense. if can there through quaternion's i'm fine solution well, since i've found other tutorials describe how quaternion euler angles.

here full working example. isn't c++-like. want use real matrix class, should ok demonstration purposes. 1 thing isn't clear question rotation order, can changed.

#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) {   (size_t i=0; i!=3; ++i) {     (size_t j=0; j!=3; ++j) {       to[i][j] = from[i][j];     }   } }  static void mul(axes &mat,axes &b) {   axes result;   (size_t i=0; i!=3; ++i) {     (size_t j=0; j!=3; ++j) {       float sum = 0;       (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 << " = (";   (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; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -