jason
jason

Reputation: 7164

OpenGL program shows only what is behind the screen

When I run my OpenGL program, if there is currently a text editor open, the opengl program shows that portion in its screen. Or a web browser is open, opengl program shows that part of the browser. Here is my main function :

int main(int argc, char** argv)
{
    glutInit(&argc, argv);  
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("OpenGL Program");

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;
} 

my display function :

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
    glLoadIdentity();

    gluLookAt(posz,posy,posz, 0, 0, 0, upx, upy, upz);
        glBegin(GL_TRIANGLES);
      glVertex3f(-1.0f,-0.25f,0.0f);
      glVertex3f(-0.5f,-0.25f,0.0f);
      glVertex3f(-0.75f,0.25f,0.0f);

    glEnd();
}

It doesn't give any error or warning, it just displays what is already on the screen. What am I missing? Thanks.

Upvotes: 1

Views: 614

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

glutInitDisplayMode (... GLUT_DOUBLE) tells the window system to use double-buffered rendering, but you have no swap buffer calls anywhere in your code.

Basically, you always draw into the back-buffer when this happens and the window system only paints its windows using the contents of the front-buffer. The idea there is that the front-buffer is always a finished image and it works great when you actually swap buffers after you finish drawing your frame. But as it stands right now, your front-buffer is always undefined.

To fix this, add a call to glutSwapBuffers () at the end of void display (). That will copy/exchange your back-buffer and the front-buffer so your rendered output is visible.

Upvotes: 5

Related Questions