-6

这是代码,没有任何错误,它有什么问题?

我编译它,命令提示符打开,窗口打开,窗口全是白色的,我将它重新着色为灰色..,它也没有绘制我的形状,那是什么问题?

    #ifdef __APPLE__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif

    #include <stdlib.h>



    void ProcessSpecialKeys(int key, int x, int y){

        switch(key){

           case GLUT_KEY_RIGHT:


    exit(0);



           case GLUT_KEY_LEFT:

    exit(0);

           case GLUT_KEY_UP:
    exit(0);

           case GLUT_KEY_DOWN:

    exit(0);
           default:

    exit(0);

        }




        }



    void renderScene(void) {
    glClearColor(0.3f, 0.3f, 0.3f, 0.3f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        glutSwapBuffers();
}

    void renderPrimitive(void){
    glBegin(GL_QUADS);
    glVertex3f(-1.0f,-1.0f,0.0f);
    glVertex3f(-1.0f,1.0f,0.0f);
    glVertex3f(1.0f,1.0f,0.0f);
    glVertex3f(1.0f,-1.0f,0.0f);
    glEnd();
}

    void display(void){
    glTranslatef(0.0f,0.0f,-0.5f);
    renderPrimitive();
    glFlush();
    glClearColor(0.3f,0.3f,0.3f,0.3f);
    glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Dimension");
    glutDisplayFunc(display);

    glLoadIdentity();

glutSpecialFunc(ProcessSpecialKeys);


    glutMainLoop();

    return 1;
}
4

1 回答 1

1

问题是您在显示功能中绘制场景后正在清除显示。此外,还有一个无用的glFlush()电话。删除这些行:

glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);

添加 aglutSwapBuffers()而不是它们。

glFlush()用于单缓冲模式。

请记住,您使用的是双缓冲模式,因此有两个缓冲区。glutSwapBuffers()您的场景被绘制到后台缓冲区,并且要显示它,您必须通过在显示函数结束时调用将其复制到前台缓冲区。

于 2013-07-18T17:43:05.727 回答