1

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?

Community
  • 1
  • 1
wd40bomber7
  • 43
  • 4
  • 8
  • 1
    I can't think of any respectable site that would suggest using Euler angles for any reason other than as a practical example of something to avoid at all costs. If your question starts with "I'm using Euler angles and I'm having this problem ..." the solution is "DON'T USE EULER ANGLES" in 99% of the cases. OpenGL accepts either angle/axis or a full transform matrix, there are plenty of references on how to compute and compose either from individual rotations, such as http://www.euclideanspace.com/maths/geometry/rotations/index.htm . – DanielKO Apr 03 '13 at 20:19
  • Yes I've seen that webpage. Unfortunately nothing I've gotten off of it has correctly done the conversion from euler to the inputs glRotatef expects. Furthermore it's in direct conflict with several other examples/other webpages. I have euler angles, whether I want them or not is irrelevant. I have euler angles and I must get them into opengl somehow. – wd40bomber7 Apr 04 '13 at 02:41
  • It doesn't matter what form you have them in, they represent the same rotation and they'll exhibit the same issues. So rotating around each axis in turn is easiest if you just want to 'get them into opengl', with the only thing you have to be careful of is that the order the rotations are applied is consistent with the way the angles were derived. But that is also true if you try to turn them into a single axis/angle, or any other representation. The inherent problem is that euler angles are interdependent. – JasonD Apr 04 '13 at 05:59
  • Maybe you are experiencing a deeper misinterpretation of what rotations are all about. The orientation of a 3D object can be represented uniquely by a 3x3 rotation matrix. You start from the initial orientation (with no rotation, the identity matrix), and apply a rotation, say Ra, its new orientation is given by Ra. Apply then another rotation, the new orientation is Ra * Rb (or Rb * Ra, depending on the convention you chose); the next one turns it into Ra * Rb * Rc. When it's time to tell it to OpenGL you call glMultiplyMatrix(). Or break it into angle-axis to feed to glRotate(). – DanielKO Apr 04 '13 at 20:06
  • I'm sorry for sounding condescending, I may be having a bad time figuring out what the problem is. If you want to work with Euler angles, make 3 rotations in the proper order you chose. Turn every rotation into a matrix or a quaternion; composing multiple rotations is a matter of multiplying said matrices or quaternions. This is a solved problem, every piece of code providing matrices and/or quaternions will let you multiply them, and build one from axis and angle. Few people will have the patience to check if your code's final formula is equivalent to said multiplication. – DanielKO Apr 04 '13 at 20:12

1 Answers1

1

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.

wombat
  • 90
  • 8
  • I am aware of why the first method doesn't work. That's why I so quickly dismissed it. Also I am not in a position to use any form of external library. Ideally I'd be able to look through the source of the library and find how it's implemented, but my attempt at doing so was not successful. – wd40bomber7 Apr 04 '13 at 00:27
  • GLM is a header only library so you can just add all the headers to your project as you would add your own source. No compilation or install should be necessary. – wombat Apr 04 '13 at 08:31