This helps to understand the theory of things well before trying to implement and use it. Below are two introductory articles about using quaternions for rotation. Both of them are mainly associated with smooth interpolation of rotation and exclude the fixation of the driveshaft during accumulated rotations:
Gamedev.net - Quaternion Powers
Gamasutra - Rotate objects using quaternions
Now, as far as the real code goes, I would suggest getting and using the "vector math library" in the industry, rather than folding your own. My suggestion would be to capture part of the LinearMath project of Bullet Physics Middleware. Bullet physics and an integrated linear math library are being developed by some of Sony's leading engineers and have been under active development for many years. It is freely available, not limited by license (Zlib license) and is used by professional game developers around the world. Lib is a cross-platform / architecture and compiles on any device from iPhone to PS3.
The lib Quaternion class is proposed, which allows you to create quaternions from Euler angles or from rotation around an arbitrary axis, for example. using setEulerZYX . When you have your quaternions, there are built-in functions for all common operations that apply to them; plus, minus, mul, normalize, slerp and more.
To actually apply your final quaternion to OpenGL rendering, the Transform class allows you to build a matrix from a quaternion. The conversion class, in turn, includes the getOpenGLMatrix function, which directly gives you a compatible matrix for moving to OpenGL.
Lib also includes many other very useful matrix and vector classes and functions.
Grab the latest version of Bullet from google code or grab only a portion of LinearMath code directly from a disruptive program using: svn checkout http://bullet.googlecode.com/svn/trunk/src/LinearMath
source share