OpenGL Frame Rate


Calculating the frames per second in our Graphics application makes sense when we are displaying animation, which is simply a collection of images displayed rapidly and in sequence.When we watch a video\movie, the usual number of frames displayed in a second is 24. Our eyes can’t see all the 24 frames one by one in a single second, and that’s why we get the illusion of something moving, rotating, or growing in size.

In this post, we are going to create an OpenGL app that animates an object and calculates the frames per second for that animation.

FPS

To make this calculation, we need to find a way to keep track of the number of frames and time elapsed. The key here is to do the calculation in the GLUT idle function (callback function passed to glutIdleFunc), which is called only when our application is not busy doing something else (such as drawing the current frame). So every time the idle function is called, we increment the frame count, check the time elapsed, and if the time is greater than or equal to 1 second, we calculate the FPS.

//-------------------------------------------------------------------------
//  This function is called when OpenGL\GLUT is not working on
//  something else... It is mainly used for animation...
//
//  It's like the timers but time intervals are dependent on how busy
//  the app is, instead of having a constant value set by the user.
//-------------------------------------------------------------------------
void idle (void)
{
    //  Animate the object
    animateObject();

    //  Calculate FPS
    calculateFPS();

    //  Call display function (draw the current frame)
    glutPostRedisplay ();
}

As you can see in the code above, the idle function is animating the object, calculating the FPS, then triggering the display function (callback function passed to glutDisplayFunc) by calling glutPostRedisplay().

The display function would basically display the object and text showing the FPS value.

//-------------------------------------------------------------------------
//  This function is passed to glutDisplayFunc in order to display
//	OpenGL contents on the window.
//-------------------------------------------------------------------------
void display (void)
{
    glClear (GL_COLOR_BUFFER_BIT);

    drawObject();
    drawFPS();

    glutSwapBuffers ();
}

To get the time elapsed, we use the glutGet(GLUT_ELAPSED_TIME) function which will get the time passed in milliseconds since the program started or since it was last called. The implementation of calculateFPS would look like this:

//-------------------------------------------------------------------------
// Calculates the frames per second
//-------------------------------------------------------------------------
void calculateFPS()
{
    //  Increase frame count
    frameCount++;

    //  Get the number of milliseconds since glutInit called
    //  (or first call to glutGet(GLUT ELAPSED TIME)).
    currentTime = glutGet(GLUT_ELAPSED_TIME);

    //  Calculate time passed
    int timeInterval = currentTime - previousTime;

    if(timeInterval > 1000)
    {
        //  calculate the number of frames per second
        fps = frameCount / (timeInterval / 1000.0f);

        //  Set time
        previousTime = currentTime;

        //  Reset frame count
        frameCount = 0;
    }
}

Download full source code here. If you have any issues compiling or running the app, check out this section for details about compiling and running an OpenGL app that uses the GLUT library. Toby Howard has a different implementation that is not dependent on GLUT. You can find it here.

About these ads
    • Omar Ayman
    • March 31st, 2011

    Thanks.. This was really helpful !!

    • Matt Langston
    • April 15th, 2012

    This was extremely helpful. I am using your code in my openCL final project and have given credit where credit is due in both the source and presentation. Thanks again!

    • Np. Glad it’s helpful Matt.

  1. It is truly a nice and helpful piece of information. I’m glad that you shared this useful ino with us.
    Please keep us up to date like this. Thanks for sharing.

  2. Finding the right house for your family means finding the best nest will
    help you to take care of your kids better.
    In fact you may just adore the procedure of moving.
    They usually provide long distance mover provides services to the
    customer who wants it from them.

  3. Howdy! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m definitely enjoying your blog and look forward to new updates.

  4. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks a lot!

  5. My spouse and I stumbled over here from a different web address and thought
    I might as well check things out. I like what I see so i
    am just following you. Look forward to finding out about your web
    page for a second time.

  6. Is there a web site that makes it simple to follow websites and podcasts?
    I don’t have an iPod, does that matter?.

  7. When I lived in Britain, my kitchen was full of labour-saving devices,
    but some of the things I used to use just once a month over there, I now see being used every day.
    For those who love cooking but hate the clean-up involved the Scrap Trap is an great tool to keep your counters and floor
    sparkling and uncluttered. These are the essential piece
    of kit to help you quickly mix dough, cream and cake ingredients, and they are available with
    a variety of speeds and attachments to help you get the job done.

  8. of course like your web-site however you need to check the spelling on quite
    a few of your posts. Many of them are rife with spelling
    problems and I in finding it very bothersome to
    tell the truth nevertheless I’ll surely come back again.

  9. Try the chef-tested and approved Kyocera ceramic knives (Seattle chefs use tnem at
    home and at work). ca to learn about andd order superior custom granite.
    This being a Square-Enix game, you have something aakin too a
    limit break iin Finhal Fantasy.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: