Why require context?

Hi,

My life has taken me from OpenGL to Java3D and now to Jogl. And I have this question.

The way I’ve been organizing my code is by letting each object build itself and then sticking it into varous contexts. But with Jogl this seems not possible since any routine that does the building needs to have an instance of the GL class. This causes a couple “problems”.

  1. I need to build each object several times depending on the context.
  2. I can’t hide implementation (that fact that I’m using Jogl) since all of my routines have GLDrawable as an argument, and I have always been taught to separate logic from implementation.

Any thoughts?

Dola

This is always a tricky one, and i’ve been tinkering with various methods for a while now. You’re trying to decouple your rendering and game logic, yes?

First off, the standard method nicked from normal J2D rendering is some kind of .paint(Graphics) method, which works nice since you can wrap up animation etc. in its own class and just have it display its current frame in the paint method. While this is slightly possible in a OpenGL driven game, its not even close to efficiant, and as soon as you start on anything more complicated you end up tearing your hair out with GL state problems…

You end up having to take your abstraction to a higher level - instead of your player/enemy/whatever game objects having a draw method, let these hold on to some kind of Sprite or Mesh class (with whatever material/texture/shader set of state that you need). Then these Mesh objects are held by some kind of renderer object which is in charge of drawing them all. Add some sort of camera object and you’ve got everything you need to draw a frame.

If you look at that again, you’ll realise that you’ve effectivly created your own simple scene graph - a scene consisting of Meshes and Materials, in some sort of collection along with the camera. Your renderer deals with the low level details of turning this abstract representation into an actual image on screen.

This leaves your renderer is free to do all the funky stuff it needs like state sorting on Materials, view culling, building geometry lists from Mesh objects etc. and the rest of your app just has to poke objects into a scene graph and not have to worry about it ;D

Hope that gives you some ideas at least :slight_smile:

Thank you very much for the reply. WHat you are saying makes sense.

But is my conclusion correct that if I want to have, say, two views of the same object, I will need two display lists?

Thanks again,

Dola

JoGL now implements shared display lists between GLContexts so you don’t need to create 2 lists for the same object if you run 2 GLContexts.
This is a recent feature and you need to get the latest official binary release (september 5) for it.
However, I think you’ll still have to set perspective and lighting independantly for each GLContext.