I am having trouble implementing a trackball rotation on OpenGL. When I rotate my cube using the trackball rotation 90 degrees to the right along the X axis (dragging mouse left to right on screen) and then try rotating it dragging my mouse from the top to the bottom of the screen, I expect the cube to rotate along my perspective's y-axis. Instead it rotates along ITS y-axis after rotation, rotating sideways from my perspective.
Can someone show me what I may be doing wrong?
It sounds like you're not using quaternions to represent the rotation. If you Google for "Arcball Graphics Gems" you should be able to find code by Ken Shoemake’s in Graphic Gems IV.
Or, if you just want the code, go here.
+1 for Daniel's advice about quaternions and Arcball. You should always be using quaternions to represent orientations and rotations.
One additional tip: the extension of Arcball implemented by Gavin Bell makes it much more intuitive for mouse clicks outside the rotation sphere. It is distributed with in the examples of the Glut library, here and here.
It is similar to the NeHe implementation, but the transition between inside and outside the sphere is smooth. Bell's approach is used in a number of open source 3D libraries, including:
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