OpenGL glutSwapBuffers不会绘制场景

时间:2013-12-04 19:05:54

标签: linux opengl

所以这真的很奇怪,我在ubuntu上,当我尝试运行这个示例程序时,我无法让窗口正常显示。如果我将它切换到glFlush并使用GLUT_SINGLE而不是GLUT_DOUBLE,那么它的工作原理非常好。出现这种情况的原因是什么?

#include <iostream>

#include <GL/glut.h>

using namespace std;

// Initialization routine.
void setup(void)
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glClearColor(1.0, 1.0, 1.0, 0.0); 
}

// Drawing routine.
void drawScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glLoadIdentity();
    gluLookAt(0,0,0,0,0,0,0,1,0);
    glColor3f(1,0,1);
    glutSolidCube(1);
    glutSwapBuffers();
    //glFlush();
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.0, (float)w/(float)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);

}

// Main routine.
int main(int argc, char **argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    //glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Test");
    setup();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(resize);
    glutMainLoop();

    return 0;
}

1 个答案:

答案 0 :(得分:1)

不要试图命运,每次通过显示回调重置你的矩阵:

#include <GL/glut.h>

void drawScene()
{
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluPerspective(60, w / h, 1.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(1,1,1,0,0,0,0,1,0);

    glColor3f(1,0,1);
    glutSolidCube(1);

    glutSwapBuffers();
}

int main(int argc, char **argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Test");
    glutDisplayFunc(drawScene);
    glutMainLoop();

    return 0;
}

此外,将gluLookAt()中的eyecenter点设置为同一位置为Bad(请参阅F和{{1的计算矢量)。