Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL glTranslatef/glRotatef not working as I expect

Tags:

opengl

I am trying to create a polygon and translate/rotate it when the mouse button is released. I am able to do this if I redraw my whole polygon again by using glBegin and glEnd but I am not sure if I really need this as I have already drawn the polygon on the screen once and just want to use the same object and apply rotation/translation to it.

I am putting a snippet below.

if(state == GLUT_UP){

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(90,0,0,1);
    //  glTranslatef(50,-50,0);
    /*
    glBegin(GL_POLYGON);
    glVertex2i (-40,40) ; //Specify li ne - se gme nt ge ometry .
    glVertex2i (-30 , -40) ;
    glVertex2i (0 , 20) ;
    glVertex2i (40 , 35) ;
    glEnd() ;
    */
    glClear(GL_COLOR_BUFFER_BIT);
    glPopMatrix();
    glutSwapBuffers();
    //  glutPostRedisplay();
}
like image 386
code4fun Avatar asked Dec 01 '25 02:12

code4fun


1 Answers

Please speak with me: "I'll never put OpenGL drawing calls into event handlers again!"

In a event handler you set some variable according to the event, then issue a redrawing call and in the rendering function you draw according to the content of the variables.

Update:

Also OpenGL does not "rememer" what you draw. You send drawing commands, OpenGL does its deed and then forgets about it. The only trace left are the fragments turned to pixels on the framebuffer. But the framebuffer contents are not affected by transformation or any OpenGL state changes. Only drawing affects the framebuffer.

like image 195
datenwolf Avatar answered Dec 02 '25 22:12

datenwolf