I have an LWJGL-based application that’s using the new Display.setParent() method to attach the LWJGL display to an AWT canvas. As the code stands everything works nicely: the scene is rendered correctly, AWT keyboard and mouse events are handled properly, etc.
With one exception - picking. This is the only part of the code that invokes any GL methods outside of the initialisation/rendering code (to get the current projection/modelview to project a ray into the scene). When this code is executed in response to an AWT click event, the GL11 ‘global’ is null, which I think means that the GL context is not attached to the current thread (the AWT dispatch thread in this case). If I tried to kludge round this using Display.makeCurrent() I get an exception because the context is attached to the main thread.
One thing to point out, I haven’t used a separate thread for the application or the rendering loop.
The following pseudo-code illustrates how I’m running the application:
create AWT frame and canvas
Display.create()
Display.setParent( canvas )
frame.setVisible()
add mouse listener to frame {
picking code that accesses the GL modelview/projection matrices
}
loop {
render scene
swap buffers
}
So what’s going on? Is this just a daft multi-threading cock-up on my behalf? Should the context be attached to the AWT dispatch thread?
Neither have I used SwingUtilities.invokeLater(), I’m fairly experienced with AWT/SWING and am VERY aware of the issues when attempting to update a SWING GUI in a multi-threaded application. But the above problem strikes me as remarkably similar - maybe this is the problem?
The SWING developers recommend that one invokes the code that realizes a SWING component using invokeLater() which I’m also not doing (indeed I don’t think I every have, whoops). Interestingly enough, if I run the above code using invokeLater() I don’t get any AWT events ???
Cheers for any suggestions or hints from anyone that’s had similar problems or can see what I’m doing wrong.
I have noticed other frameworks/engines implementing a sort of event queue mechanism, is that what I should be using to access the GL context? i.e. add a ‘pick’ event to a queue in the application, and then handle events from within the rendering loop where the context is valid?
Apologies if I’m reiterating an known problem - I’ve had a nose round but can’t find any examples or forum threads that are related.
- chris
