Sign in to follow this  
irdalama

OpenGL cant get it to rotate right

Recommended Posts

Hey, im trying to rotate objects from the center of their body. I am using visual c++ with the opengl, its rotating from the bottom left corner of the front face (+z normal - righthand coord system) of a cube. I used the glBegin(Gl_QUAD) and drew six faces that ended up as a cube. How can i get my rotate functions to rotate from the center of cube's body instead of rotating from the front face corner(+z normal - right hand coord system)? thanks for any help.

Share this post


Link to post
Share on other sites
make sure one corner of your cube is not sitting at 0,0,0. that would cause a problem. if it is, translate your cube so that 0,0,0 is at the center of your cube and then try rotating it.

Share this post


Link to post
Share on other sites
You need not change the model's data if it's difficult - you can effectively change the model's orientation with a conjugate-translation:

The point about which a model rotates is the origin of the current space's coordinates. In your case, it looks like this is at the corner of the cube. In order to rotate about its centre, you need to translate the object so that it is centred about the origin. Supposing that the centre of the object is at (x0, y0, z0) wrt the current coordinates; call glTranslatef(-x0, -y0, -z0), rotate, then finally translate back. The whole operation goes from

glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);

to

(Edit:)
glTranslatef(x0, y0, z0);
glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);
glTranslatef(-x0, -y0, -z0);

Sorry if any of my syntax is wrong: OpenGL isn't my native tongue and I haven't used it in a while.

Regards
Admiral

[Edited by - TheAdmiral on September 28, 2006 2:26:26 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by TheAdmiral
glTranslatef(-x0, -y0, -z0);
glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);
glTranslatef(x0, y0, z0);

Not quite. To rotate around an arbitrary point T in OpenGL, you do this:
    glTranslatef( T.x, T.y, T.z );
glRotatef( angle, x, y, z );
glTranslatef( -T.x, -T.y, -T.z );
In math form, it corresponds to v' = TRT-1v. Using D3D's row vector notation, it would be written as v' = vT-1RT.

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