G
G
German2019-05-20 19:36:53
C++ / C#
German, 2019-05-20 19:36:53

How to limit FPS in OpenGL and glut?

I still can't figure out the problem of FPS limit, I need to set a fixed number, how can I do this? In the idle function , I have glutPostRedisplay() , and I need to set a limit there, but how?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2019-05-25
@mrjbom

The frame rate limit itself is very simple. Outside the idle function, you need to contain a variable with the time of the last call to the idle function, and in the function itself, you just need to accumulate the frame rate delta and call glutPostRedisplay.

double GetCurrentTime()
{
  using Duration = std::chrono::duration<double>;
  return std::chrono::duration_cast<Duration>( 
    std::chrono::high_resolution_clock::now().time_since_epoch() 
  ).count();
}

const double frame_delay = 1.0 / 60.0; // 60 FPS
double last_render = 0;
void OnIdle()
{
  const double current_time = GetCurrentTime();
  if( ( current_time - last_render ) > frame_delay )
  {
    last_render = current_time;
    glutPostRedisplay();
  }
}

This code is a highly simplified mechanism that only shows the general principle of frame rate limiting. It can be used where precise delta control between frames is not required. By the way, the representation of time in the type doublemakes it easier to find the delta between frames and control the delta error.
For a more precise control of the frame rate, it is worth referring, for example, to such material .

S
sddvxd, 2019-05-20
@sddvxd

If you need 60 FPS, then get the current time, divide the second by 60 and update the frame every 1/60 second

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question