I need to extract euler angles from a 4x4 matrix ( OpenGL matrix ).
I have some problems, and i your help.
This is my matrix :
Code: Select all
|M0 M4 M8 M12|
M = |M1 M5 M9 M13|
|M2 M6 M10 M14|
|M3 M7 M11 M15|
Code: Select all
R1 = cos(y) * cos(z)
R2 = -cos(y) * sin(z)
R3 = sin(y)
R4 = sin(x) * sin(y) * cos(z) + cos(x) * sin(z)
R5 = -sin(x) * sin(y) * sin(z) + cos(x) * cos(z)
R6 = -sin(x) * cos(y)
R7 = -cos(x) * sin(y) * cos(z) + sin(x) * sin(z)
R8 = cos(x) * sin(y) * sin(z) + sin(x) * cos(z)
R9 = cos(x) * cos(y)
Code: Select all
#include <cstdlib>
#include <iostream>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cmath>
#define size 480
using namespace std;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("OpenGL Matrix !",0);
SDL_SetVideoMode(size, size, 32, SDL_OPENGL);
FILE *stream ;
stream = freopen("CON", "w", stdout);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glTranslated(0,0,0);
glRotated(95,1,0,0); // Problem with this axe
glRotated(130,0,1,0);
glRotated(-99,0,0,1);
float m[16] = {0};
glGetFloatv(GL_MODELVIEW_MATRIX , m);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cout << m[i+j*4] << "\t";
}
cout << endl;
}
float fAngX,fAngY,fAngZ;
fAngZ = atan2f(m[1], m[5]) * 180 / M_PI;
fAngY = atan2f(m[8], m[10]) * 180 / M_PI;
fAngX = -asinf(m[9]) * 180 / M_PI;
cout << "X >> " << fAngX << endl;
cout << "Y >> " << fAngY << endl;
cout << "Z >> " << fAngZ << endl;
SDL_Quit();
return 0;
}