FunnyFunkyBuggy
FunnyFunkyBuggy

Reputation: 613

What should I do to optimise my jogl performance?

I am writing a jogl program using JFrame and OpenGL to show 1500 spinning cubes(drawn by six squares). Each with different size, rotating speed and directions. The max fps is about 25. I think it is quite slow. I haven't implement the lighting effects and other stuffs. My laptop is a 13 inch Macbook air with i7 bought a few months ago. Here is my code for your reference. Could you kindly give me some advice to improve my program's performance? Advice that is not suitable for my situation is also welcomed. For example, which gl or flu functions will slow down my computer? Which should I avoid? Or, Which can be replaced? Lastly, how could I examine the overall performance of my program and how could I know my program is optimised?

    public void draw(GLAutoDrawable drawable) { // this is a method called in the display method.

    GL2 gl = drawable.getGL().getGL2();
    gl.glMatrixMode(GL2.GL_MODELVIEW);      
    if ( Constants.p == null ) return;
    for ( Primitives p : Constants.p ) { //Constants is a class that holds all the global variables

        gl.glLoadIdentity();
        gl.glTranslated(p.centerPoint.x,p.centerPoint.y, p.centerPoint.z);
        gl.glRotated(p.angle, p.direction.x, p.direction.y, p.direction.z);p.angle += p.speed;p.angle = p.angle % 360;


        if ( Constants.modes == 0 ) { //draw using lines
            gl.glBegin (GL2.GL_LINES);//static field

                    for ( Integer[] l : p.line ) {

                        gl.glColor3d(p.color.get(l[0]).x,p.color.get(l[0]).y,p.color.get(l[0]).z);
                        gl.glVertex3d( p.vert.get(l[0]).x, p.vert.get(l[0]).y, p.vert.get(l[0]).z);
                        gl.glColor3d(p.color.get(l[1]).x,p.color.get(l[1]).y,p.color.get(l[1]).z);
                        gl.glVertex3d( p.vert.get(l[1]).x, p.vert.get(l[1]).y, p.vert.get(l[1]).z);
                    }

            gl.glEnd();
        } else if ( Constants.modes == 1 ) { //draw using squares
            gl.glBegin (GL2.GL_QUADS);//static field

            for ( Integer[] f : p.face ) {

                for ( Integer l : f ) {

                    for ( Integer i : p.line.get(l) ) {

                        gl.glColor3d(p.color.get(i).x,p.color.get(i).y,p.color.get(i).z);
                        gl.glVertex3d( p.vert.get(i).x, p.vert.get(i).y, p.vert.get(i).z);

                    }

                }
            }

            gl.glEnd();
        }

    }

}

Upvotes: 3

Views: 405

Answers (1)

elect
elect

Reputation: 7190

which gl or flu functions will slow down my computer? Which should I avoid? Or, Which can be replaced?

You should avoid the following calls because deprecated:

glMatrixMode, glLoadIdentity, glTranslated, glRotated

And especially these one because they are also very inefficient:

glBegin, glColor3d, glVertex3d, glEnd

Essentially you are sending very few data per time and your gpu, when executing, is greatly underused and most of time stalling waiting for data.

To change that, you have to do a couple of things, that may bring you a boost even up to hundred times faster.

For all the matrices operation, you can use the jogl util FloatUtil or use a library, such as joml or my jglm, and then upload it to an uniform variable or an uniform buffer object (UBO).

For the vertices and the colors you should use instead a vertex buffer object (VBO). Basically you pack all your data together once at the begin, upload them to the gpu, set a couple of parameters and then launch the rendering call that will fetch the data directly from this(these) buffer(s).

If you don't need any GUI for the moment (such as awt, swt or swing), you can use the high performance native toolkit Newt.

I'd suggest you rather then modifying your code, to start from a solid and working scenario, such as one of my Hello Triangle (take GL3 for the moment since GL4 requires OpenGL 4.5 and OSX offers only up to 4.1, so you should modify it, but this is not recommended at the moment for you).

Then once you get it run, study a little the code in order to understand the basics. Then start to build your code on top of it.

In this way, once you apply a modification, you can test and see if it works, if not, you know the error lies probably in your last attempt.

If you have any problem, don't hesitate to ask for help :)

Upvotes: 3

Related Questions