PDA

View Full Version : Rotating an Object around its center.



avanindra
11-18-2009, 03:13 PM
Hi,

My program creates array of voxels of any scanned object. What I want is to rotate the object around its center.

Right now my code displays the object but when I rotate it, it is rotated around some point which cause the object to go outside the window. I rather want to rotate the object around its center, so that I could see its all parts in the window. I have already calculated the center of the object and I have put center of interest in gluLookAt function as center of the object.

Can anyone please help me out to solve this problem.

Thanks

dorbie
11-18-2009, 04:13 PM
Translate center to origin, rotate, translate back to center:

glMatrixMode(GL_MODLEVIEW);
glPushMatrix();
glTranslate(-xcenter, -ycenter, -zcenter);
glRotate(.....whatever.....);
glTranslate(xcenter, ycenter, zcenter);
DrawObject();
glPopMatrix();

marshats
11-18-2009, 04:29 PM
The order order of operations might need to be reversed as follows:



glMatrixMode(GL_MODLEVIEW);
glPushMatrix();
glTranslatef(xcenter, ycenter, zcenter); // move back to focus of gluLookAt
glRotatef(.....whatever.....); // rotate around center
glTranslatef(-xcenter, -ycenter, -zcenter); //move object to center
DrawObject();
glPopMatrix();

avanindra
11-19-2009, 11:04 AM
Thanks dorbie and marshats.

The code worked perfectly.