双缓冲不工作OpenGL

时间:2012-07-31 19:42:10

标签: opengl doublebuffered

我想让它成为双缓冲区,但它仍然是闪烁的。它基本上是OpenNI UserTracker示例的修改版本,它在我的计算机上运行良好,但我认为我没有删除或显着改变任何重要的OpenGL命令,以至于它们将以不同的方式运行。我在我的电脑上运行Ubuntu 10.04。

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

1 个答案:

答案 0 :(得分:1)

  

我想让它成为双缓冲区,但它仍然在闪烁。

这些是不同的问题。您的代码 使用双缓冲。

闪烁很可能是由于缺乏垂直同步。不幸的是,启用/禁用vsync是very platform-specific