[Qt-interest] Fwd: calculate FPS using QTime

ami guru dosto.walla at gmail.com
Mon Jan 12 10:18:35 CET 2009


Hello Oliver,


I have tried something as you have suggested , i believe..

Reference has been taken from Lighthouse 3D as well.


The following snippet shows how it has been done:


*****************************************'

void GLWidget::calculateFPS()
{
    //get the elapsed time
    //that will be used
    //to calculate the
    //frame per second
    frame++;
    t = time.elapsed();


    if(t - timeBase > 1000)
      {
    fps = frame * 1000.0/(t-timeBase);
    timeBase = t;
    frame = 0;


    emit frameRateChanged(QVariant(fps).toString());
      }

    //qDebug() << fps << endl;
    //updateGL();
}
****************************************'

Is there any idle function or any kind of looping function so that i can
continuously update the fps

Now i do not get that updated one untill i interact with the scene.


Thanks

Sajjad

---------- Forwarded message ----------
From: <Oliver.Knoll at comit.ch>
Date: Mon, Jan 12, 2009 at 10:01 AM
Subject: Re: [Qt-interest] calculate FPS using QTime
To: qt-interest at trolltech.com


ami guru wrote on Monday, January 12, 2009 6:12 AM:

> Hello forum,
>
>
> I am trying to calculate the the fps using QTime.
>
> starting the time inside the widget that is the subclass of QGLWidget
> and calling the calculateFPS() function inside paintGL() function.

Measuring the FPS with any 3D toolkit (OpenGL, Direct3D) is not as easy as
it seems at first sight. First off, most API calls in these toolkits are
asynchronous. That means by the time you call (pseudo-code):

void MyGLWidget::paintGL() {
 int elapsed;
 QTimer timer;
 ...
 timer.start();
 glBegin(GL_TRIANGLES);
 {
   glVertex3f(...);
   ...
 }
 glEnd();
 // 'elapsed' is NOT really the time it takes to draw the scene!
 elapsed = timer.elapsed();
}

the vertices might not even have arrived in the graphic cards memory yet
when paintGL() above finishes! Only a glFlush() actually waits until all
commands have been processed, but you really want to avoid glFlush() as much
as possible for performance reasons (unless you really HAVE to make sure the
scene has been fully drawn, e.g. when you want to create a pixel overlay
over the buffer afterwards etc.)

That said, in the above example 'elapsed' will in fact mostly 0 anyway, due
to the precision.

An easy and more or less accurate way is to:

1. Start a timer
2. render 100 frames
3. Take the elapsed time.

Then divide the elapsed time by 100 and you get your FPS. Off course you can
continuosly update your timer like this as well, if you want to have a "life
FPS" display (start a counter in the very beginning, count the number of
frames rendered so far).

Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090112/faaf786c/attachment.html 


More information about the Qt-interest-old mailing list