Mapping GL identifiers in java objects

I noticed something in the GLCanvas javadocs (actually, I read it from http://www.java-gaming.org/forums/index.php?topic=14732.0 , but nevertheless, it is in the javadoc… :slight_smile: )

“users should not maintain a hash table with the GL object as the key.”

I’ve done exactly this (made a HashMap<GL,Integer> or the like) in one of my classes to store identifiers like the number of allocated light numbers and display list names, and it has worked in all of my test cases… what is the problem with using this technique? And what’s an alternative - should I instead be mapping by GLAutoDrawables or something similar?

The issue is that each time the OpenGL context is recreated (for example, because the GLCanvas was removed from the component hierarchy and re-added) a new GL instance will be associated with it. You can’t use the GL object itself to keep track of the identity of the GLContext. You can use the GLContext object for this purpose, but you will need to check for calls to GLEventListener.init() and if it is called and you don’t have display list sharing in your application you’ll need to make sure you properly clear out these hash tables since all of those objects will have been deleted.

BTW, these issues arise principally because of the possibility of destruction and re-creation of the OpenGL context which is inherent when you’re dealing with an OpenGL binding to a window system toolkit which allows dynamic updates to the widget hierarchy, such as the AWT.

So is all the OpenGL state copied over when the GL object changes (but the GLContext object stays the same), or is it only for a GLContext change that the GLEventListener.init gets called? That is, is it guaranteed that if the GL object changes, init is called, or is it only when the GLContext is recreated that init is called?

And thanks for the explanation - this also explains why it’s worked up until now (I have simple widgets that aren’t being altered much in the test case).

No, the OpenGL state is not copied over. When GLEventListener.init() is called that means a new low-level OpenGL context with a new set of state has been created – except in the situation where the application has requested sharing of textures, etc. with another context, in which case the texture and other objects will persist. When this happens the Java-level GLContext object stays the same; the Java-level GL object changes.