Reputation: 3010
Dear all I am trying to create animation using OpenGL through glutIdleFunc()
. Below is my code:
float t = 0.0;
void idle (void)
{
t += 0.1;
if (t > 2*pi)
{
t = 0.0;
}
glutPostRedisplay();
}
//in main function
glutIdleFunc(idle);
I have been trying to adjust the increment of t in order to slow down my animation. But somehow my animation keeps moving on too fast, until I can't catch it with my eye. Does anyone know how to slow down this kind of animation? Thank's
Upvotes: 1
Views: 4700
Reputation: 29055
Rather than trying to find an artificial t
value to use in your idle function, you'll probably be better off using a real timer such as C's time()
. Then, simply advance your animation by the appropriate amount given the elapsed time since the last frame was drawn.
Here's how it might look:
time_t lastTime;
void draw() {
const time_t now = time();
const double dt_s = difftime(now, lastTime);
// Update your frame based on the elapsed time. For example, update an angle
// based on a specified rotation rate (omega_deg_s):
const double omega_deg_s = 10.0;
angle += dt_s * omega_deg_s;
angle = fmod(angle, 360.0);
// Now draw something based on the new angle info:
draw_my_scene(angle);
// Record current time for next time:
lastTime = now;
}
Upvotes: 2
Reputation: 1730
You need to use the time since the last function call rather than a straight value as your metric, since that time may vary.
For more information, read valkea's answer on GameDev, which suggests that you use glutGet(GLUT_ELAPSED_TIME)
to calculate that value.
Upvotes: 2