# rotating objects

Does anyone know how to rotate objects the same as in 3d cad programs, i.e. the axis that you rotate about alters as the 3d image you have is rotated.

i.e. say I have a 3d box, if I move right to left the box rotates around the vertical axis (of the current view, not the box) and up and down it rotates about the horizontal axis (of the view).

if I use something like:

glRotatef ( angle, rotation.x, rotation.y, rotation.z );

then it wants to rotate about the 3d box, not the axis of the view.

Do you understand what I am aksing for? If so any help would be much appreciated.

Thanks

I have what I believe is the same question: How do you rotate an object around its axes, which have no relation to the 3d “world” frame?

Does OpenGL keep track of individual object’s axes?

Thanks,
Erik

GLfloat mm[16];
// FIRST BLOCK
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, mm);

// SECOND BLOCK
glMatrixMode(GL_MODELVIEW);
glRotatef(delta_phi, 0., 0., 1.);
glRotatef(delta_psi, 1., 0., 0.);
glMultMatrixf(mm);
glGetFloatv(GL_MODELVIEW_MATRIX, mm);
// THIRD BLOCK
gluLookAt(0., -10., 0., 0., 0., 0., 0., 0., 1.);
glMultMatrixf(mm);
DrawScene();

you need to “remember” your current transformation matrix in GLfloat mm[16]. you need to initialize mm as identity which is done in the first few lines. you do this ONCE at the beginning of the program.

in the second block you rotate your model around the x and z axis, multiply the current transformation matrix and put the result back into the transformation matrix.

in the third block you use the lookat-function and multiply the current transformation matrix.

you have to call the second and third block everytime you want to rotate your model.

in this example you look at the x-z-plane; if you want to look into another direction, you have to change the rotation axes in the glRotatef calls.

Google: rotate arbitrary axis, then create a function to implement it rather than mess with OpenGL’s conceptually messy matrix functions…

that may work if you want to rotate a cube or something as simple as that.

but as soon as you have a hundred thousand vertices to rotate it will be too much for your cpu.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.