Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting max frames per second in openGL

Is there any way to calculate how much updates should be made to reach desired frame rate, NOT system specific? I found that for windows, but I would like to know if something like this exists in openGL itself. It should be some sort of timer.

Or how else can I prevent FPS to drop or raise dramatically? For this time I'm testing it on drawing big number of vertices in line, and using fraps I can see frame rate to go from 400 to 200 fps with evident slowing down of drawing it.

like image 814
Raven Avatar asked Jul 20 '10 22:07

Raven


2 Answers

Is there any way to calculate how much updates should be made to reach desired frame rate, NOT system specific?

No.

There is no way to precisely calculate how many updates should be called to reach desired framerate.

However, you can measure how much time has passed since last frame, calculate current framerate according to it, compare it with desired framerate, then introduce a bit of Sleeping to reduce current framerate to the desired value. Not a precise solution, but it will work.

I found that for windows, but I would like to know if something like this exists in openGL itself. It should be some sort of timer.

OpenGL is concerned only about rendering stuff, and has nothing to do with timers. Also, using windows timers isn't a good idea. Use QueryPerformanceCounter, GetTickCount or SDL_GetTicks to measure how much time has passed, and sleep to reach desired framerate.

Or how else can I prevent FPS to drop or raise dramatically?

You prevent FPS from raising by sleeping.

As for preventing FPS from dropping...

It is insanely broad topic. Let's see. It goes something like this: use Vertex buffer objects or display lists, profile application, do not use insanely big textures, do not use too much alpha-blending, avoid "RAW" OpenGL (glVertex3f), do not render invisible objects (even if no polygons are being drawn, processing them takes time), consider learning about BSPs or OCTrees for rendering complex scenes, in parametric surfaces and curves, do not needlessly use too many primitives (if you'll render a circle using one million polygons, nobody will notice the difference), disable vsync. In short - reduce to absolute possible minimum number of rendering calls, number of rendered polygons, number of rendered pixels, number of texels read, read every available performance documentation from NVidia, and you should get a performance boost.

like image 119
SigTerm Avatar answered Sep 19 '22 06:09

SigTerm


You have two different ways to solve this problem:

  1. Suppose that you have a variable called maximum_fps, which contains for the maximum number of frames you want to display.

    Then You measure the amount of time spent on the last frame (a timer will do)

    Now suppose that you said that you wanted a maximum of 60FPS on your application. Then you want that the time measured be no lower than 1/60. If the time measured s lower, then you call sleep() to reach the amount of time left for a frame.

  2. Or you can have a variable called tick, that contains the current "game time" of the application. With the same timer, you will incremented it at each main loop of your application. Then, on your drawing routines you calculate the positions based on the tick var, since it contains the current time of the application.

    The big advantage of option 2 is that your application will be much easier to debug, since you can play around with the tick variable, go forward and back in time whenever you want. This is a big plus.

like image 26
Edison Gustavo Muenz Avatar answered Sep 20 '22 06:09

Edison Gustavo Muenz