Game loops with JOGL

Most JOGL examples I’ve seen create an Animator and put game logic (usually just animation) in display(). Is this the safest place to put it, or can there be problems because you’re in JOGL’s thread? Also, is it safe to use Thread.sleep in the display() method? (I noticed that if I minimize my application, display() is still called, leading to 1000+ “fps”; is there a way to tell whether the canvas is visible and sleep in this case?)

Also, an unrelated question, about JOGL performance: I started with the second lesson on http://nehe.gamedev.net, and modified display() to draw 10000 triangles instead of 1 as follows:

gl.glBegin(GL.GL_TRIANGLES); // Drawing Using Triangles
        for(int i=0; i<10000; i++) {
            gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
            gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
            gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
        }
        gl.glEnd(); // Finished Drawing The Triangle

This slows down performance pretty significantly, from about 60 fps (which is my monitor’s refresh rate - if I turn off wait-for-vsync I get about 200) to maybe 5 fps. (My software 3d renderer probably does better than that!). I assume this is because glVertex3f is a native method, and there’s a high overhead in calling it. Is there a way to specifiy the mesh within an array and then pass it to OpenGL in a “batch”? (I’d also like to use skeletal animation so I want to build up or modify this thing every frame, if possible).

To stop the renderer when the window is minimized, add a WindowListener to the frame/window that contains your canvas, and implement the following methods:


            public void windowIconified(WindowEvent event)
            {
               // pause rendering
            }



            public void windowDeiconified(WindowEvent event)
            {
               // resume rendering
            }



            public void windowClosing(WindowEvent event)
            {
               // stop rendering
            }

Hi,

You can have a look at JOGL demos sources to use VBOs or Vertex Arrays, which are far more efficient than a lot of vertex3f calls, you can also use display lists if your data don’t change…

And for the game logic, the only limitation is to do every single gl call into the JOGL thread (even GlUnproject and these kinds of things which can be useful to your logic…! So display is one of the best places…

Thanks, I found how to use vertex arrays and I’m doing that now. Unfortunately my graphics card doesn’t support vertex buffer objects :-/.