Some general OpenGL questions

When the init method is called can I store the gl object I get there and use that everywhere else or do I have to call this

gl = drawable.getGL().getGL2();

every single time, and why?

About the state of OpenGL,
how costly are things like glLineWidth, glColor, glBlendMode, glEnable/Disable?
Would it be worth it to create a class to manage the state of OpenGL eg.
instead of:

glLineWidth(2);

Something like:

StateManagementClass.glLineWidth(float width){
      if(currentLineWidth != width){
            currentLineWidth = width;
            glLineWidth(currentLineWidth)
      }
}

With similar methods for enabling/disabling texture, blend mode, color etc.

No don’t do this. In some situations, the context can be lost and the GL instance has to be refreshed. If you keep your old instance instead of using the fresh instance returned by getGL(), you will have some problems…

It depends on the weight of each call and of the frequency of calls. Some 3D engines already implement such a feature.

OK Thanks.

So on average is some kind of state management class worth it or not?
Or do you only start to see a gain when you are working with more a complicated application.

I find state management worth it. It’s also very useful to do sorting by state, so if you have objects that encapsulate the opengl state in discrete pieces, you could sort by these. Then when rendering, only modify the opengl state when the state objects switch to the next. This saves a lot of time with just checking each opengl state, and you might even be able to get away with no lower level state management.