CroftSoft / Library / Tutorials

Quaternion Rotations in 3D Java OpenGL

David Wallace Croft

2008 Sep 28 Sun


CroftSoft JOGL Demo Applet

The above applet demonstrates a multicolored rotating cube in 3D. By mouse clicking on the applet, you can change the rotation speed. By using the keyboard, you can translate and rotate the viewpoint in six dimensions: x, y, z, pitch, yaw, and roll.

Input Command
Mouse click Change cube rotation speed
A Translate left
D Translate right
W Translate forward
S Translate reverse
Up arrow Translate up
Down arrow Translate down
Left arrow Yaw left
Right arrow Yaw right
Shift-Left arrow Roll left
Shift-Right arrow Roll right
Shift-Up arrow Pitch down
Shift-Down arrow Pitch up

JOGL

The Java applet uses the Java Binding for OpenGL (JOGL) application programming interface (API). This permits the applet to take advantage of your graphics card for hardware acceleration. The current frame rate is displayed in the lower left corner of the applet. I have limited it to the round number of 100 frames per second so that it will not use excess processor time.

Quaternion Rotations

In order to make all of these translations and rotations relative to the current orientation of the viewer, I used quaternion rotations. In the process, I developed a pure Java library of axis angle, matrix, and quaternion classes for 3D math. Getting the math correct, especially the signs and the order of operations, took some research and effort so I wanted to document the library in this tutorial.

  • Class JoglModelImp (JOGL model implementation) processes the move request message by passing the relative axis of translation or rotation and the distance to translate or rotate to an instance of JoglCameraImp.

  • JoglCameraImp maintains the viewer orientation, the "camera", as an AxisAngle instance because that is the data structure that is used by JoglRendererImp to make the OpenGL rendering call.

  • During a rotation update called by JoglModelImp, JoglCameraImp converts the axis angle representation in order to use quaternion multiplication and then converts the result back to axis angle representation again.

Open Source

The Java code demonstrated here is available for you to reuse under the terms of an Open Source license. You can download it from the CroftSoft Code Library.

Browse Javadoc

I included the "linksource" option when I generated the javadoc so you can browse the source code directly from the javadoc by clicking on the class name and method name links:

Browse CVS

You might prefer to browse the source code directly from the CVS repository:

Books

Here are a couple of books that I recommend if you are interested in learning more about quaternion rotations for 3D graphics.

 
 
 
CroftSoft
 
 
About
Library
-Books
-Code
-Courses
-Links
-Media
-Software
-Tutorials
People
Portfolio
Update
 
 
Google
CroftSoft Web

 

Creative Commons License
© 2011 CroftSoft Inc.
You may copy this webpage under the terms of the
Creative Commons Attribution License.