I am trying to rotate an object around it’s center with the following code :
-(void)render
{
// clear the matrix
glPushMatrix();
glLoadIdentity();
// move to my position
//scale
glScalef(0.000015, 0.000015, 0.000015);
xRotation =280.0;
zRotation =1.0;
yRotation +=0.5;
glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(yRotation, 0.0f, 1.0, 0.0f);
glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
[mesh render];
//restore the matrix
glPopMatrix();
}
This code works correctly and the object rotates around the center. The problem is that I also have a matrix which defines the starting position of the object in the scene called matricesArray. I added a new line to my code to multiply by this matrix and place the object in its correct starting position :
-(void)render
{
// clear the matrix
glPushMatrix();
glLoadIdentity();
// move to my position
//xRotation = 280;
//scale
glScalef(0.000015, 0.000015, 0.000015);
xRotation =280.0;
zRotation =1.0;
yRotation +=0.5;
glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(yRotation, 0.0f, 1.0, 0.0f);
glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
glMultMatrixf(matricesArray); //This is the line I have added to apply the matrix.
[mesh render];
//restore the matrix
glPopMatrix();
}
The problem is that the object now rotates in a wide circle around the screen.
My question is what is the correct order to apply the matrix in my code ?
Thank you in advance.
You need to first rotate then translate to the location you want it to be.