即使在 openGL window 关闭后继续执行程序

Continue program even after openGL window closed

我一直调用glutMainLoopEvent处理图形。但是,在有人关闭 window 之后,我想退出循环并显示 Code reached here. 。似乎当 window 关闭时,调用 exit 函数并且整个应用程序停止。虽然我需要应用程序继续。我应该如何修复代码?

#include <stdio.h>
#include <GL/freeglut.h>

//display function - draws a triangle rotating about the origin
void cback_render()
{
    //keeps track of rotations
    static float rotations = 0;

    //OpenGL stuff for triangle
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glRotatef(rotations, 0, 0, 1);

    glBegin(GL_TRIANGLES);
        glVertex3f(0,0,0);
        glVertex3f(1,0,0);
        glVertex3f(0,1,0);
    glEnd();

    //display on screen
    glutSwapBuffers();

    //rotate triangle a little bit, wrapping around at 360°
    if (++rotations > 360) rotations -= 360;
}

void timer(int value )
{
    glutPostRedisplay();
    glutMainLoopEvent();
    glutTimerFunc(30, timer, 1);
}

int main(int argc, char **argv)
{
    //initialisations
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(512, 512);

    //create window and register display callback
    glutCreateWindow("freegluttest");
    glutDisplayFunc (cback_render);
    glutTimerFunc(30, timer, 1);

    //loop forever
    long i=0;
    while(1)
    {
        printf("[%ld]\n",i);
        i++;
        glutMainLoopEvent();
    }
    printf("Code reached here.");

    return 0;
}

使用 GLUT_ACTION_ON_WINDOW_CLOSE 允许您的程序在 window 关闭时继续。

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
              GLUT_ACTION_GLUTMAINLOOP_RETURNS);

来源:

http://www.lighthouse3d.com/cg-topics/glut-and-freeglut/ http://freeglut.sourceforge.net/docs/api.php