• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Display Lists sometimes work, sometimes don't, depending on when created.

I'm creating a game in gl4java, and I am trying to make use of display lists because there are several large models in use.  The GLAnimCanvas, when the program starts, is not visible; it's actually part of a tabbed panel, and the *other* tab is visible from the start (it's a 2D representation of the screen, using something other than OpenGL).  Even though I call canvas.start() when the program loads, none of the init/preInit/display functions are called because it is not visible, only when I switch to the OpenGL tab do these functions get called for the first time (but on subsequent tab changes, they do not get called again-- just once).  

My problem:  On the 2D side, I place my objects (planes, tanks, etc.), and then I have the ability to view them in the OpenGL side.  Any objects I place *before* I go to the 3D side for the first time seem to work fine.  The first time they are rendered, I check to see if the display list has been compiled, if so I render (glCallList), if not, i compile.  However, if I go to the 3D side, and then choose to place a model on the 2D side that i haven't yet placed, when I go back to the 3D (opengl) side, they do not show up, however the display list *is* compiled and the glCallList call *is* executed.  It's almost as if after a few calls to display() that any display list compiled is not valid: if i place an object before display() is ever called, then it will be compiled on the *very first* call to display and will consequently render properly; if, however, i switch to the 3D side (therefore causing display() to be called many times) and *then* compile display lists, they don't show up.

Any ideas?  I'm not exactly sure what glGenLists() does; could this have something to do with it?
  • 3
  • 2
1 Solution
Once again, google is your friend -- you can look up the definition of any OpenGL function pretty quickly:

Two samples:

glGenLists works like many other GL functions, in that you tell it a 'count', and it 'allocates' that many indices for you to use.

You must call it to 'create' a display list index for later use.  You can call it like:
GLuint dlIndex;
dlIndex = glGenLists(1);

That sets dlIndex to whatever the starting available index is.  If you had already allocated a few, it might be '4' or '5'.

You can also allocate a block at a time:
dlIndex = glGenLists(64);

say dlIndex comes back as '5', you have 64 indices starting at the number 5, ending with the number 68.

YOU need to keep track of all indices 'created' and make sure to destroy them the same way.  Look at the second sample I gave, it'll help some.

slikrik98Author Commented:
Makes sense, and I'm now using it correctly (I think), however here's what's happening:

If I switch to the 3D tab (and thus start the GLCanvas running) before I add any display lists, glGenLists always returns 0 (and they thus don't show up).  If, however, I place the models in the system *before* kicking off the GL canvas, glGenLists returns 1, 2, 3, ... and so on, as it should (and they show up).

Why won't it allow me to allocate the space for the indices after the canvas has started?
slikrik98Author Commented:
Now i'm just getting incredibly bizarre behavior (most likely a result of the memory allocation for the display lists);  I found one problem that might have been the cause of the 0 return value from genLists, I didn't have my gljMakeCurrent call before this was occuring.  However, even now that it allocates space (in other words, glGenLists is returning nonzero values), the models sometimes don't appear and the program crashes (saying abnormal runtime error, or something along these lines).  I'm beginning to think that maybe my display() function is being called from two sources, because I vaguely remember a problem i used to have before where this occured.  Is it possible that two threads are calling the display function?  how would I know if there are 2 glContexts created?  my main loop looks like this:

public void display()
            glj.gljMakeCurrent();      //Ensure GL is initialised correctly
            gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glj.gljSwap();             //Swap buffers
            glj.gljFree();             // release GL

I've implemented a scene graph and root is the root node of the graph, so my traverseRender function basically traverses the graph and renders each node.  I'm wondering if someone else is calling display while i'm in the traverseRender() function and so gljMakeCurrent is called before the previous gljFree() was called.  Just so you know, when each object in the graph gets rendered, it firsts looks to see if a display list has been created for that type, if so, it just calls the display list, if not, it compiles the display list.
Yes, you need to have GL established and a context current when making any GL calls that allocate structures attached to a context..

That code looks okay by me.  If display is re-entrant, that'd be a potentially huge problem.  I don't know enough about Java usage in this situation -- I'm a GL expert, not a Java one. ;)

If you don't compile the display lists, and just use the raw data, does everything work fine?

slikrik98Author Commented:
I made the switch to JOGL, and everything works fine.  I couldn't handle not having full control over the display method (and when it was called); I'm sure my problems were a result of sloppy code, and not gl4java's fault, but it's a lot easier to write sloppy code when using it.

Thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now