glutTimerFunc and FPS : Graphics
This is a discussion on glutTimerFunc and FPS within the Graphics forums in Theory and Concepts category; I'm just learning opengl but one thing that is bugging me is I keep running into glutTimerFuc being used to generate a specified FPS. Even if I set it to update at 1 FPS my cpu is being 100% consumed. Is glutTimerFunc implemented with busy spinning? What is the preferred way to control FPS? I guess I could always use my specific OS's timer utilities but is there any better way to do this with the opengl api? -- Zack...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| running into glutTimerFuc being used to generate a specified FPS. Even if I set it to update at 1 FPS my cpu is being 100% consumed. Is glutTimerFunc implemented with busy spinning? What is the preferred way to control FPS? I guess I could always use my specific OS's timer utilities but is there any better way to do this with the opengl api? -- Zack |
|
#2
| |||
| |||
| > I'm just learning opengl but one thing that is bugging me is I keep > running into glutTimerFuc being used to generate a specified FPS. Even > if I set it to update at 1 FPS my cpu is being 100% consumed. Is > glutTimerFunc implemented with busy spinning? What is the preferred way > to control FPS? I guess I could always use my specific OS's timer > utilities but is there any better way to do this with the opengl api? A timer function is not a good way to do this, because your code is locked down on a specific framrate. Normally fps measurement is done that way: GLUT provides a function, called glGet( GLUT_ELAPSED_TIME ). This function returns the number of milliseconds since the program started or glGet was called the first time. Let's say, you have a idleFunc(). This is a good place to put all stuff for fps measurement. The idle func would look like this: void idleFunc(){ static int lastUpdate = 0; static int frames = 0; char buf[20]; glutPostRedisplay(); // calls your display callback function glutSwapBuffers(); int currentTime = glutGet( GLUT_ELAPSED_TIME ); frames++; // is the time difference between lastUpdate and current time > one second ( 1000 ms )? if ( ( currentTime - lastUpdate ) >= 1000 ){ sprintf( buf, "FPS: %d", frames ); glutSetWindowTitle( buf ); frames = 0; lastUpdate = currentTime; } // you could also put a sleep here, to avoid glut consuming full processor time // relaxing things Sleep(1); } greets, benjamin |
|
#3
| |||
| |||
| Zack wrote: > I'm just learning opengl but one thing that is bugging me is I keep > running into glutTimerFuc being used to generate a specified FPS. Specified frames/sec is usually a bad way to do things (mainly because no matter how low you set it there's always somebody slower than that and the program will go crazy on their machine). > if I set it to update at 1 FPS my cpu is being 100% > consumed. Is glutTimerFunc implemented with busy spinning? You can put in a call to Sleep() in your idle function but you're only delaying the day when you figure out that timers are bad. > What is the preferred way to control FPS? Best way is not to do it. Much better to calculate elapsed time per frame and move things by that amount. > is there any better way to do this with the opengl api? OpenGL only does rendering. There's no timers or even "SwapBuffers" in OpenGL - they're OS functions. -- <\___/> / O O \ \_____/ FTB. For email, remove my socks. We’re judging how a candidate will handle a nuclear crisis by how well his staff creates campaign ads. It’s a completely nonsensical process. |
|
#4
| |||
| |||
| phibre wrote: > // you could also put a sleep here, to avoid glut consuming full processor time > // relaxing things > Sleep(1); Is there a way to throttle opengl's cpu usage w/o giving up my apps time slice like that. Say I want to make a lightweight opengl application that shows some basic animation say a stop watch that displays fractions of seconds. It also has some user interface (clicking the start and stop button). Also say this is a small app that shouldn't be taking up needless CPU time. -- Zack |

