Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display fps with opengl es (android)

I am new to java and opengl and still learning so forgive me for my ignorance. I have made an looming object apk file. Now i want to view the fps as the application is playing on my device. I have been following this tutorial http://www.youtube.com/watch?v=1MYgP8Oj5RY&list=UUKwypsRGgHj1mrlQXhLXsvg&index=67 and attempted to modify it for android but i can't get it to work.

Could anyone show me an example of how to add a simple fps indicator on the corner or on the title page and how to control the fps.

Thanks

like image 272
ihave2teeth Avatar asked Mar 13 '13 23:03

ihave2teeth


People also ask

Can I use OpenGL on Android?

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL®), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware.

How to set frame rate in Android?

The new option is under Settings > System > Developer options > Show refresh rate.

What is OpenGL es Android?

The OpenGL ES APIs provided by the Android framework offers a set of tools for displaying high-end, animated graphics that are limited only by your imagination and can also benefit from the acceleration of graphics processing units (GPUs) provided on many Android devices.


2 Answers

if you use this class and call logFrame() it will tell you the fps in a log on your logcat:

import android.util.Log;

public class FPSCounter {
    long startTime = System.nanoTime();
    int frames = 0;

    public void logFrame() {
        frames++;
        if(System.nanoTime() - startTime >= 1000000000) {
            Log.d("FPSCounter", "fps: " + frames);
            frames = 0;
            startTime = System.nanoTime();
        }
    }
}

from what ive read you optimise your code to get a better framerate by minimising the calls to the gl states

here is a link for drawText()

like image 57
JRowan Avatar answered Sep 19 '22 17:09

JRowan


I know it's late, but this is Google's first suggestion. So I just want to make it better:

Why count the frames that one second passes? I need in my application a live value of the FPS (as you should use it too). And so far I know it says

Frames/Second

That means that you can measure the time between one frame and the next and say:

Second/Frame

To get to FPS it's simply

1/(Second/Frame).

Here is the code. I used the code from above and edited it to get to my result:

public class FPSCounter {
    private long lastFrame = System.nanoTime();
    public float FPS = 0;

    public void logFrame() {
        long time = (System.nanoTime() - lastFrame);
        FPS = 1/(time/1000000000.0f);
        lastFrame = System.nanoTime();
    }
}
like image 27
Georg A. Friedrich Avatar answered Sep 18 '22 17:09

Georg A. Friedrich