How do you make sure that the OpenGL animation speed is consistent across machines?

How do you control the speed of the animation? My objects expect faster on another machine.

void idle(void){ if (!wantPause){ circleSpin = circleSpin + 2.0; //spin circles if(circleSpin > 360.0) { circleSpin = circleSpin - 360.0; } diamondSpin = diamondSpin - 4.0; //spin diamonds if(diamondSpin > 360.0) { diamondSpin = diamondSpin + 360.0; } ellipseScale = ellipseScale + 0.1; //scale ellipse if(ellipseScale > 30) { ellipseScale = 15; } glutPostRedisplay(); } } void drawScene() { ... glColor3f(1,0,0); glPushMatrix(); glRotatef(circleSpin,0,0,1); drawOuterCircles(); glPopMatrix(); } int main (int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(400,400); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Lesson 6"); init(); glutDisplayFunc(drawScene); glutKeyboardFunc(keyboard); glutReshapeFunc(handleResize); glutIdleFunc(idle); glutMainLoop(); return 0; } 
+6
animation opengl
source share
1 answer

This is a poor man’s decision:

 FPS = 60.0; while (game_loop) { int t = getticks(); if ((t - t_prev) > 1000/FPS) process_animation_tick(); t_prev = t; } 

this is the best solution:

 GAME_SPEED = ... while (game_loop) { int t = getticks(); process_animation((t - t_prev)*GAME_SPEED/1000.0); t_prev = t; } 

In the first, getframe moves your object by a fixed amount, but this is error prone if the frame rate drops.

In the latter case, you move objects depending on the elapsed time. For example. if the gap is 20 ms, you rotate the object by 12 degrees, and if 10 ms pass, you rotate it by 6 degrees. In general, animation if the function of time has passed.

The implementation of getticks() up to you. For starters, you can use glutGet(GLUT_ELAPSED_TIME) .


In your case, it will look something like this:

 int old_t; void idle(void) { int t = glutGet(GLUT_ELAPSED_TIME); int passed = t - old_t; old_t = t; animate( passed ); glutPostRedisplay(); } void animate( int ms ) { if (!wantPause){ circleSpin = circleSpin + ms*0.01; //spin circles if(circleSpin > 360.0) { circleSpin = circleSpin - 360.0; } diamondSpin = diamondSpin - ms*0.02; //spin diamonds if(diamondSpin > 360.0) { diamondSpin = diamondSpin - 360.0; } ellipseScale = ellipseScale + ms*0.001; //scale ellipse if(ellipseScale > 30) { ellipseScale = 15; } } } 
+10
source share

All Articles