I am trying to rotate a Triangle along Z axis from centre of triangle. But here i am getting triangle rotation from centre of an edge of triangle and not from its centre.
Renderer code:
@Override
public void onDrawFrame(GL10 gl) {
float scratch[] = new float[16];
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
int time = (int) (SystemClock.uptimeMillis() % 4000l);
float angle = 0.090f * time;
Matrix.setRotateM(mRotationMatrix, 0, angle, 0.0f, 0.0f, -1.0f);
Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
triangle.draw(scratch);
}
The problem is more likely due to your vertices, which compose the triangle.
Solution 1: Before rotating the triangle, translate it so that its center aligns with the scene's center.
Solution 2: Provide vertices, that are around the center. For example:
glVertex(0,0,0);
glVertex(1,0,0);
glVertex(0,1,0); // will produce rotation around the first vertex
... so offset them with a half:
glVertex(0-0.5,0-0.5,0-0.5);
glVertex(1-0.5,0-0.5,0-0.5);
glVertex(0-0.5,1-0.5,0-0.5); // will produce rotation around the approximate center
Best way is to calculate the center and translate before rotation.
Good luck!