Sign in to follow this  
soofian

OpenGL rotation

Recommended Posts

consider the example of a cube at center (0,0,0) when i rotate this cube it rotate about its center i.e (0,0,0) correct. but when i changed the center to (0,0,1) and rotate, it doesnot rotate about its center (0,0,1) but it rotate about (0,0,0). so what i want is that, the cube is any where on the screen it rotate about its center i mean when cube is at center(0,0,1) then it rotate at center(0,0,1) when cube is at center (2,3,4) then it rotate at center(2,3,4).i m using C# with OpenGL.

Share this post


Link to post
Share on other sites
Translate the cube to the origin, perform the rotation, then translate back. OpenGL transforms are applied in reverse relative to the code, so what you want might look something like this:

glTranslate(x, y, z);
glRotate(/*whatever*/);
glTranslate(-x, -y, -z);
RenderCube();

Share this post


Link to post
Share on other sites
Translating after rotating will move you relative to the rotation. The only way to counter this is to apply the operations in the reverse order after rendering the cube, or using the matrix stack to remove the need all together, like this:

glPushMatrix();
//move to the center of the cube
glTranslatef(0.0f,0.0f,1.0f);
//rotate the cube
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glRotatef(zRot,0.0f,0.0f,1.0f);
//draw the cube
...
glPopMatrix();

Share this post


Link to post
Share on other sites
Both of our solutions are correct, depending on what the OP means by 'changing the center'.

@the OP: If you are moving your cube by actually changing the position of the corners, you should use the code I posted. But if your cube corners are defined relative to the origin and you are simply repositioning via translation, use gorax's code.

I assumed the former because we often get that question here. However, the latter is the preferred way to handle object transformations.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this