LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: xaster on March 12, 2008, 18:19:13

Title: rendering from different threads
Post by: xaster on March 12, 2008, 18:19:13
If I create my display in one thread, then want to render to it from another thread how would I accomplish this. I think it has something to do with GLContext.useContext(), but I cannot figure out how.
Title: Re: rendering from different threads
Post by: xaster on March 12, 2008, 19:32:31
well here is what I have tried with no luck,

semi-psuedo code

Thread 1
-------------------
createDisplay()

createThread2(Display.getDrawable())

Thread2.run()

Thread 2
-----------------------------------
render(Drawable d)     //value from Display.getDrawable() in thread 1
    GLContext.useContext(d.getContext())
    GLContext.useContext(c);
    GL11.glClearColor(0,0,0,0);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    Display.update()
endRender      
Title: Re: rendering from different threads
Post by: xaster on March 12, 2008, 23:08:59
Well I got some help on #LWJGL , ended up I was doing a few minor things wrong. But there a few things I don't quite grasp yet.

The steps I used to make this work are listed as follows

1) I grabbed the drawable (surface?) from within the main thread with Display.getDrawable() and saved it off into a variable to send to thread2
2) I released the context from the Display within the main thread with Display.releaseContext()
3) I then start the new thread (thread2) and then pass it the drawable surface that I had gotten in step 1
4) within thread2 I used GLContext.useContext(drawable.getContext())
5) made the context the current context with by calling Display.makeCurrent()
6) at this point I am now able to render into the window from thread2

I am confused about why this works, it seems I getting the drawable surface from display, then just turning around and re-attaching to Display. I can't quite see how this ties in with the GL11 calls and different threads.

Title: Re: rendering from different threads
Post by: vladl on April 09, 2008, 16:57:31
Hello.. Is there any way to do what xaster says, including the posibility of rendering from the main thread?
The idea is to do a 'loading' screen while all the resources are being loaded (sending messages to the second thread indicating what resources are being loaded).

Thanks in advance
Title: Re: rendering from different threads
Post by: kappa on April 09, 2008, 18:05:48
you can use different threads, have a look at the Display.releaseContext() and Display.makeCurrent().
Title: Re: rendering from different threads
Post by: vladl on April 09, 2008, 18:56:25
Quote from: javalwjgl on April 09, 2008, 18:05:48
you can use different threads, have a look at the Display.releaseContext() and Display.makeCurrent().

I already checked the wiki about using releaseContext() and makeCurrent(), but i want to know if there is any way of not releasing and making current all the time during the load of the resources.

Also, is there any way of checking if the thread in which i am, is the current?

Thanks
Title: Re: rendering from different threads
Post by: wolf_m on April 09, 2008, 21:11:05
Quote from: vladl on April 09, 2008, 18:56:25
Also, is there any way of checking if the thread in which i am, is the current?
Yes. Introduce a boolean named isCurrent into your threads and toggle it when you call releaseContext() and makeCurrent()  ;)