Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: glutSwapBuffers seems not working

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    5

    Unhappy glutSwapBuffers seems not working

    Hello everyone, I met with a strangle problem in a demo program to lean double buffering of OpenGL. I specified GLUT_DOUBLE in glutInitDisplayMode and called glutSwapBuffers at the end of display callback. But the result is exactly the same without GLUT_DOUBLE and glutSwapBuffers, so what is wrong with my codes, could anyone help me out of this?

    Here is the code, spinning a square continuously on the screen:

    Code :
    #include <stdio.h>
    #include <GLUT/GLUT.h>
    #include <math.h>
     
    float theta = 0;
     
    void display ();
     
    void idle ();
     
    void mouse (int button, int state, int x, int y);
     
    int main (int argc, char** argv)
    {
        glutInit (&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE |GLUT_RGB);
        glutInitWindowPosition (100, 100);
        glutInitWindowSize (500, 500);
        glutCreateWindow ("Double Buffer Testing");
        glutDisplayFunc (display);
        glutIdleFunc(idle);
     
        glClearColor (1, 1, 1,1);
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        gluOrtho2D (-1, 1, -1, 1);
        glMatrixMode (GL_MODELVIEW);
     
        glutMainLoop();
        return 0;
    }
     
    void display ()
    {
        glClear (GL_COLOR_BUFFER_BIT);
        glColor3f (1, 0, 0);
     
        glBegin (GL_POLYGON);
        float thetar = theta * (3.14159 / 180.0);
        glVertex2f (cosf (thetar), sinf (thetar));
        glVertex2f (-sinf (thetar), cosf(thetar));
        glVertex2f (-cosf (thetar), -sinf (thetar));
        glVertex2f (sinf (thetar), -cosf (thetar));
        glEnd ();
     
        glutSwapBuffers();
     
    }
     
    void idle ()
    {
        theta += 2;
        if (theta > 360) theta -= 360;
        glutPostRedisplay ();
    }

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,125
    The rendered result looks great to me. Nice, stable, and framelocked with double-buffering (GLUT_DOUBLE).

    Note however that with single-buffering (GLUT_SINGLE), sync-to-vblank doesn't happen by default (i.e. glXSwapBuffers called under-the-hood is a no-op in this case, as there are no buffers to swap). So to generate a somewhat comparable rendered result with single-buffering, put this after your glutSwapBuffers() call:

    Code cpp:
        glFinish();
        usleep(15000);

    Now change back and forth between GLUT_DOUBLE and GLUT_SINGLE and the results should be fairly comparable. Notice that with single-buffering you see tearing artifacts, but not so with double-buffering (assuming you've got sync-to-vblank enabled).
    Last edited by Dark Photon; 09-26-2013 at 05:29 AM.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    5
    thanks for reply, I found the reason leading GL_DOUBLE didn't work, the v-sync is disabled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •