Reputation: 43
I want to use euler angles with opengl. Some people suggest something like:
glRotatef(Rx,1,0,0);
glRotatef(Rx,0,1,0);
glRotatef(Rx,0,0,1);
Of course if you rotate in more than one direction at a time this method completely falls apart. The better approach as far as I know is to convert everything to the format OpenGL expects (angle and a vector)
Euler angles to Quaternion is something that is covered rather extensively in a lot of different places on the internet. Unfortunately it seems every post on the internet has a different suggestion, and for the life of me none of them seem to work. It is very possible there is a suggestion that works, but it is lost to me in the long list of not quite working suggestions.
Following this suggestion has gotten me close. The rotations work in individual directions, but when combined they seem to misbehave. Currently my code looks like this:
double c1 = cos((Rx/180.0f*3.141592)/2.0f);
double c2 = cos((Ry/180.0f*3.141592)/2.0f);
double c3 = cos((Rz/180.0f*3.141592)/2.0f);
double s1 = sin((Rx/180.0f*3.141592)/2.0f);
double s2 = sin((Ry/180.0f*3.141592)/2.0f);
double s3 = sin((Rz/180.0f*3.141592)/2.0f);
double x = (s1*c2*c3-c1*s2*s3)*180.0/3.141592;
double y = (c1*s2*c3+s1*c2*s3)*180.0/3.141592;
double z = (c1*c2*s3-s1*s2*c3)*180.0/3.141592;
double w = acos(c1*c2*c3+s1*s2*s3)*2.0*180.0/3.141592;
//normalize vector, maybe not necessary?
double mag = sqrt(x*x+y*y+z*z);
x /= mag;
y /= mag;
z /= mag;
glRotatef(w,x,y,z);
Can anyone offer any suggestions on where to go from here?
Upvotes: 1
Views: 2842
Reputation: 90
The reason the first suggestion doesn't work is because it is rotating relative to the previous rotations. For example, the second line (assuming you meant glRotatef(Rx,1,0,0); glRotatef(Ry,0,1,0); glRotatef(Rz,0,0,1);) will rotate relative to the new y axis which has already been rotated by the first line. You want to rotate relative to the original axis.
Wolfram Alpha (http://mathworld.wolfram.com/EulerAngles.html) has a comprehensive description of Euler Angles, whatever representation you want to use.
One possibility would be to use the OpenGL Mathematics Library and use the extension. You can then use axisAngle rotations around each axis successively.
Upvotes: 1