Convert Quaternion rotation to rotation matrix?

Viewed 44965

Basically, given a quaterion (qx, qy, qz, qw)... How can i convert that to an OpenGL rotation matrix? I'm also interested in which matrix row is "Up", "Right", "Forward" etc... I have a camera rotation in quaternion that I need in vectors...

4 Answers

You might not have to deal with a rotation matrix at all. Here is a way that appears to be faster than converting to a matrix and multiplying a vector with it:

  // move vector to camera position co (before or after rotation depending on the goal)
  v -= co;

  // rotate vector v by quaternion q; see info [1]
  vec3 t = 2 * cross(q.xyz, v);
  v = v + q.w * t + cross(q.xyz, t);

[1] http://mollyrocket.com/forums/viewtopic.php?t=833&sid=3a84e00a70ccb046cfc87ac39881a3d0

Related