I have an object which I want to rotate via keys. The object should yaw, pitch and roll. After trying a lot, I figured out that glRotate
has its limitations and it won't be possible to implement something like that with that function.
I've researched a bit and found out about quaternion-based rotation. It would be also possible to rotate via a rotation matrix, but almost everyone describes the quaternions as the best ever.
I have read about the quaternions and understood them fairly well, but how to implement them in my OpenGL program is still a mystery. Does anyone know a small example? Not how to implement the quaternion class but how to use it. I don't get how to put the things together.
Both OpenGL and Direct3D give you ways to specify rotations as matrices, so a quaternion-to-matrix conversion routine is useful.
Unit quaternions, known as versors, provide a convenient mathematical notation for representing spatial orientations and rotations of elements in three dimensional space. Specifically, they encode information about an axis-angle rotation about an arbitrary axis.
eul = quat2eul( quat ) converts a quaternion rotation, quat , to the corresponding Euler angles, eul . The default order for Euler angle rotations is "ZYX" . eul = quat2eul( quat , sequence ) converts a quaternion into Euler angles. The Euler angles are specified in the axis rotation sequence, sequence .
You can easily build rotation matrices out of unit quaternions.
Given a unit quaternion a + bi + cj + dk, you can build the following 3x3 matrix:
Add the last line and column taken from the identity 4x4 matrix, glMultMatrix
and you're done :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With