PDA

View Full Version : glutSwapBuffers seems not working



xiewneqi
09-26-2013, 02:59 AM
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:


#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 ();
}

Dark Photon
09-26-2013, 05:01 AM
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:


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).

xiewneqi
09-26-2013, 07:06 AM
thanks for reply, I found the reason leading GL_DOUBLE didn't work, the v-sync is disabled.