[Qt-interest] calculate FPS using QTime
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Mon Jan 12 10:01:59 CET 2009
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
More information about the Qt-interest-old
mailing list