Threading problem in JOGL

Dear All,

           I have a threading problem while running a JOGL program. When i use Animator of JOGL, it calls display method frequently. In display method i have cleared background and rendered some text. With animator it works fine.

          But when i put same code in thread it does not work.

          Can anyone explain me by giving example ?. 
          Any short example will be appreciated me using thread.

Thanks In Advance.
Vinod

Why do you want to do it in a different thread at all? Multithreading with opengl is complicated and error prone. The JOGL developers put great effort in making JOGL work reliable on different platforms by abstracting the neccessary steps with the Animator API.

So what do you want to achieve and is this only possible by avoiding the Animator API?

Dear cylab,

            There is no strong reason to use thread but existing C++ 3D application run OpenGL animation using thread. I want to keep same application architecture as c++. Because c++ application architecture is very good and i want same in Java.

           If possible, please give one example to run animation usiing thread.

Thanks
Vinod Patel

For one, the Animator class creates a Java thread which runs your 3D app, so you are using a thread to run your 3D app when you use the Animator.

I don’t understand your first post though, when you said But when i put same code in thread it does not work.
Did you try to create your own Java thread to do the same task as the Animator?

If yes, some things to check:

  • is there a current OpenGL context on the thread that does the rendering? (call GLContext.makeCurrent(), and check the return code against the constants of that class to see if it there is a current OpenGL context)
  • if you have another OpenGL context somewhere, be sure to release it (GLContext.release()) before starting your rendering thread. When you do start your rendering thread, create a new context (new GLContext()) and make it current (GLContext.makeCurrent()). If that last call to makeCurrent() succeeded, then you should be able to to do any OpenGL operations (like calling the rendering methods of the OpenGL objects in your app)

When using the Animator, you don’t normally have to deal with things like the OpenGL context, as I believe it takes care of those things for you.

vinodpatel, if you have a complex drawing operation, just do it on your main thread, then pass the optimized data over to the GL thread as raw vertex, color, normal, etc.

to visualize how a JOGL app looks in terms of threads google ThreadLister.java. it’s a small snippet of code that poles the current VM threads and shows their hierarchy and priority. or you can roll your own from calls to Runtime.

if you do so, you’ll see that the generic setup is something like:

  • some virtual machine threads ( secret stuff for running the VM )

  • some AWT threads ( mouse, windows, etc. ) – this is where your GLEventListener is working and living ( your renderer that responds to display() .

  • then you get some threads under main. this is your app. so in a way, you already have a thread going… your app. so unless this thing you’re drawing is so complex ( a 20 dimensional super surface, or some mathematical insanity ), there’s no reason to spawn a thread to draw it. and you MOST definitely don’t want to do it inside your display() method!!!

what you are looking to do probably is create an object which knows how to “draw” what you want, that is convert meaningful data from you into a big list of vertices, colors, normals, etc. this all happens in your main thread. then, every frame over at the JOGL thread, display() can call some method in your main thread, something like public synchronized BigListType getAllTheInfoNeedToDrawAThingy(). Then, the renderer can speedily whip through your data plugging your vertex info into raw OpenGL calls.

you’ll want to limit the amount of data you send to the opengl thread as much as possible. so if you are drawing the same thing over and over and just moving it around, etc. just make an opengl list, or vertex array, or vertex buffer.

hope that helps, and i hope i have the information i’ve given correct – please say so if i have it wrong gurus…

cheers