Double Buffering not Working OpenGL

Tag: opengl , doublebuffered Author: wag12345 Date: 2012-07-15

I wanted to make it double buffer, but it is still flickering. It is basically a modified version of the OpenNI UserTracker sample, which runs well on my computer, but I don't think I've deleted or significantly changed any of the important OpenGL commands to the point where they would be running differently. I am running Ubuntu 10.04 on my computer.

#include <GL/glut.h>
#include <GL/glx.h>
void DrawRectangle(float topLeftX, float topLeftY, float bottomRightX, float bottomRightY){
    GLfloat verts[8] = { topLeftX, topLeftY,
        topLeftX, bottomRightY,
        bottomRightX, bottomRightY,
        bottomRightX, topLeftY };
    glVertexPointer(2, GL_FLOAT, 0, verts);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void DrawTexture(float topLeftX, float topLeftY, float bottomRightX, float bottomRightY){
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
    DrawRectangle(topLeftX, topLeftY, bottomRightX, bottomRightY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void DrawLimb(KinectUserData player, int a, int b){
    glBegin(GL_LINES);
        glVertex3d((player.joints[a].x/500), (player.joints[a].y/400), 0);
        glVertex3d((player.joints[b].x/500), (player.joints[b].y/400), 0);
    glEnd();
}
void Draw(KinectData kdata){
    glColor4f(0.75,0.75,0.75,1);
    glEnable(GL_TEXTURE_2D);
    DrawTexture(320,240,0,0);   
    glDisable(GL_TEXTURE_2D);
    for (uint i = 0; i < kdata.size(); i++){
        glColor4f(1-Colors[kdata[i].getID()%nColors][0], 1-Colors[kdata[i].getID()%nColors][1], 1-Colors[kdata[i].getID()%nColors][2], 1);
        DrawLimb(kdata[i], 0, 1);
        DrawLimb(kdata[i], 1, 3);
        DrawLimb(kdata[i], 3, 4);
        DrawLimb(kdata[i], 4, 5);
        DrawLimb(kdata[i], 1, 6);
        DrawLimb(kdata[i], 6, 7);
        DrawLimb(kdata[i], 7, 8);
        DrawLimb(kdata[i], 3, 2);
        DrawLimb(kdata[i], 6, 2);
        DrawLimb(kdata[i], 2, 9);
        DrawLimb(kdata[i], 9, 10);
        DrawLimb(kdata[i], 10, 11);
        DrawLimb(kdata[i], 2, 12);
        DrawLimb(kdata[i], 12, 13);
        DrawLimb(kdata[i], 13, 14);
        DrawLimb(kdata[i], 9, 12);
    }
}
void glutDisplay(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_TEXTURE_2D);
    Draw(data); //data is correctly intialized in other functions
    glPopMatrix();
    glutSwapBuffers();
    glutPostRedisplay();
}
void glInit(int * pargc, char ** argv){
    glutInit(pargc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
    glutCreateWindow("Tracker");
    glutSetCursor(GLUT_CURSOR_NONE);
    glutDisplayFunc(glutDisplay);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}
int main(int argc, char** argv){
    glInit(&argc, argv);
    glutMainLoop();
}
Your glutDisplay function seems to be cut off, can you update your post with the full thing?
Sorry about that. It is now fixed.
I replaced the draw function with just a triangle, and it works for me... no flickering at all. If I remove the GLUT_DOUBLE and swap buffers calls, it goes back to flickering like crazy...
Why do you use glutDisplayFunc and glutPostRedisplay? You can give your glutDisplay function to glutIdleFunc and remove the glutPostRedisplay. Also avoid writing your own function names starting with glut! It gets confusing.
@Shahbaz: I just tried doing that, and it made the flickering even worse. Also, sorry about the names; I just didn't bother to change the names of the functions when I modified the existing code.

Other Answer1

I wanted to make it double buffer, but it is still flickering.

Those are separate issues. Your code is using double-buffering.

The flicker is most likely due to the lack of vertical synchronization. Unfortunately enabling/disabling vsync is very platform-specific.

comments:

I tried adding glXSwapInterval (or glXSwapIntervalSGI) to my code, but the compiler doesn't recognize the functions. What do I need to include? I already added "#include <GL/glx.h>", but it still doesn't recognize them.
I'd recommend GLEW for extension management.
Okay, I included glxew.h, and I now get an error when I call glXSwapIntervalSGI(1): undefined reference to __glewXSwapIntervalSGI. I found the line extern PFNGLXSWAPINTERVALSGIPROC __glewXSwapIntervalSGI; in glxew.h, but I don't know how to initialize it since I don't know what a PFNGLXSWAPINTERVALSGIPROC is. Do you have any advice?
I can also just make an uninitialized __glewXSwapIntervalSGI, but then I just get a segfault since it is called by glXSwapIntervalSGI.
Did you build GLEW from source or are you using the version in the Ubuntu repos? If you're using the repo version just link against GLEW via -lGLEW or maybe -lglew, not sure how's it capitalized.