Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trackball Rotation in OpenGL

Tags:

opengl

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?

like image 421
sudo Avatar asked Oct 28 '10 04:10

sudo


2 Answers

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.

like image 63
Daniel Paull Avatar answered Sep 25 '22 20:09

Daniel Paull


+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:

  • The Blender project
  • Meshlab / VCGlib.
like image 24
Kyle Simek Avatar answered Sep 23 '22 20:09

Kyle Simek