GLCanvas context creation/destruction

I’m having an interesting problem with the context of my GLCanvas objects. The software I work on uses a number of GLCanvas objects to display views on a data set. It creates a number of textures to hold the views.

When a view is hidden, it’s removed from its parent component temporarily; when the parent component is made visible again, the GLCanvas is added back in. However, the GL context has lost any textures that were associated with it.

Currently we deal with this by subclassing GLCanvas and overriding addNotify and removeNotify. However, this isn’t a very nice approach. Is there some standard JOGL way of detecting this context cleanup instead, to allow textures and suchlike to be invalidated?

An approach I’ve used before is to have each object have a version number. This version number is set to the context’s version when the object is init’ed. Whenever the context is recreated (so the GLEventListener’s init() method is called again), increase the context version number. Before using any texture/object, make sure that its version matches the contexts; if it doesn’t, it’s invalid.

Ah! I hadn’t realise that init() was getting called every time the context is recreated. That makes things a lot easier.

Thanks!

If you don’t want to reload the textures again, you can either use a pbuffer or always visible 1x1px GLJPanel to hold them and share the textures (or other resources) over the GLCanvases…

I’ve been considering that, but recreating the textures isn’t a problem. We use quite a few GLCanvas objects with only a small number visible at once, so keeping the textures around would just use texture memory that would be better reused elsewhere. I can think of better approaches, but it’s not really worth refactoring at this point.