Effective Time-Keeper

Hi,
I am writing a driving simulation program using GLUT. where in I need 2 write an effective time keeper. would u guess suggest me a good time-keeper using glutTimerFunc for 30 fps.


regards

Originally posted by ajmrm:
[b]Hi,
I am writing a driving simulation program using GLUT. where in I need 2 write an effective time keeper. would u guess suggest me a good time-keeper using glutTimerFunc for 30 fps.


regards[/b]

GLUT timers can be somewhat off in terms of accuracy, especially when your render loop takes longer than the timeout you request.

For a 30 fps timer, try glutTimerFunc timeout value of ~ 30ms. Also, remember that glut timers are not recuring, so in your timer callback, you have to reset the timer every time.

For more advanced, and accurate timing you might take a look at some of the GLUT replacement toolkits available. For Win32, I’d highly recommend Cpw.

Here’s a toolkits FAQ: http://www.mathies.com/glfaq/

Regards,
Jim

[This message has been edited by jmathies (edited 11-29-2002).]

Hi,
Actually I am calling a func. FeedWater() to feed water in every frame and i restrict to 30fps. I have a loop in the render function from frame 1 to 30 and calling this FeedWater() func @ 33ms.

Now the problem is once the control goes to the FeedWater() func, I have to create a timer to be there inside that function for 33ms. Here I need to do some operation.

Listing…
RenderScene()
{
for(frame=1;frame<=30;frame++)
{

time = frame * (1/30);
FeedWater(time);

OutputWater(time);
}

}

FeedWater(time)
{
start_time = GetTickCount();
time_len = time * 1000;
final_time = time_length - final_time;
while((GetTickCount() - start_time) < final_time)
{
TimerFunc(final_time);


}
}

TimerFunc(int value)
{
r1 = rand()%50;
c1 = rand()%75;
glutPostRedisplay();
glutTimerFunc(value, TimerFunc, 1);
}

The above listing code is not working fine… pls suggest and help me dude.

thanx
regards

Hello!

I see you are using GetTickCount(), which is a Windows specific function (not GLUT and not portable). It also happens to be one of the worst timer sources in Windows (in terms of resolution). The accuracy of GetTickCount() is usually worse than 10 ms, sometimes as bad as 50 ms as I have heard. Since 30 fps means 33 ms per frame, your timer source is not accurate enough (you will often get the same time for two different frames => problems).

One of the best, portable, timers around (in my opinion) is the one in GLFW , which normally has a resolution better than 1 us. It automatically choses the best timer source for the target platform (e.g. RDTSC CPU instruction for Pentium-class x86 systems, free running hardware counter @ > 1 MHz for SGI stations, etc).