Convert quaternions from D3D to OpenGL

Started by
17 comments, last by Zakwayda 14 years, 4 months ago
Did you try negating w and z?
Advertisement
Don't know how to directly convert quaternions from D3D to OpenGL (but I know it is possible, I'd have to do some math). But if you have little amount of data (or you can precompute it F.e. while loading scene), then convert your D3D quaternion to D3D matrix, now convert D3D matrix to OpenGL matrix and now OpenGL matrix to OpenGL quaternion.

This solution will work properly (although it will be much slower than converting quaternion directly, or you might also write this solution into few equations and try to solve how to perform it directly).

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

Negating x and y didn't work? I use that in my game. I got it by doing the matrix conversions and comparing the values like the previous poster suggested.

D3DXQUATERNIONs are quite strange from what I've found. The angle of rotation represents a CCW rotation around the axis, and it's simply converted to a CW rotation when they convert it into a matrix. However, this gives rather strange properties, such as performing a CW rotation when using the matrix, but a CCW rotation when using the quaternion directly.
-- gekko
Quote:[D3DXQUATERNIONs are quite strange from what I've found. The angle of rotation represents a CCW rotation around the axis, and it's simply converted to a CW rotation when they convert it into a matrix. However, this gives rather strange properties, such as performing a CW rotation when using the matrix, but a CCW rotation when using the quaternion directly.
I'm not sure if that's correct. How did you determine that positive rotations are CCW when the quaternion is applied directly?

IIRC, DirectX uses 'reverse' multiplication order for quaternions for consistency with the library's use of row-vector notation, which means that if you try to rotate a vector 'manually' using the standard formula for quaternion-vector rotation, the rotation will likely come out to be the opposite of what you expect. This isn't because DirectX quaternions 'encode' positive rotations in a CCW direction (which isn't actually even possible), but rather is just a side effect of the notational convention being used.

So unless I'm mistaken, the only thing that's at all unusual about the DirectX quaternion class is the order of multiplication; other than that, they should behave as expected.
I'm basing that on both the documentation, and from viewing the results of a rotation using both the matrix constructed from the quaternion, and the quaternion directly. Here's what the docs say:

http://msdn.microsoft.com/en-us/library/ee415479%28VS.85%29.aspx

Quote:However, the following illustrates how each element of a unit quaternion relates to an axis-angle rotation (where q represents a unit quaternion (x, y, z, w), axis is normalized, and theta is the desired CCW rotation about the axis):

q.x = sin(theta/2) * axis.x
q.y = sin(theta/2) * axis.y
q.z = sin(theta/2) * axis.z
q.w = cos(theta/2)


If I'm wrong in my interpretation of that, please correct me.
-- gekko
Quote:I'm basing that on both the documentation, and from viewing the results of a rotation using both the matrix constructed from the quaternion, and the quaternion directly.
How are you viewing the results of the quaternion directly? You must be performing *some* kind of conversion in order to visualize the results, right? Are you doing something like this?
v' = q * v * conjugate(q)
An axis-angle conversion? Something else?

I'm not doubting you, but without knowing *how* you're visualizing the quaternion rotation, I can only guess as to why the rotations aren't coming out as would be expected.
Quote:Here's what the docs say:

http://msdn.microsoft.com/en-us/library/ee415479%28VS.85%29.aspx

Quote:However, the following illustrates how each element of a unit quaternion relates to an axis-angle rotation (where q represents a unit quaternion (x, y, z, w), axis is normalized, and theta is the desired CCW rotation about the axis):
I don't know why it says CCW, but I can tell you that taken by itself a quaternion has no handedness, so whatever they mean, it must have something to do with how the quaternion is interpreted down the road (this is why I'm curious as to how you're visualizing it).

Consider the following quaternion:
x = 0y = .707z = 0w = .707
This quaternion represents (more or less) a rotation of +90 degrees about the y axis. Now, when viewed looking down the y axis from +y to -y, is this rotation CW, or CCW?

If a quaternion can be said to have an inherent handedness, then you should be able to tell just from those four numbers whether the rotation is CW or CCW. You can't, of course, because (geometric) handedness only comes into play when we add the element of visual interpretation.

Anyway, if you have the bit of code you're using to visualize the quat rotations available, I'd be interested in seeing it.
I was curious about this so I wrote a little program to test it out (using a left-handed coordinate system), and I was unable to get the results you describe. I rotated a vector using a positive rotation first encoded as a quaternion and then converted to a matrix, and then directly using the formula v' = conjugate(q) * v * q (reversed from the standard formula to match DX's reversed quaternion multiplication order). In both cases the vector rotated clockwise.

I'd still be interested to know how you got a CCW result. As for why the docs say CCW, I'm not sure (maybe it's a typo).
I was doing v' = q * v * conjugate(q). I noticed the rotation direction and reversed multiplication order at completely different times and never connected the two.

Thanks though, now I know.
-- gekko
Quote:I was doing v' = q * v * conjugate(q).
Yup, that would explain it :)

That leaves only the mystery of why the docs state that the rotation is counterclockwise. My guess is it's a typo, or just an error that's never been fixed. (In any case, it doesn't really make sense to specify a rotation direction in the docs at all; since DX has support for both left- and right-handed coordinate systems, a positive rotation could be either CW or CCW, depending on how the projection and view transforms are set up.)

This topic is closed to new replies.

Advertisement