Reduce number of FPS

feanor91

28-08-2008 10:38:19

Hi guys

I almost do what I want to get Ogre running in my project, but there is one things left to do....

My render run under very high rate FPS on my computer (600 and more) and it's not of a recent generation (Core 2 Duo 1.8Ghz and GeForce 7600 pro)...So I think it will be very much faster on more recent computer (my scene is very tiny).

What I want is reduc FPS number to something lik 50, maximum 100...

Question : how can achieve that? In fact I think I have to do something like geting average fps in my render loop (I use render one frame, but see my class to understand what I do) But how can I calculate and implement a sort of loop to reduce FPS number?

Beauty

28-08-2008 20:08:04

For each frame you can stop the rendering thread for the needed time.

Now I wrote a code just for you.

I made a test:
Without this break I have 800 fps in my test application.
Using this code it is reduced to 65 fps.
Good would be to get the frequency of monitor or a little bit more (e.g. 80 fps).

It's not exactly the wanted value of 50 fps.
This is related to the minimum precision of 1ms for Thread.Sleep() method and the deviants will be added up. (I suppose this is the reason)


Add this to the method FrameStarted(FrameEvent evt)
if (fps == 0)
{
//-- initialize at first run --
fps = 50; // set wanted fps
frameTime = new TimeSpan(TimeSpan.TicksPerSecond / fps); // wanted time for one frame
lastFrameTime = new TimeSpan(DateTime.Now.Ticks);
}

timeSinceLastFrame = new TimeSpan(DateTime.Now.Ticks - lastFrameTime.Ticks);
lastFrameTime = new TimeSpan(DateTime.Now.Ticks);
if (timeSinceLastFrame < frameTime)
// wait if needed
System.Threading.Thread.Sleep(frameTime - timeSinceLastFrame);


And add this to the class where FrameStarted() is inside:
TimeSpan frameTime, lastFrameTime, timeSinceLastFrame;
Int16 fps;

feanor91

28-08-2008 20:40:30

Thanks a lot Beauty, I will test this tomorow.

Beauty

28-08-2008 21:27:18

By testing I found out a strange thing.
This could be the real reason why it works incorrect.
Especially with higher fps values.
Without using Thread.Sleep() I get a strange result for the time spans between frames:
(the values are in milliseconds)

0 62,5 15,625 0 0 0 0 0 0 0 0 0 0 15,625 0 0 0 0 0 0 0 0 0 0 15,625 0 0 0 0 0 0 0 0 0 0 15,625 31,25 0 0 0 0 0 0 0 0 0 0 0 15,625 0 31,25 0 0 0 0 0 0 0 0 0 0 15,625 0 0 0 0 0 0 0 0 0 15,625 0 0 15,625 0 0 0 0 0 0 0 0 0 0 0 15,625 0 0 0 0 0 0 15,625 0 0 0 0 0 0


Can you test it with your application, too?

Use this code for testing:
(and look to the output window)

In the class:
Int16 fps, counter;
System.Text.StringBuilder test = new System.Text.StringBuilder(1000);


In the method FrameStarted(FrameEvent evt)
if (fps == 0)
//-- initialize at first run --
{
fps = 85; // set wanted fps
frameTime = new TimeSpan(TimeSpan.TicksPerSecond / fps); // wanted time for one frame
lastFrameTime = new TimeSpan(DateTime.Now.Ticks);
}

timeSinceLastFrame = new TimeSpan(DateTime.Now.Ticks - lastFrameTime.Ticks);
lastFrameTime = new TimeSpan(DateTime.Now.Ticks);

if (counter++ < 100)
test.Append(" " + timeSinceLastFrame.TotalMilliseconds.ToString());
if (counter == 100)
Console.WriteLine(test.ToString());

feanor91

29-08-2008 05:41:43

Euh...I will try...I must first implement into my VB class then try to use console with it.

Regards.

feanor91

29-08-2008 07:31:38

Hi

Here's my code :

in class :

Private FrameTime As New System.TimeSpan
Private LastFrameTime As New System.TimeSpan
Private TimeSinceLastFrames As New System.TimeSpan
Private FPSMax As Integer
Private Counter As Integer
Private test As System.Text.StringBuilder = New System.Text.StringBuilder(1000)
Private _txtCtrl As System.Windows.Forms.RichTextBox


in framestarted event:


If fps = 0 Then
'-- initialize at first run --

FPSMax = 85 ' set wanted fps
FrameTime = New TimeSpan(TimeSpan.TicksPerSecond / FPSMax) ' wanted time for one frame
LastFrameTime = New TimeSpan(DateTime.Now.Ticks)


End If

TimeSinceLastFrames = New TimeSpan(DateTime.Now.Ticks - LastFrameTime.Ticks)
LastFrameTime = New TimeSpan(DateTime.Now.Ticks)

Counter += 1
If Counter < 100 Then
test.Append(" " + TimeSinceLastFrames.TotalMilliseconds.ToString())
End If
If Counter = 100 Then
_txtCtrl.Text += test.ToString '(_txtCtrl is a richtextbox control)
'Console.WriteLine(test.ToString())
End If


And the results :

0 140,625 0 15,625 0 0 0 15,625 0 0 0 0 15,625 0 0 0 0 62,5 0 0 15,625 15,625 0 0 15,625 0 15,625 15,625 0 0 15,625 15,625 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 0 15,625 0 0 0 0 15,625 0 0


so what?

Edit :

To try to understand, I change the code in framstarted events to put every timesincelastframe in my richtextbox, then wite all to a file and I get this :

0 78,125 15,625 0 15,625 0 0 15,625 0 0 15,625 31,25 0 15,625 0 15,625 0 31,25 0 0 0 15,625 0 46,875 0 0 31,25 0 0 0 15,625 31,25 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 15,625 0 0 0 15,625 0 0 0 15,625 0 0 15,625 0 0 0 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 453,125 0 15,625 0 0 0 15,625 0 0 0 15,625 109,375 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 78,125 15,625 31,25 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 11562,5 15,625 0 15,625 0 15,625 0 15,625 0 15,625 0 15,625 0 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 31,25 140,625 0 15,625 15,625 0 15,625 0 15,625 0 15,625 15,625 0 15,625 0 15,625 0 15,625 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 31,25 171,875 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 15,625 31,25 15,625 31,25 31,25 31,25 15,625 31,25 0 46,875 15,625 15,625 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 0 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 140,625 15,625 0 31,25 15,625 15,625 0 15,625 0 15,625 0 15,625 15,625 0 15,625 0 15,625 0 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 140,625 15,625 0 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 62,5 31,25 0 15,625 31,25 15,625 31,25 0 46,875 0 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 0 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 140,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 62,5 0 15,625 15,625 328,125 15,625 15,625 15,625 15,625 0 15,625 46,875 15,625 0 15,625 15,625 0 15,625 15,625 78,125 15,625 0 15,625 31,25 15,625 0 15,625 15,625 31,25 15,625 15,625 31,25 0 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 109,375 0 15,625 0 15,625 0 62,5 0 15,625 0 15,625 15,625 0 15,625 0 15,625 62,5 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 140,625 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 125 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 125 15,625 15,625 0 46,875 15,625 15,625 31,25 46,875 0 15,625 31,25 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 0 125 31,25 0 31,25 15,625 31,25 15,625 15,625 31,25 0 31,25 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 0 15,625 15,625 15,625 0 15,625 15,625 15,625 0 31,25 0 15,625 109,375 31,25 31,25 15,625 31,25 62,5 15,625 0 15,625 15,625 31,25 0 31,25 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 31,25 140,625 46,875 15,625 0 15,625 15,625 0 15,625 109,375 62,5 31,25 31,25 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 46,875 15,625 31,25 15,625 31,25 0 31,25 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 125 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 46,875 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 78,125 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 109,375 15,625 15,625 15,625 0 15,625 15,625 0 62,5 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 62,5 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 31,25 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 15,625 125 31,25 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 15,625 0 15,625 15,625 15,625 15,625 15,625 140,625 0 15,625 31,25 15,625 0 15,625 15,625 15,625 0 15,625 15,625 0 15,625 15,625 15,625 62,5 15,625 0 46,875 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 31,25 15,625 15,625 31,25 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 15,625 31,25 15,625 31,25 15,625 15,625 31,25 15,625 15,625

it seems not so bad no? Exept for 0 value perhaps?

feanor91

29-08-2008 08:06:29

Well by now, I try with threadsleep ans it works. Not as we want, because, if I say I want 100 FPS max, I get about 120, if I say I want 50 max it's about 65 and 20 max (to test :) ) i'm about 25, so we have an effect with yourcode. I will have just to try on my computer that is faster than this I am on now...

Beauty

29-08-2008 10:03:52

put every timesincelastframe in my richtextbox.
I didn't use a constant output on Console or GUI, because it can falsify the time spans. Many output operations can be like a break. So I collected the results in a (fast) StringBuilder variable.
Because of this you got bigger time spans with RichText output.

it seems not so bad no? Exept for 0 value perhaps?
This is my problem of understanding. Either some frames are rendered directly back-to-back (time span = 0ms) or maybe it has something todo with the callback function (if it has a delay between rendering and calling).


What does Ogre professionals thing about? :?: :?:


.

feanor91

29-08-2008 10:32:52

This is my problem of understanding. Either some frames are rendered directly back-to-back (time span = 0ms) or maybe it has something todo with the callback function (if it has a delay between rendering and calling).

Yeah, I understand, meanwhile reducing FPS by this method works....There is a lot of thing that I renouce to understand about MOGRE-OGRE, because, I found nowhere a documentation about MOGRE, examples, are, well...all but not commented....API documentation of OGRE are all but very difficult to understand and not in relation with MOGRE (I think about the property in particular and somme function are very hard to understand for me)...And all is in english...So....It is for this that I try to make a class well commented for guys like me that will try to understand how it works. The only thing that I hope, is that I didn't make to much errors. And, there is MOGRENEWT-NEWTON to manage also...And there is one month ago I didn't know anything about OGRE...

Next step, I will to use some animation in my project so I will started to deal with this soon....

Some words about my project, if your intersting?

I do a sport that a few people knows about it : indoor kites flying. Some friends of me have invented a kind of descriptive and coded language to code posture of a kite when it flys, like altitude, posture, movement of pilot and so on...So, I have do a soft to write in that language. And as I have posture of the kite, I can transcribe it in 3D, so I look for a 3D engine to code in VB with. I found the so called 3DState but it's not free, and the student license expire by surprise current July so, I look for a new engine and found OGRE. The only thing to do is to relearn to coding with it. By now, I achieve the same level (even more because I can use shadows, reflections...) in my soft. I am about to publish the new release of it this week end, so comunity of kite flyer can use it with it's all beautifull and new 3D Engine.

And I have one more things to do in a future version. The final point of kite flying is to do ballet on music and in my soft, I have manage to modelise a gymnasium to show a simulation of ballets coding with the soft, so I will have a pilot, with it's skeleton, a kite, and a music and I have to manage movement with music. In the soft, I know duration of a figure, so I will have to see how manage all of this and with OGRE...It will be a huge work I think and it's first time I play with such thing...So I think that they will be a lot, very big lot, of questions to come....

Meanwhile, thanks a lot for your help.

Beauty

29-08-2008 10:57:50

I also had many problems with details of Ogre. Most important are the example codes of tutorials and forum. When I found out something what can be important for others I try to describe it in the wiki.
The main forum is the biggest place to find solutions.

Yes, Mogre is very different in some cases.
Sometimes it can help to look to the Mogre sources. There you see how Ogre is wrapped.

With MogreNewt I also have some small experiences. I use it only for collision detection, but this tooks enough time, too.
For generally Newton questions use the Newton Forum - it's the best place. Also the author of Newton helped me a lot.
Annoying are little undocumented things. For example collision detection only works if you have defined a mass for the objects.

Ogre is only for graphic, but it I suppose it should be easy to add sound functions. I didn't do until now, but other people.
Either by a sound library or just by .NET classes.

Your project sounds interesting. I will watch, what you do (-;
If you like, you can add it in the wiki:
www.ogre3d.org/wiki/index.php/Projects_using_MOGRE

r2d22k

29-08-2008 11:36:12

I wouldn't try to set the FPS to a distinct value. You should try to use FPS independent movement instead.

First: calculate the time, the last Frame needed to draw (QueryPerformanceCounter would be a good go), then move your objects the following way: Object.move((the distance the thing might pass in one sec)*(time needed to draw last frame));

feanor91

29-08-2008 12:08:06

Hello

I wouldn't try to set the FPS to a distinct value. You should try to use FPS independent movement instead.

First: calculate the time, the last Frame needed to draw (QueryPerformanceCounter would be a good go), then move your objects the following way: Object.move((the distance the thing might pass in one sec)*(time needed to draw last frame));


I think that you will speak about the anim stuff no? For the moment, it's not the reason that I want to reduce FPS number. In fact I have 2 (and even 3) different scenes. The first is a simple view of one kite ine a little room, this is here that my FPS are to high. The second is the gymnasium, here I will put anim of a pilot and kite, with shadows and all, and here, I think that my FPS will drop a little (lol), and the third (that I have to
modelise) will be a beach (for kite can fly outdoor also)...Meanwhile, thanks for yout reponse, because one time or another I surely post a question about that subject (lol) I will take note somewhere about this.

To beauty now....

Sometimes it can help to look to the Mogre sources. There you see how Ogre is wrapped.


Yeah, I think I will download that sources.


With MogreNewt I also have some small experiences. I use it only for collision detection, but this tooks enough time, too.
For generally Newton questions use the Newton Forum - it's the best place. Also the author of Newton helped me a lot.
Annoying are little undocumented things. For example collision detection only works if you have defined a mass for the objects.


Yes, I post on the good forum, don't worry, I have some difficulties with newton too, and I use it also for collision detection. But there is a lot of thing that I didn't understand too...Even more than ogre, perhaps.

Ogre is only for graphic, but it I suppose it should be easy to add sound functions. I didn't do until now, but other people.
Either by a sound library or just by .NET classes.


I use BASS.NET, a vey good sound class, I hope that I will not have difficulties to use with OGRE.

I will see for Wiki, you know, it's a vey little project...If you want to see it (even if you don't fly a kite, there is examples and it's localised ine french and english) you can download it here :

www.cramayailes-indoor.org/Kiteswap

It's the 3DState version, as I say before, I think that I will publish the OGRE version this afternoon, if the FPS reduction test works fine on my own computer.

And I've posted on Wiki....Fun.

Bekas

29-08-2008 12:32:01

You don't have to use your own "time tracking" code. FrameEvent already has this information:
bool Scene_FrameStarted(FrameEvent evt)
{
float t = evt.timeSinceLastFrame;
..............


By the way, if you only to want to reduce FPS because of CPU usage, I recommend using the VSync option, which reduces the FPS to the refresh rate of the monitor: http://www.ogre3d.org/phpBB2/viewtopic.php?t=34545&view=next&sid=ce193664e1d3d7c4af509e6f4e2718c6

feanor91

29-08-2008 12:42:57

Hi

You don't have to use your own "time tracking" code. FrameEvent already has this information:
bool Scene_FrameStarted(FrameEvent evt)
{
float t = evt.timeSinceLastFrame;
..............


By the way, if you only to want to reduce FPS because of CPU usage, I recommend using the VSync option, which reduces the FPS to the refresh rate of the monitor: http://www.ogre3d.org/phpBB2/viewtopic.php?t=34545&view=next&sid=ce193664e1d3d7c4af509e6f4e2718c6


About that, I see that this is use for a lot of thing but I don't understand it's purpose. The only thing I see it's if we don't use it, then nothing works, so I use stupidly with now understanding of what it does in the end.

r2d22k

29-08-2008 13:15:51

Hello

I wouldn't try to set the FPS to a distinct value. You should try to use FPS independent movement instead.

First: calculate the time, the last Frame needed to draw (QueryPerformanceCounter would be a good go), then move your objects the following way: Object.move((the distance the thing might pass in one sec)*(time needed to draw last frame));


I think that you will speak about the anim stuff no?

No you can use this technique for nearly everything, movement, animation, gamelogic etc, you sometimes just need to alter it a bit. It's an easy method to synchronize everything and to nearly do as much as would have happened in real time.

If i understood you right, your problem is that you got 800 AI/Movement calculations per sec in the one scene and about 100 in the other. Your AI looks something like this:


if(condition xyz == true)
{
do_something()
if(...){
entity->move(400);
}
}


When calculation the AI 800 times a sec, the npc would move about 800*400 if both conditions return true, while on the other one the npc would only move 100*400. To compensate this you have to do as i explained earlier.

There's no other reason i can think of, that you may need a FPS limitation.



About that, I see that this is use for a lot of thing but I don't understand it's purpose. The only thing I see it's if we don't use it, then nothing works, so I use stupidly with now understanding of what it does in the end.


What exactly are you talkin bout? The Vsync? It's definitely not needed. I have it disabled by default.
The evt.timeSinceLastFrame? Afaik i nevere had to call.

feanor91

29-08-2008 14:19:51

OK to explain why I want FPS limitation.

I try my soft on 2 computers, one where FPS are about 50 and one about 500 when shadows and reflections are in use.

I use newton to manage collision detection too.

The problem is that implement newton update from examples I found and I try to turn around TimeSetp, so I implement some variable for newton update fps and time to calculate newton update but I did not understand and found nowhere some explanation od what and how all this mess is working. I found an explanation on ai web site from someone whis seem to know what about it talk to adjust timestepby using a high resolution counter, so I try to implement this :


Public Function Timer() As Single

'Une fonction Timer haute résolution basée sur les performances de l'ordi
'Pour la gestion de l'update de Newton, non encore implémenté

Static Start As Decimal = 0
Static Frequency As Decimal = 0

If Start = 0 Then
'Si premier appel, on initialise les variables statiques
Dim bool As Boolean = QueryPerformanceCounter(Start)
QueryPerformanceFrequency(Frequency)
Return 0.0F
End If

Dim counter As Decimal
QueryPerformanceCounter(counter)

'On renvoi la valeur depuis le premier appel
Return ((counter - Start) / Frequency)



End Function


But QueryPerformancefrequency crash...So here my newton update func :


Private Sub UpdateNewton(ByVal TimeSinceLastFrame As Single)

'La fonction d'update du monde physique / function to update physical world

If _TimeFactor <> 0 Then
TimeElapsed = 3.5 * TimeSinceLastFrame * _TimeFactor
End If

'If TimeElapsed > TimeUpdate Then 'si le temps passé depuis le dernier update est plus grand que le temp minimum choisi, on se rattrape / if time passed since last update greater than minimum time, running after time
' While TimeElapsed > TimeUpdate
' myWorld.Update(TimeUpdate)
' TimeElapsed -= TimeUpdate
' End While
'Else 'sinons / else...
If TimeElapsed < TimeUpdate Then
'Le temps passé est plus petit que le temps minimum, on ne fait rien / time elapsed smaller than minimum time
Else
myWorld.Update(TimeUpdate) 'sinon, on update / else updating
End If
'End If
End Sub


First, I commented the while loop because, on certain case, Timeelapsed was very very great meaning ogre window busy.

So, now it works, only, if my FPS are vey high, _TimeFactor must be great to have a cool movement of camera and on slower computers, _TimeFactor must be low.

What I want, is a way to have a good time factor on every computer, and not to have it choosen by user.n So I think that to limit FPS number is a good way to achieve that.

Meanwhile how to manage timesincelastframe on different computer so You have greater on slow computer and lower in high computer but you can't know if it is high or low for you have nothing to compare. So I think the solution is to use the high res timer, but I understand nothing about this (I know, i'm not so good on this....)

r2d22k

29-08-2008 15:45:24

Yeah the normal timing functions aren't precise enough for framerate calculations, so either use the one Ogre supplies, or if that one really doesn't work, use the QueryPerformanceCounter.

To use the QueryPerformanceCounter in C# you basically may follow: this i don't think that it's very hard to port this to VB.Net.

Then you should only need one TimeFactor for every computer. It's somehow equal to classical mechanics(Newton-mechanics).

The Ogre TimeSinceLastFrame function, does it return the time needed to draw the last frame or the time from "the last frame has been finished" until now? If the later is the case, then we know why using this produces strange results.

[Edit]
the time you hand over to newton has also to be adjusted. "TimeUpdate" = timeelapsed*timingfactor; Then everything should just work fine. (I haven't used Newton for quite some time, but i think that should basically do the trick)

Beauty

29-08-2008 17:33:51

Nice to know.
The talk is about method GetTickCount().
Is it the same with DateTime.Now.Ticks?

r2d22k

29-08-2008 19:19:07

I'm not sure which method you are exactly talking about.
The DateTime.Now.Ticks isn't as precise as the QueryPerformanceCounter. The QPC is the best timer you can get on a Windows based OS, but i think for graphics apps the precision of DateTime.Now.Ticks should suffice(its about 100 nanoseconds).

If you're talking about the Windows API method GetTickCount() then this should answer your question:

GetTickCount Function

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

DateTime.Now.Ticks should be used in favor of GetTickCount, cause milisecondprecision is not enough.

Something i noticed about your FPS calculations during the first posts: you are doing an integer division and you are casting a long on the result, basicly no problem, but this gives you a mathematical error, that could cause some of your problems.

Something to read about Timers

feanor91

29-08-2008 19:26:18

Hi guys

I've tested on my own computer and limited to 100 give me about to 150 FPS. So it's work with same value of _TimeFactor...So I think I will publish like this, searching more acurate system later (in particular with animation), I will look about all you says.

But what about QueryPerformanceFrequency that crash? And what about time counter from Ogre?

drr00t

16-09-2008 02:46:58

Hi,

I have search about game loop development some time, and i found a very good article about this topic, take look

http://dewitters.koonsolo.com/gameloop.html

it´s work to me.

i am testing in Mogre samples, see my game loop.



public virtual void Go()
{
if (!Setup())
return;

root.RenderSystem._initRenderTargets();

//m_defaultFrameRate have default value of 25;
Int64 skipTicks = 1000 / m_defaultFrameRate ;
const int maxFrameSkip = 5;

Int64 nextGameTick = root.Timer.Milliseconds;

while (m_IsRunning && root.RenderOneFrame())
{
Byte loops = 0;

// frame rate of input update is 25 FPS
while (root.Timer.Milliseconds > nextGameTick && loops < maxFrameSkip)
{
inputKeyboard.Capture();
inputMouse.Capture();

nextGameTick += skipTicks;
loops++;
}

HandleInput(m_TimeInterpolationStep);

m_TimeInterpolationStep = Convert.ToSingle(root.Timer.Milliseconds + skipTicks - nextGameTick)/ Convert.ToSingle(skipTicks);
}

// clean up
DestroyScene();

root.Dispose();
root = null;
}

feanor91

16-09-2008 06:48:36

Hi, I will study that. Thansk

smirnof.pl

17-09-2008 09:04:51

Hi,

I'm developing a simple game editor for my project and i want reduce fps too. I was trying to use
root.RenderSystem.SetConfigOption("VSync", "Yes");
Whatever if VSync option is turn on/off it really don't influence on FPS rate. So why VSync don't reduce fps?

feanor91

17-09-2008 11:03:10

Hello, as I say, before, I study that...And, of course, I have questions :

You have no framestarted callback? Can you explain more deeply?

smirnof.pl

17-09-2008 12:42:16


You have no framestarted callback? Can you explain more deeply?

I have ogre embedded in panel form. To start ogre loop i'm using Root.startRendering() and I'm using framestart callback.

feanor91

17-09-2008 13:02:13

OK, me too I've embeded ogre in panel, but I use renderoneframe in a while loop, with frame started callback. So. can tou be more explicit on your method? Why are you calling renderoneframe in the first loop?
And what are you doing in inpuhandle function?

drr00t

17-09-2008 13:58:47

Hi,

I didn´t want use callback, just to think is not needed. In my sample tha HandleInput(m_TimeInterpolationStep) just update (positions, rotations) of scene objects. I remove all references of input capture from HandleInput e and brought to my gameloop with fixed update framerate in 25 FPS.

This would be use in phisics update, anything that you want that be update in a fixed framerate. Have graphics updated with maximum frame rate.


sorry my english.

drr00t

17-09-2008 14:01:24

Hi again,

My gameloop was based on this one. It provide a constant game speed independent of Variable FPS.


const int TICKS_PER_SECOND = 25;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;

DWORD next_game_tick = GetTickCount();
int loops;
float interpolation;

bool game_is_running = true;
while( game_is_running ) {

loops = 0;
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
update_game();

next_game_tick += SKIP_TICKS;
loops++;
}

interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
/ float( SKIP_TICKS );
display_game( interpolation );
}

feanor91

17-09-2008 14:16:39

Yes, I know, I read the link you gave. But, I have some dificulties to understand the code you wrote in relation with my own code.

drr00t

17-09-2008 15:08:23

Can you post the code of your game and just of methods that you call inside you game loop?

This kind of gameloop the framerate of rendering is separate of framerate of physics, input, etc...

feanor91

17-09-2008 16:32:54

Can you post the code of your game and just of methods that you call inside you game loop?

This kind of gameloop the framerate of rendering is separate of framerate of physics, input, etc...


Hi,

You can find all here : http://www.ogre3d.org/wiki/index.php/ClOgre

feanor91

18-09-2008 10:37:54

Well

If I understand well, You didn't use at all callback function as they are used for each render frame, if you choose to update only x time by seconds, you can't use them because they will be fired as ogre render speed.

So I didn't see how I can implement that kind of loop with my system, because I use a lot of callback function (for handle input, handle animations, handle reflections) and I have to separate animation from my main loop (OgreGo) because I think that animations will not be handle in my render class.

And what about frameEvent that I use to calculate anything?

Beauty

22-12-2009 16:40:00

Now I know why my fps reduce code didn't work as wanted and why there were such strange time results in my tests.

Here is the reason:
The value of DateTime.Now.Ticks has an accuracy of 0.0001 ms,
BUT the returned value isn't correct. The (operation?) system updates the DateTime object only all few milliseconds. In the time between the returned value doesn't change.

As a solution I read that you can use the PerformanceCounter on a windows system.
This class is in namespace System.Diagnostics.
The time accuracy depends to the computer hardware, but is much more better then DateTime.

Here is the MSDN class/member description:
http://msdn.microsoft.com/en-us/library ... 80%29.aspx

I just wanted to tell it if somebody is interested to limit the fps.
Maybe somewhen I'll edit my code and use the PerformanceCounter instead of the DateTime object.