JOGL problem with thread

Dear All,

        I am new in JOGL. I have a problem with running JOGL program in thread. 
        I have created a thread in display method of the GLEventListener implementation class. This thread renders a text in the GLCanvas. But it does not working.

       But, if i put same thing in display method and use Animator for repeated display of text it works fine. I don't understand what the problem is?.

       Please help me if possible.

       Thanks in Advance..........

For any sort of OpenGL rendering to occur, it must be done on the thread where the OpenGL context is current. Using JOGL’s Animators and GLEventListeners, the context is guaranteed to be current when methods like display(), reshape() and init() are called (I think those are the methods anyway, I can’t seem to get to the API docs right now). If you create another thread and want to run OpenGL functions in there, you have to make the context current on that thread before performing those actions.

Take a look at the javadocs for the GLContext class for doing this. Don’t forget to release the context once you’re done with it, if you’re wanting other threads to use it.

How about some good code snippets about threading in Java? I need it too, but there are no examples to find. Always the same answer: look at the javadocs. I’ve looked in this docs now several times, but I just don’t get this thing to work.

Are there any examples or code snippets with JOGL and threading?!? Or someone who would do something like that?

Well, it’s kinda hard to explain how to write a multi-threaded Java application. To quote a website I stumbled across when reading about multithreading, “it’s a bit of a black art really”.

It takes a lot of understanding and practice. First things first though: get a basic understanding about threads and Java threads. There was a synchronization tutorial which I’ve used on Sun’s site, but I can’t seem to find it now. Closest thing I can find is this: http://java.sun.com/docs/books/tutorial/essential/concurrency/
Using some other keywords in Google turned-up a few promising results, as well as tutorials:
java threads
java multithreading
java synchronized
java volatile

As for JOGL and multithreading, what sort of things are you after? Why writing GL methods outside of display(), reshape(), and init() don’t work? How to control the OpenGL context using JOGL’s GLContext class? Or the biggie: how to create a multi-threaded OpenGL application?

Hum, well, I think I have basic knowledge about threadprogramming. My special problem at the moment is a program with Swing-GUI in which a GLJPanel sits (as fast alternative to Java2D, because there are many color gradients to show, which is really slow with Java2D). But my problem is to have the GUI still reachable, while JOGL works. Because there are many objects to render in high quality and its just running on damn slow graphics cards, the render process can last up to some seconds. How can I fix this? I tried it several time, but even with glcontext.release() and glcontext.makecurrent() I just always get a black screen. I would be very happy if someone just could post a little code snippet (maybe as easy as possible) on which you can see, how to implement threading with JOGL.

P.S.: I am sorry for my bad english, but I am german :o

I think the -Dopengl.1thread=worker switch from https://jogl.dev.java.net/issues/show_bug.cgi?id=191 might solve your problem. But there were issues with this, so it is no longer the default: http://www.java-gaming.org/forums/index.php?topic=13421.0. But it may be worth to try it out.

@FatFire: Just gonna try to repeat what you said to make sure I understand what you have: a swing application, with lots of objects to be rendered, on an old graphics card? And that the rendering takes so long, that it takes processing time away from having the GUI react in real-time?

If I’ve misunderstood you, then ignore the rest of this post :stuck_out_tongue:

If you’re on a single-core CPU, then I think the problem here isn’t really about trying to multi-thread the program, but rather that you don’t have enough processing power to do everything you want. You could try to reduce how much CPU time the Animator takes, by using the FPSAnimator instead, and setting a ‘good enough’ (low) framerate. (I think) The FPSAnimator only takes as much time as it needs to reach the desired framerate, leaving the CPU free when it’s not rendering.
This should free-up CPU time to help with the responsiveness of your Swing GUI.

If you have lots of complicated objects to render, you might want to think about optimizing the rendering of those objects. If you’re using OpenGL ‘immediate mode’ to render your objects (that is, when your rendering code has lots of glBegin() glEnd() functions), you might want to read about OpenGL display lists and vertex arrays to speed-up rendering times. Immediate mode is SLOW.
Putting ‘OpenGL performance optimize’ into Google gave me a few hits on pages which might help.

Yes, I am on a single core cpu, but I don’t even use the Animator, because it’s no animation. It’s a digital terrain model, which is only rendered if the view to this model changes, normal repaint events are just a little bytebuffer work. But rendering of even one of those images lasts up to 2-3 seconds in which the GUI just freezes.

I looked at some optimiziation hints in Google, but the needed functions (like glGenBuffersARB oder glBufferDataARB) aren’t supported by the graphics card I have to use. It’s just this lousy Matrox Millennium G450. Yeah, I know, no hardware acceleration at all. But I have no other options.

Have you tried the -Dopengl.1thread=worker switch? Does it help?

Yes, I tried it, but screen is still black. Maybe some hints, what I do wrong?

Ah sorry. This switch has nothing to do with your makeCurrent() problem - it is for detaching the JOGL calls from the AWT event queue, so you can use the standard approach without freezing the GUI. Just implement the GLEventListener and call display() on the GLDrawable (e.g. your GLCanvas - not on the GLEventListener implementation!).

Ah, the Animator is just a name. It doesn’t actually animate anything, it just keeps calling the display() method of your GLDrawables (eg: your GLCanvas). It’s more of an auto-renderer thread than some sort of animation maker.