GLUT只运行一次显示回调并且不输出到终端

时间:2016-07-27 22:22:30

标签: c++ opengl glut

我的GLUT计划表现非常出乎意料。它所做的一切(它应该做的全部)都是清晰的屏幕蓝色并画出一个向后移动的紫色三角形。

首先,这是我的代码:

#include <GL/glut.h>
#include <iostream>

class Ground{
private:
    float zpos;
public:
    Ground(){
        zpos = -5.0f;
    }
    void Render(){
        glColor3f(1.0,0.0,1.0);
        glBegin(GL_TRIANGLES);
        glVertex3f(0,0,zpos);
        glVertex3f(0,1,zpos);
        glVertex3f(1,1,zpos);
        glEnd();
        zpos -= 0.1f;
    }
};

class Skybox{
public:
    Skybox(){

    }
    void Render(){
        glClearColor(0.5, 0.8, 1.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
    }
};

Skybox s;
Ground g;
void init(void) 
{
    s = Skybox();
    g = Ground();
    glShadeModel (GL_SMOOTH);
    glMatrixMode(GL_MODELVIEW);
    glDepthFunc(GL_LEQUAL);
    glViewport(0,0,glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,glutGet(GLUT_SCREEN_WIDTH)/glutGet(GLUT_SCREEN_HEIGHT),0.1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display(void)
{
    std::cout << "Hi";
    glClearColor(0,0,0,0);
    glClearDepth(1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    s.Render();
    g.Render();
    glLoadIdentity();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize( 800, 600 ); 
    glutCreateWindow ("Umm");
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;
}

然而,代码无法按预期工作 - 紫色三角形不会退回到距离中,它会保持静止,简单的cout甚至不会做任何事情! (但不知何故,显示功能之后的所有代码都被执行,但只执行一次?)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

添加计时器回调以反复发布重新显示:

void timer( int value )
{
    glutPostRedisplay();
    glutTimerFunc( 16, timer, 0 );
}

否则display()仅在操作系统感觉到时才会被调用,通常是after an expose/damage/paint event (like a window resize, or exposing more of the window by moving another window off of it)

所有在一起:

#include <GL/glut.h>
#include <iostream>

class Ground{
private:
    float zpos;
public:
    Ground(){
        zpos = -5.0f;
    }
    void Render(){
        glColor3f(1.0,0.0,1.0);
        glBegin(GL_TRIANGLES);
        glVertex3f(0,0,zpos);
        glVertex3f(0,1,zpos);
        glVertex3f(1,1,zpos);
        glEnd();
        zpos -= 0.1f;
    }
};

class Skybox{
public:
    Skybox(){

    }
    void Render(){
        glClearColor(0.5, 0.8, 1.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
    }
};

Skybox s;
Ground g;
void init(void) 
{
    s = Skybox();
    g = Ground();
    glShadeModel (GL_SMOOTH);
    glMatrixMode(GL_MODELVIEW);
    glDepthFunc(GL_LEQUAL);
    glViewport(0,0,glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,glutGet(GLUT_SCREEN_WIDTH)/glutGet(GLUT_SCREEN_HEIGHT),0.1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display(void)
{
    std::cout << "Hi";
    glClearColor(0,0,0,0);
    glClearDepth(1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    s.Render();
    g.Render();
    glLoadIdentity();
    glutSwapBuffers();
}

void timer( int value )
{
    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize( 800, 600 ); 
    glutCreateWindow ("Umm");
    init ();
    glutTimerFunc( 0, timer, 0 );
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;
}