28119

Rotating object around itself in OpenGL

Question:

i'm having trouble rotating a room around itself. its center is at (5,5,0) so i thought if i translated the room and the objects in the room with glTranslatef(5,5,5) then glRotatef(rotateroom,0,0,1) and then draw the items and use glTranslate(-5,-5,0) it would rotate the room and everything in it around 5,5,0 but it seems to still be rotating around (0,0,0) and i'm not really sure what I'm doing wrong. Thanks for the help in advance.

void drawside(){ int i,j; /*for (j = 0; j <= 8; j++) { glBegin(GL_LINE_STRIP); for (i = 0; i <= 30; i++) glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0); glEnd(); glBegin(GL_LINE_STRIP); for (i = 0; i <= 30; i++) glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0); glEnd(); }*/ glEvalMesh2(GL_FILL, 0, 20, 0, 20); } void drawRoom(){ //floor glBegin(GL_POLYGON); glColor3f(1,1,1); glNormal3f(0,0,0); glVertex3f(0,0,0); glNormal3f(0,10,0); glVertex3f(0,10,0); glNormal3f(10,10,0); glVertex3f(10,10,0); glNormal3f(10,0,0); glVertex3f(10,0,0); glEnd( ); //wall glBegin(GL_POLYGON); glColor3f(0,0,1); glNormal3f(0,10,0); glVertex3f(0,10,0); glNormal3f(0,10,10); glVertex3f(0,10,10); glNormal3f(10,10,10); glVertex3f(10,10,10); glNormal3f(10,10,0); glVertex3f(10,10,0); glEnd(); //wall2 glBegin(GL_POLYGON); glColor3f(0,1,0); glNormal3f(10,10,0); glVertex3f(10,10,0); glNormal3f(10,10,0); glVertex3f(10,10,10); glNormal3f(10,0,10); glVertex3f(10,0,10); glNormal3f(10,0,0); glVertex3f(10,0,0); glEnd(); } void drawObjects(){ glColor3f(1,0,1); glTranslatef(2,2,0); if(conefill == 1) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); glutSolidCone(cone,5,10,2); glTranslatef(5,5,0); glColor3f(1,0,0); if(spherefill == 1) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); glutSolidSphere(sphere,20,20); } void display(){ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(viewx,viewy, viewz,viewx +lx, 5, viewz + ly,0.0f, 0.0f, 1.0f); //gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1); glClear(GL_COLOR_BUFFER_BIT); GLfloat light_position[] = {-1.0,5.0,5.0,0.0}; //GLfloat light_direction[] = {-5,-5,-5}; //GLfloat amb[] = {0,0,0,1}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction); //glLightfv(GL_LIGHT0, GL_AMBIENT,amb); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(5,5,0); glRotatef(rotateroom,0,0,1); glPushMatrix(); drawRoom(); glPopMatrix(); glColor3f(0.0, 0.0, 1.0); glPushMatrix(); glTranslatef(6,3,1); glPushMatrix (); drawside(); glPopMatrix(); glPushMatrix(); //glTranslatef(5,0,0); glRotatef(90,1,0,0); //glTranslatef(-5,0,0); glTranslatef(0,.5,.5); drawside(); glPopMatrix(); glPushMatrix(); //glTranslatef(5,0,0); glRotatef(180,1,0,0); //glTranslatef(-5,0,0); glTranslatef(0,1,0); drawside(); glPopMatrix(); glPushMatrix(); glRotatef(90,0,0,1); glTranslatef(-.5,.5,0); drawside(); glPopMatrix(); glPushMatrix(); glRotatef(270,0,0,1); glTranslatef(.5,.5,0); drawside(); glPopMatrix(); glPopMatrix(); drawObjects(); glTranslatef(-5,-5,0); glPopMatrix(); glutSwapBuffers(); glFlush(); }

Answer1:

You only need to invert the order of the istruction:

glRotatef(rotateroom,0,0,1); glTranslatef(5,5,0);

Whenever you trasform a point you must invert the sequence of trasformation you want to apply. This dipend on the way the transformation matrix are multiplicated.

Recommend