# OpenGL 60FPS?

## Recommended Posts

Does OpenGL provide a timer or something which I can use to enforce a 60 fps speed for my game? At the moment it seems like it would run slower on slow computers and faster on fast ones.

##### Share on other sites
Nope. What other APIs are you using? Which platforms are you developing/releasing on?

##### Share on other sites
I'm using DevIL, Windows Platform SDK, and OpenGL currently. I also have OpenAL, but I'm not using it on my project currently. The only one of those which might have a timer is the WPSDK, but I can't find any documentation on it (although I haven't really looked). I'm currently developing on Windows, and don't actually plan to release this project (just a learning thing). I have a little experience with DirectX and I know this is possible, but I don't know how to do it.

On a semi-related matter, can someone point me to some windows sdk documentation? I could really use it.

##### Share on other sites
What you need to do is to temper the speed of motion by the FPS speed. That is, for all motion figure out how much it should be per second, then multiply that by 1 over FPS. Then take the resulting value as the motion to use.

// EXAMPLE Motion right on X, 2 units per second.
motionthisframe = 2 * (1/fps);

##### Share on other sites
That sounds like it would work, but how do I know what the current FPS is on the platform running the game? Obviously on a tricked - out gaming PC, the FPS will be higher than on someone's grandmother's old Pentium II. How do I get the exact FPS on the current computer?

##### Share on other sites
the second easiest (and most coarse) method is to:

a. Keep a counter that holds how many frames have been rendered.
b. For each second that passes, that number is your FPS. Copy it to somewhere useful. Render that useful number. Clear the original and the cycle starts over.

Obviously this doesn't take into account the fact that almost never will an exact second pass. Hence, the coarseness.

the first easiest method is to just use the reciprocal of the amount of time elapsed during a single frame.

But the average FPS is calculated in a similar manor to the following pseudo-code:

int frames_rendered = 0float frames_per_second = 0// put the current elapsed time into T1 to prevent possible whacked out first FPS recordfloat T1 = 0while (game_looping){   do_game_interval()   frames_rendered = frames_rendered + 1   float T2 = time_get_seconds()      if (T2 - T1 > 1.0)   {      frames_per_second = frames_rendered / (T2 - T1)      T1 = T2      frames_rendered = 0   }}

... god I hope that's right

I suggest using the reciprocal and the average methods at the same time to display to the user (or you).

Now, to use this with time-based movement, you're going to pretty much have to use the reciprocal method if you're not using fixed time step physics (which is recommended, but a tad harder).

So, get the time elapsed at the beginning of the frame (T1) and once again at the end (T2). This becomes your time step for the next frame. If this is in seconds, then all you do is multiply every velocity by the difference of T2 and T1.

Granted, it doesn't *have* to be in seconds per se, but it's the easiest to mentally associate with I find.

##### Share on other sites
That helps a lot, but I'm still confused. time_get_seconds() isn't the actual method to get the seconds, is it? Because if it is, I'm missing some library or something - it isn't working. Is the problem that I don't have some library included in my program, or was that just a pseudocode example? If so, what is the actual function to get the seconds? TIA.

##### Share on other sites
QueryPerformanceCounter for Windows only.

##### Share on other sites
int timeGetTime()

is fine... but win32

From MSDN :

Requirements

Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Mmsystem.h; include Windows.h.
Library: Use Winmm.lib.

##### Share on other sites
I went through MSDN and found what I wanted. Here is my fps code. It seems to work just fine. Thanks very much for all your help.

[SOURCE] 		SYSTEMTIME timer;		GetSystemTime(&timer);		float T1;		float T2 = timer.wSecond;		int FramesElapsed = 0;		float fps;			while(!done)									// Loop That Runs While done=FALSE	{		FramesElapsed++;		GetSystemTime(&timer);		T1 = timer.wSecond;		if (T1 >= T2)		{			fps = FramesElapsed;			FramesElapsed = 0;			GetSystemTime(&timer);			T2 = timer.wSecond;			T2++;			T1 = 0.0f;		}[/SOURCE]

##### Share on other sites
To keep const(units/sec) the speed that your objects are moving lets assume that you calculated FPS frames per second

double v = 0;
double GTC=0;
if(FPS>20)
{
v=(double)6*(double)((GetTickCount()-GTC))/1000);//6un/sec
i+=v;
GTC=GetTickCount();
}
else
{
i+=6;
GTC=GetTickCount();
}
the if is for the start of the game that frame rate is minimum.

DWORD GetTickCount(void);

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started(so the division by 1000). It is limited to the resolution of the system timer.

Declared in Winbase.h; include Windows.h.
Requires Kernel32.dll.

##### Share on other sites
GetTickCount() is good to use, but watch out that you don't rely on it incrementing. It wraps after 49.7 days, which means if you happen to be reading it at that time, it'll return 4,294,967,294, 4,294,967,295, 0, 1... Many programs fail because of timer wrapping like this.

##### Share on other sites
why not VSync ? Much better than the 60FPS cap...

Regards,
DarkProphet :)

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627686
• Total Posts
2978637
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 12
• 14
• 12
• 10
• 12