Reputation: 1299
I've used the following lines for the rotation of the object in open gl es2. Translation and Scaling works properly, but rotation doesn't happen.
//modelViewMatrix is float[16];
Matrix.setIdentity(modelViewMatrix,0);
Matrix.translateM(modelViewMatrix, 0, x, y, z);
Matrix.rotateM(modelViewMatrix, 0, ax, 1, 0, 0);
//shader concatentation
//gl_Position = projection*modelview*position;
I tried making a custom matrix for rotation, but still the same result.
If I remove the rotation line, then translation works perfectly, but the moment I add the rotation line, the translation stops too.
Surprising to find that this same approach works properly in OpenGLES2 on iOS.
How can I solve this ? What is the proper way rotate an object in OpenGL es2 on android.
Upvotes: 0
Views: 231
Reputation: 451
Since there are no qualified answers yet, I'll throw in what I've "picked up" from playing with openGLES2, this may or may not be accurate or even correct.
My code seems to resemble what ClayMontogmery said - I translate my Model Matrix then have a seperate float[16] matrix to hold the rotation (mCurrentRotation). Once this so done, I Multiply the matrixs and put the result into a temporary matrix which I then use for the projection (my terminology might be a bit scatty here), below shows how I'm translating and rotating. I cobbled mine together from examples a while back:
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.8f, -3.5f);
// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 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 model taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);
and then in drawModel I have
/* this multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix (which now contains model * view * projection). */
Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
And the last line of onSurfaceCreated:
Matrix.setIdentityM(mAccumulatedRotation, 0);
This may not fully answer your question, but this code works for what I need, so you may be able to take something from it. I apologize in advance if any of this is "inncaruate", my knowledge on this is scarce at best, and I only replied because nobody else has given any useable answers.
Upvotes: 2