Search code examples
openglrotationglrotate

Rotate a object in OpenGL


I want to move an objecto in OpenGL. I draw the object in (0,0,0), if the object is on that point it rotates fine (using the center of the object as reference to rotate). The problem comes when I move the object, it rotates using the same point as reference and not the point where it is. The code is:

glPushMatrix();
glRotatef(xangle,1.0f,0.0f,0.0f);
    glRotatef(yangle,0.0f,1.0f,0.0f);
    glRotatef(zangle,0.0f,0.0f,1.0f);
    glTranslatef(x,y,z);
//draw object
    glPopMatrix();

Does anyone know what it have to do so the object always rotates using the point where it is as reference?


Solution

  • Just reorder your transformations:

    glPushMatrix();
    glTranslatef(x,y,z);
    glRotatef(xangle,1.0f,0.0f,0.0f);
    glRotatef(yangle,0.0f,1.0f,0.0f);
    glRotatef(zangle,0.0f,0.0f,1.0f);
    //draw object
    glPopMatrix();