Objectmix
Tags Register Mark Forums Read

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...


Object Mix > Theory and Concepts > Graphics > glutTimerFunc and FPS

Reply

 

LinkBack Thread Tools
  #1  
Old 03-15-2007, 09:25 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default glutTimerFunc and FPS

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
  #2  
Old 03-16-2007, 03:38 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: glutTimerFunc and FPS

> 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  
Old 03-16-2007, 06:03 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: glutTimerFunc and FPS

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  
Old 03-16-2007, 09:33 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: glutTimerFunc and FPS

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
Reply

Thread Tools



All times are GMT -5. The time now is 08:38 AM.

Managed by Infnx Pvt Ltd.