glTranslatef messing up previously called glRotatef

im trying to rotate a simple cube round on origin to get a bit of experience in animation with openGL, but it aint working ???
it seems ti still rotate around hte point 0,0
heeres my code


glPushMatrix();
    glTranslatef(-px,-py,0);
    glRotatef(angle,0.0,0.0,1.0);
    glTranslatef(px,py,0);
    glBegin(GL_QUADS);
        glVertex2f(px,py);
        glVertex2f(px+50,py);
        glVertex2f(px+50,py+50);
        glVertex2f(px,py+50);
    glEnd();
    angle+=1;
glPopMatrix();

thanks for any help in advance, P.S im actually using C++ on this but i believe the openGl is just the same as in Java

But is it really rotating, or it does nothing?

Think we need a bit more info mate, px and py, then perhaps how you set things up.

well currently px and py are set to 90, but it should work whatever number you make them, the issue is that the cube is at x=90 and y=90 but is not rotating around the origin of the cube like i wanted it to

[s]yep.

To rotate around the origin you have to translate to the middle of the cube! :slight_smile:

(Such a COMMON mistake. The web helps you a LOT: Stackoverflow)…

But just handing out links isn’t okey too… so:[/s]

Just noticed you don’t draw it really like you should… You should draw after translating and without calculating the x and y position for the vertices in the calls to glVertex2f

soooo:


int w = 50; // The width of the cube
int h = 50; // The height of the cube
glPushMatrix();
    // Translate:
    glTranslatef(-px, -py, 0); // Translate to the position of the cube
    // Rotate:
    glTranslatef(-w/2, -h/2, 0); // Translate even further to the middle of the cube
    glRotatef(angle, 0, 0, 1); // Rotate around z-axis by angle
    glTranslatef(w/2, h/2, 0); // Translate to the corner of the cube
    // Draw:
    glBegin(GL_QUADS);
        glVertex2f(0, 0);
        glVertex2f(w, 0);
        glVertex2f(w, h);
        glVertex2f(0, h);
    glEnd();
glPopMatrix();

Yes but isnt that going to draw the cube at -90x -90y, ive played about with the code you sent me and it just wont work

Sorry. this should be glTranslatef(px, py, 0);, right? Did you try that out?

Thanks, i dont know why i didnt try that, but you still made a mistake, it should translate like this :


        glTranslatef(w/2, h/2, 0);
        glRotatef(angle, 0, 0, 1);
        glTranslatef(-w/2, -h/2, 0);

not


        glTranslatef(-w/2, -h/2, 0);
        glRotatef(angle, 0, 0, 1);
        glTranslatef(w/2, h/2, 0);

Anyway thanks so much for your help, everythings working now and i greatly appreciate all responses :slight_smile:

Yup… just messed up with negating…

It was the translating for the viewport where you needed negation… messed that up :slight_smile: