# Applying two rotations to the same object

I have a simple rectangular shape in my application which I am rotating fixing one of its side, and rotating around the X axis. The rotation looks something like the following figure (grey denotes the current rotation that I am getting with this figure)

I am using the following code to get this rotation

``````    glPushMatrix();
glTranslatef(position of the axis point which has to be fixed for rotation);
glRotatef(rotationAmount, 1,0,0);
glTranslatef(-position of the axis point which has to be fixed for rotation);
Rectangle(xPosition, Position,200,100);
glPopMatrix();
``````

However, I have to rotate this same figure with an additional rotation around y axis around one of its side (green arrow direction in the figure). How do I rotate this rectangle such that it keeps on rotating around x-axis and around y-axis simaltaneously?

I did try adding another glRotatef(rotateAroundYaxis amount,0,1,0) but the output does not actually looks like what I was expecting. The figure rotates in two quadrants instead of rotating around y-axis like a simple page turn.

While if I try these two rotations independently using only one of them in the program (not both of them together), i.e.

``````    glRotatef(rotateAmount,1,0,0);
glRotatef(rotateYamount,0,-1,0);
``````

I do get the required X and Y rotations independently, but when together, it combines into some weird rotation effect.

I have a simple rectangular shape in my application which I am rotating fixing one of its side, and rotating around the X axis. The rotation looks something like the following figure (grey denotes the current rotation that I am getting with this figure)

I am using the following code to get this rotation

``````    glPushMatrix();
glTranslatef(position of the axis point which has to be fixed for rotation);
glRotatef(rotationAmount, 1,0,0);
glTranslatef(-position of the axis point which has to be fixed for rotation);
Rectangle(xPosition, Position,200,100);
glPopMatrix();
``````

However, I have to rotate this same figure with an additional rotation around y axis around one of its side (green arrow direction in the figure). How do I rotate this rectangle such that it keeps on rotating around x-axis and around y-axis simaltaneously?

I did try adding another glRotatef(rotateAroundYaxis amount,0,1,0) but the output does not actually looks like what I was expecting. The figure rotates in two quadrants instead of rotating around y-axis like a simple page turn.

While if I try these two rotations independently using only one of them in the program (not both of them together), i.e.

``````    glRotatef(rotateAmount,1,0,0);
glRotatef(rotateYamount,0,-1,0);
``````

I do get the required X and Y rotations independently, but when together, it combines into some weird rotation effect.

combing the 2 rotations with the 2 calls is correct even if it looks weird

Try first call the Y rotation then call the X rotation.

Try first call the Y rotation then call the X rotation.

OK. Optimus Prime. It seems you can’t transform right.
Any transformation order as t1,t2,…,tn. You should write your code as tn,…,t2,t1.

Remember, to transform in OpenGL world, you should turn your head down and put your feet up.

Best Regards,

newbiecow

OK. Optimus Prime. It seems you can’t transform right.
Any transformation order as t1,t2,…,tn. You should write your code as tn,…,t2,t1.

Remember, to transform in OpenGL world, you should turn your head down and put your feet up.

Best Regards,

newbiecow