1

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();
}
user1469474
  • 23
  • 1
  • 7
  • Your glutDisplay function seems to be cut off, can you update your post with the full thing? – vmpstr Jul 31 '12 at 19:49
  • Sorry about that. It is now fixed. – user1469474 Jul 31 '12 at 20:07
  • 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... – vmpstr Jul 31 '12 at 20:30
  • Why do you use `glutDisplayFunc` and `glutPostRedisplay`? You can give your `glutDisplay` function to [`glutIdleFunc`](http://www.opengl.org/resources/libraries/glut/spec3/node63.html) and remove the `glutPostRedisplay`. Also avoid writing your own function names starting with `glut`! It gets confusing. – Shahbaz Aug 01 '12 at 14:13
  • @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. – user1469474 Aug 01 '12 at 14:27
  • Have you tried adding a sleep in your function? Something to change the (possible) rate of 1000 calls per second down to 60? Also, is your graphics card driver up-to-date? – Shahbaz Aug 01 '12 at 14:41
  • How do I add a sleep function? I have very limited knowledge about OpenGL. Would genpfault's answer be referring to the same thing? Also, I don't know how to tell if the graphics card is up to date, but I assume that it is. – user1469474 Aug 01 '12 at 14:57
  • Where is the corresponding `glPopMatrix()` for the `glPushMatrix()` in `glutDisplay()`? The matrix stack generally only has 32 slots. – genpfault Aug 01 '12 at 20:44
  • I just added that in, but it didn't have much of an effect. Also, the original code that I modified had only `glPushMatrix()` and not `glPopMatrix`, but it still ran smoothly. – user1469474 Aug 02 '12 at 13:51
  • In *nix, in `` there is a `usleep` function (in micro seconds). In Windows, there is a `Sleep` function (in milliseconds) which you should be able to find in MSDN. glut has timers that can be used which are more precise (but probably sleep here is easier to test with). Also, you can figure out your graphics card brand/model, go to its website and download the latest driver. – Shahbaz Aug 02 '12 at 13:57
  • Adding the sleep did make the flickering less prominent, but it is still there (unless I use a really high number and make it go extremely slowly). It is better than before, but do you have any other ideas? – user1469474 Aug 02 '12 at 14:10
  • Actually, the sleep is probably good enough. The flickering is still there, but it doesn't detract too much. – user1469474 Aug 02 '12 at 14:21

1 Answers1

1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 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 ", but it still doesn't recognize them. – user1469474 Aug 01 '12 at 14:09
  • 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? – user1469474 Aug 01 '12 at 15:52
  • I can also just make an uninitialized __glewXSwapIntervalSGI, but then I just get a segfault since it is called by glXSwapIntervalSGI. – user1469474 Aug 01 '12 at 16:00
  • 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. – genpfault Aug 01 '12 at 16:00
  • Also, don't forget to [initialize GLEW](http://glew.sourceforge.net/basic.html) before you start using the function pointers. – genpfault Aug 01 '12 at 16:02
  • Sorry, I don't have a huge amount of experience with programming, and I don't see anything from a quick google search. What is linking against? And I am using the repo version. – user1469474 Aug 01 '12 at 16:04
  • [Linking](http://en.wikipedia.org/wiki/Linker_%28computing%29). Something like `g++ main.cpp -o main -lGLEW -lGLU -lGL` will tell `ld` to link against GLEW, GLU, and GL after it's done compiling `main.cpp`. – genpfault Aug 01 '12 at 16:12
  • Actually, I was a bit hasty earlier. It does run now, but it is still flickering. Why do you think that it is not a problem with double buffering? – user1469474 Aug 01 '12 at 19:08
  • If double-buffering was messed up you wouldn't be seeing *anything* :) You need to get the OS to sync your buffer swaps with the vertical retrace so they don't occur mid-scanout. `glXSwapIntervalSGI()` isn't obeyed by all drivers. Which video card and driver are you using? – genpfault Aug 01 '12 at 19:43
  • I'm using a radeon card, and I was using `glXSwapIntervalMESA()`, not SGI. I was looking at a lot of sites though, and the ones that mentioned vsync were referring to fixing screen tearing, not flickering images. – user1469474 Aug 01 '12 at 20:16