PDA

View Full Version : Matrix transformations for trackball rotation OpenGL (LWJGL)



jamie_wizard
09-17-2014, 08:13 AM
Hi,

I have some code to implement a trackball which I've adapted from an OpenGL ES Android
example online. It works fine on Android, I want to use this same method in the desktop
version of my program which uses OpenGL (LWJGL), but for some reason the code is
not working...The model rotates but in a random, chaotic manner....

Because OpenGL-ES on android has the Matrix class with native bindings, I've had to merely
change the matrixes from float[16] (android) to Matrix4f (LWJGL), but it's not working
as expected!


IN MOUSE MOVE EVENT
DeltaX and DeltaY set appropriately

ON INITIALISATION

mAccumulatedMatrix.setIdentity();


AFTER SETTING UP SCENE

// ----------------------------------------------------------------------------------------------
// Trackball Rotation matrix code from:
// http://www.learnopengles.com/rotating-an-object-with-touch-events/

// Set a matrix that contains the current rotation.
mCurrentRotation.setIdentity();
Matrix4f.rotate(DeltaX, new Vector3f(0f,1f,0), mCurrentRotation, mCurrentRotation);
Matrix4f.rotate(DeltaY, new Vector3f(1f,0f,0), mCurrentRotation, mCurrentRotation);
DeltaX = 0.0f;
DeltaY = 0.0f;

// Multiply the current rotation by the accumulated rotation, and then set the accumulated
// rotation to the result.
Matrix4f.mul(mCurrentRotation, mAccumulatedRotation, mTemporaryMatrix);
Matrix4f.load(mTemporaryMatrix, mAccumulatedRotation);

FloatBuffer matrixBuff = BufferUtils.createFloatBuffer(16);
mAccumulatedRotation.store(matrixBuff);
matrixBuff.flip();
GL11.glMultMatrix(matrixBuff);

DRAW_THE_MODEL();

// End Trackball code ---------------------------------------------------------------------------



THE ORIGINAL OPENGL ES 1.0 ANDROID CODE (WORKS FINE) IS BELOW:

----------------------------------------------------------------------------------------------
// Trackball Rotation matrix code from:
// http://www.learnopengles.com/rotating-an-object-with-touch-events/

// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, DeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, DeltaY, 1.0f, 0.0f, 0.0f);
DeltaX = 0.0f;
DeltaY = 0.0f;

// Multiply the current rotation by the accumulated rotation, and then set the accumulated
// rotation to the result.
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);

// Rotate the cube taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);

gl.glMultMatrixf(mAccumulatedRotation, 0);

DRAW_THE_MODEL();
// End Trackball code

---------------------------------------------------------------------------

Jamie

mynameisjohn
09-21-2014, 10:29 PM
I'm no expert here, and I'm not familiar enough with these libraries to say anything for certain, but could it be that one uses degrees while the other uses radians?