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. I think the admin of this site is really working hard in favor of his web page, because
    here every material is quality based information.

  2. I have learn a few good stuff here. Definitely worth
    bookmarking for revisiting. I wonder how much effort you put to make the sort of excellent informative website.

  3. I’ve been exploring for a little bit for any high-quality articles or blog posts on this sort of house . Exploring in Yahoo I finally stumbled upon this site. Studying this information So i am satisfied to exhibit that I’ve
    an incredibly good uncanny feeling I discovered exactly what I needed.
    I so much certainly will make sure to don?t fail to remember this website and provides it a glance on a relentless basis.

  4. Hello, I check your new stuff on a regular basis. Your humoristic style is witty, keep it up!

    \

  5. The roasted and cooled beans are usually left to ‘degas’
    for a period of time. Knowing the difference is essential as this
    will help you buy gourmet coffee. Like Kona Grove #1, it
    has a tantalizing, buttery aroma.

  6. Excellent blog here! Also your site loads up fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as quickly as yours lol

  7. If your Garage Door has been fixed with the presence
    of poles then attach the wire with the poles. Steel garage doors cost about $600 for the
    most basic variety and there are many more styles which are priced accordingly.

    These materials are all very sturdy and perfect as garage gate.

  8. Hmm it seems like your site ate my first comment (it was super long) so I guess I’ll just
    sum it up what I had written and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any suggestions for first-time blog writers?
    I’d certainly appreciate it.

  9. I’m gone to inform my little brother, that he should also pay a visit this blog on regular basis to take
    updated from latest reports.

  10. Hi there, this weekend is pleasant in support of me, for the reason that this moment i am reading
    this great educational post here at my house.

  11. I am no lonɡer ure wɦere ƴoս are getting youг info, howеνer
    greɑt topic. ӏ must pend a whilе studyiing morе oг figuring
    out more. Ҭhanks fߋr fantastic info I սsed tο be on tthe lookout for thiѕ information for
    my mission.

  12. What i don’t understood is if truth be told how you’re not really a lot more smartly-favored than you might be now.
    You’re very intelligent. You already know thus significantly in relation to this matter, produced me individually imagine it from numerous various angles.
    Its like women and men are not involved until it’s one thing to accomplish with Lady gaga!
    Your own stuffs excellent. At all times take care of it up!

  13. Hi! This is kind of off topic but I need some advice from
    an established blog. Is it very difficult to
    set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but I’m not sure where to begin.
    Do you have any ideas or suggestions? Cheers

  14. You could certainly see your expertise within the work you write.

    The arena hopes for more passionate writers such as you who
    aren’t afraid to say how they believe. All the time go
    after your heart.

  15. What i don’t realize is in truth how you’re no longer actually much more neatly-liked than
    you might be right now. You are very intelligent.

    You already knnow therefore considerably with regards
    to this topic, made me for my part believe it from so many various
    angles. Itts like women and men are not invoplved
    except it’s something to accomplish with Woman gaga! Your
    individual stuffs outstanding. Always care for iit
    up!

  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: