问题
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 render some circles (known speed and coordinates) and I have already made the function that computes it's next position knowing the refresh rate.
I've tried to put my display callback in a timer callback but the program just freezed.
What can I do in order to run the display callback every x ms ?
回答1:
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;
}
来源:https://stackoverflow.com/questions/10321315/c-display-every-x-milliseconds-opengl