Need some help with glutTimerFunc

Hi

Ok ive simulated a solar system with the planets and moons etc… orbiting the sun. The problem is it goes way to fast, i don’t want to solve this by changing variables thouh. I’d like some kind of frame limiter so that it works the same on any system…

Ive tried using glutTimerFunc but i can’t seem to get it working right…

Where should I actually be calling this function

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800 , 600);
glutInitWindowPosition(10, 100);
glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
glutMouseFunc(mouse);
glutMainLoop();
return 0;

}

this is my main. I have a Init function with all my vertex arrays etc… and all the animation happens in the render function…

where should i actually be placing the glutTimerFunc to ensure it slows my animation to 60fps. I know ill have to create a timer function, and call it in the main but then what…

Thanks in advance

First you call glutTimerFunc with callback function and callback time.

glutTimerFunc(1000.0/60.0, timer, 0);

Then you make your timer callback where you do all the necessary stuff like updating animations and then ask for a redisplay. You also have to start a new timer if you want to keep updating. Something like this.

void timer(int)
{
/* update animation */
glutPostRedisplay();
glutTimerFunc(1000.0/60.0, timer, 0);
}

Thanks that worked just great

Hi !
I haven’t understand what is the third parameter of GluttimerFunc(time,clbk,??). what is the use of this value.
Mao

Say you start two or more times with the same callback function, how do you know which timer triggered the callback? You use an ID which is passed to the callback to identify which timer triggered it.

Thank u !
And where it is the best place to put this timer-callfunction for an animation and having the precise time that we want ? The main function ? the reshape function ? the Idle function ? or the gldraw function ?
Shingouki has put this in the main, and what happened when its in the idle one ?
Do u know some examples about it ?
Mao

You need to look at some example code, this will give you a better idea. See how they run and look at the code to see how functions works. Look at some of my code posted on my website and run the programs all written with glut and have comments: www.angelfire.com/linux/nexusone

On the glutTimer: You can set the gluttimerfunc call back about anywhere also glutidlefunc.

Let say’s you want animation running all the time.

glutTimerFunc( 100, My_timer_func, 0);
glutIdleFunc( My_idle_func );
glutMainLoop();
} // End of main routine, nothing is call again in main after glutmainloop is called.

examples of both

void My_idle_func(void)
{
// Do something when glut is Idle
glutPostRedisplay();
}

void My_timer_func( int t )
{
// Do something every X milliseconds

glutPostRedisplay();
glutTimerFunc(100, My_timer_func, 0);
}

Now you can control the animation by user input from the keyboad

int animation_state; // set state with key press.

// in glut keyboard call back
if (key == “a” )
{
animation_state = 1;
glutTimerFunc( 100, My_timer_func, 0);
}
if (key == “b” ) animation_state = 0;

// Place in My_timer_func
// This will make the time loop every 100 ms until the animation_state becomes zero.

if ( animation_state ) glutTimerFunc( 100, My_timer_func, 0);

Originally posted by Mao:
Thank u !
And where it is the best place to put this timer-callfunction for an animation and having the precise time that we want ? The main function ? the reshape function ? the Idle function ? or the gldraw function ?
Shingouki has put this in the main, and what happened when its in the idle one ?
Do u know some examples about it ?
Mao

[This message has been edited by nexusone (edited 05-08-2003).]

[This message has been edited by nexusone (edited 05-08-2003).]

[This message has been edited by nexusone (edited 05-08-2003).]

Thank U Nexusone !