即使在 openGL 窗口关闭后继续程序

标签 c opengl glut freeglut

我不断调用 glutMainLoopEvent 来处理图形。但是,在有人关闭窗口后,我想退出循环并显示 Code reached here. 。似乎当一个窗口关闭时,一个 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 允许您的程序在窗口关闭时继续。

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

关于即使在 openGL 窗口关闭后继续程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420304/

相关文章:

c - 如何在 switch 语句中连续打印输出?

c - 程序得到 101 个数字而不是 100

c++ - -fno-strict-aliasing 的性能影响

c++ - 使用 GLee(GL Easy Extension Library)构建错误

c++ - ZZ坐标不匹配

c++ - 管理 Windows XP 和 Vista/Server 2008 之间的 Windows API 差异

c++ - 从平面方程绘制任意平面,OpenGL

c++ - 如何使用 GLM 设置 OpenGL/C++ 项目?

c++ - OpenCV IplImage *图像到 OpenGL GLuint 纹理

c++ - 将 Circle 旋转特定角度(openGL)