Search code examples
javaopengljoglframe-rate

Jogl animator always says FPS is 0


I want to print FPS to the screen in my program. The number returned from getLastFPS is always 0. I've looked at FPSCounter.html#getLastFPS() and some other examples, and I believe this is the right method to call to get the fps. I know the fps counter is active because getTotalFPSFrames() returns a value that increases. getTotalFPSDuration() always says 0.

Why is it wrong, how can I fix it?

public void display(GLAutoDrawable drawable) {

    float time=drawable.getAnimator().getLastFPS();

    GL2 gl =drawable.getGL().getGL2();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    gl.glPushMatrix();
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glPushMatrix();
    gl.glLoadIdentity();
    int[] viewport =new int[4];
    gl.glGetIntegerv (GL2.GL_VIEWPORT, viewport,0);
    glu.gluOrtho2D (0,viewport[2], viewport[3], 0);
    gl.glDepthFunc(GL2.GL_ALWAYS);
    gl.glColor4f(1, 0, 0,1);
    gl.glRasterPos2d(30,30);
    glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, "FPS: "+time);
    gl.glDepthFunc(GL2.GL_LESS);
    gl.glPopMatrix();
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glPopMatrix();

    //other 3d rendering
}

Solution

  • You need to set up a frequency of FPS counter update. So for example to your init() method add this:

    drawable.getAnimator().setUpdateFPSFrames(3, null);
    

    Then drawable.getAnimator().getLastFPS(); will deliver new FPS count every 3 frames.