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.


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

    //  Calculate FPS

    //  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);


    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

    //  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.

  10. I simply couldn’t depart your website before suggesting that I really enjoyed the usual info a person supply on your guests?
    Is going to be again often to inspect new posts

  11. Wonderful website, just lately been scouting forever for tips
    on the best rattan furnishings for our home and in our patio.
    This website truly helpedgreat blog some great info here

  12. we not long ago set up our steam shower unit, the best thing we have purchased
    for some time, kids and family members like it so much, simply cannot
    see everyone going back to regular showers ever again

  13. Hello my family member! I wish to say that this post is amazing, great written and include approximately all significant infos.
    I would like to see more posts like this .

  14. He actually created the Nakshatras, thuus these natives seem to be all knowing andd knmow things witout any methodical practice.
    In thhis stage future members will think about what may be expected off
    tjem when they join the team. The trader ccan run this process
    simultaneously as he will purchase again the US Dollars when the price of
    US Dollars come again at the level which is equal to or lower than 80.

  15. I got this site from my pal who told me about this website and now this time I am visiting this website and reading very informative posts
    at this time.

  16. Yesterday, while I was at work, my sister stole my iphone and tested to
    see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and
    she has 83 views. I know this is entirely off topic but I had to share it with someone!

  17. I will right away seize your rss feed as I can’t find
    your e-mail subscription link or newsletter service. Do you have
    any? Please allow me know in order that I could subscribe.


  18. I honestly believe that Adrian Peterson, Arian Foster, Ray
    Rice, Marshawn Lynch, or Jamaal Charles should be your focus in the first round.
    The different types of defensive football positions are:
    . There are some excellent websites that offer live football matches, as well as live match scores,
    for one low price.

  19. Hello there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly? My blog looks weird when browsing from my
    iphone. I’m trying to find a theme or plugin that might be able to resolve this issue.
    If you have any suggestions, please share. Thanks!

    • best coffee maker with grinder and thermal Carafe
    • September 6th, 2014

    After looking into a number of the articles on your blog, I honestly like your way of blogging.
    I saved as a favorite itt to my bookmark webpage list and will be checking back soon. Please
    visit my web site as well and let me know what you think.

  20. What’s Taking place i am new to this, I stumbled upon this I have found It positively helpful and it has
    aided me out loads. I hope to give a contribution & aid other users like its helped me.
    Great job.

  21. Thank you a lot for sharing this with all
    people you actually understand what you are talking about! Bookmarked.
    Kindly also discuss with my web site =).
    We could have a hyperlink change contract among us

  22. What i do not understood is in fact how you’re no longer actually a
    lot more neatly-liked than you might be now.
    You’re very intelligent. You understand thus significantly
    on the subject of this matter, produced me individually consider it from a lot of numerous angles.
    Its like men and women are not fascinated unless it’s one thing to
    accomplish with Lady gaga! Your personal stuffs outstanding.
    Always deal with it up!

  23. Lawyer or attorney Victorianne Musonza, Lawyer At Laws, Licensed in NY And
    NC, Maxwell Law Office, PLLC is accepted to rehearse rules
    in Rhode – Tropical isle and Massachusetts, so the Federal government
    Section Courts, which is a member of the Pub
    of the us Superior Court. When working under GAAP, revenues and gains have completely
    separate definitions. This method pts are crucial inside working with a defined talent management

  24. Currently itt sounds like Expression Engine is the
    preferred blogging platform available right now.

    (from what I’ve read) Is that what youu are using on your blog?

  25. Excellent way of explaining, and good post to take data
    concerning my presentation subject matter, which i am
    going to present in institution of higher education.

  26. After exploring a handful of the articles on your web site,
    I seriously like your way of blogging. I saved as a favorite
    it to my bookmark webpage list and will be checking back in the near future.
    Please visit my website as well and let me know what you think.

  27. Hey there! I’ve been reading your web site for a while now and finally got the
    bravery to go ahead and give you a shout out from Houston Tx!
    Just wanted to say keep up the good work!

  28. Hmm it looks like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I
    submitted and say, I’m thoroughly enjoying
    your blog. I as well am an aspiring blog blogger but I’m still new to
    everything. Do you have any tips for first-time blog writers?
    I’d certainly appreciate it.

    • family guy the quest for stuff hack
    • September 26th, 2014

    Is your Family Guy The Quest for Stuff Hack is a ‘survival mode’ type Family Guy The
    Quest for Stuff Hack or an ‘endless runner’. In the case of Star Conflict, it seems those who invest the money get a better ‘head start’ on those who don’t.
    Spring, summer, and even fall birthday parties in N.

  29. Had been in actual fact searching for just an average shower enclosures before I uncovered this page,
    didn’t even know there were any such thing as a ‘steam shower
    enclosure’, really, may very well just have to have one

  30. This post is worth everyone’s attention. When can I find out more?

  31. I recently set up our steam shower unit, finest thing we’ve bought
    in a long while, the children and family members think it’s great, unable
    to see everybody going back to traditional showers again

  32. Hey there! I’ve been reading your site for some time now and
    finally got the courage to go ahead and give you a shout out from
    Lubbock Tx! Just wanted to say keep up the good job!

  33. Only use those that have a color guide, which will help you
    see where you applied the lotion. You can buy swimsuits in floral and island prints to add more fun to your look at the beach.
    Suede is less durable than other varieties of Buffalo
    Leather and is cheaper because many pieces of suede can be split from
    a single piece of the hide.

  34. With the growing popularity of the game, the sophistication of the audience has
    also increased. The Diablo 3 site released
    this official FAQ regarding the open beta ‘stress test':.
    Bio – Ware is giving the gaming community a new formula, one that’s as different
    from Wo – W’s model as quantum mechanics is from Newtonian physics.

  1. No trackbacks yet.

Leave a Reply

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

You are commenting using your 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


Get every new post delivered to your Inbox.

%d bloggers like this: