The last kind of conversion you will learn about in this chapter converts a rotation angle and axis to a quaternion. You’ll need to do this because most graphics APIs cannot use a quaternion to directly represent a rotation, so when the time comes to apply the rotation, the quaternion must be converted to another form. It might be possible to simply rotate around an axis using a function such as glRotate, rather than build a complete rotation matrix. Because there are fewer operations involved in converting from a quaternion to an axis angle than converting from a quaternion to a rotation matrix, the axis-angle approach may be a better choice when available.
This is by far the easiest conversion of the three. The left side of Figure 2.7 shows how axis-angle rotation can be represented in three dimensions. The angle is a rotation around an arbitrary axis, generally represented by a unit vector. To convert your axis/angle pair to a
quaternion, you first must make sure that your axis is a unit vector. If it is, you calculate sin(angle / 2) and divide all the components of the axis by it before storing them in the vector component of the destination quaternion. The scalar part of the quaternion is calculated using the formula cos(angle / 2). Simple enough? Check out the right side of Figure 2.7 if you are still a little bit confused.
Figure 2.7 Left:A picture axis-angle rotation. Right: Converting the axis and angle to a quaternion.
Converting back is not hard at all. Using the inverse trig functions to reverse the earlier operations and one of the most basic trig identities, you can easily extract the rotation angle and axis. Step right over to Figure 2.8 to see how to do it.
angle = 2arccos (qw)
Figure 2.8 Converting a quaternion to a rotation axis and angle. This conversion is cheaper in terms of processing power than a quaternion-to-matrix conversion, making it ideal for use when a rotation matrix is not necessary and when the graphics API can accept a rotation in the axis-angle format.