Jump to content
  • Advertisement
Sign in to follow this  
soofian

OpenGL rotation

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!