C : display every x milliseconds (OpenGL)

后端 未结 1 1209
梦如初夏
梦如初夏 2021-01-25 17:44

I\'ve got a C / C++ (only main file is .cpp so I can use OpenGL) program, I use OpenGL (GLUT, GLUI) in it. It already displays something but I want it to move every x ms. I rend

相关标签:
1条回答
  • 2021-01-25 18:07

    I've tried to put my display callback in a timer callback but the program just freezed.

    Make sure to re-arm the timer in your timer callback otherwise it will only fire once:

    #include <GL/glut.h>
    
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-10, 10, -10, 10, -1, 1);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        static float angle = 0;
        angle += 1.0f;
        glRotatef(angle, 0, 0, 1);
    
        glColor3ub(255,0,0);
        glBegin(GL_TRIANGLES);
        glVertex2f(0,0);
        glVertex2f(10,0);
        glVertex2f(10,10);
        glEnd();
    
        glutSwapBuffers();
    }
    
    void reshape(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
    
    void timer(int extra)
    {
        glutPostRedisplay();
        glutTimerFunc(30, timer, 0);
    }
    
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitWindowSize(640,480);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
        glutCreateWindow("Timer");
    
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutTimerFunc(0, timer, 0);
        glutMainLoop();
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题