Rendering in a thread not possible?

Started by Blacksmid, June 02, 2011, 18:06:00

Previous topic - Next topic

Blacksmid

Hey,
I've been programming in java for a while now. Now i'm trying to make a 3d game with lwjgl. However, i want a thread to calculate and prepare the display lists for the terrain(infinity terrain). But i got a nullpointerexception. I read that you can't use opengl in 2 diffrent threads, but isn't there ANY way around this?

thanks

CodeBunny

I believe you have to share contexts, using a PBuffer or a SharedDrawable. This is what I've been told and seen in the examples, but unfortunately my machine doesn't support context sharing (which is a possible problem with your plan), so I haven't tested it myself.

Blacksmid

Hmm but does that mean only one thread can render at the time? Cause that pretty much ruins my plan. I need both threads to be able to access opengl commands at the same time, i want to make display lists for the terain, while the other one is still drawing everything.

spasi

No, you can have the main thread doing rendering, while the SharedDrawable or Pbuffer context is creating/updating resources. See org.lwjgl.test.opengl.multithread.BackgroundLoadTest in LWJGL's test package for an example.

Blacksmid

Thanks, i got it working. but what's the diffrence between the Pbuffer thing and a Shareddrawable?

spasi

SharedDrawable is more lightweight, it's just a GL context sharing objects with the Display context. Pbuffer on the other hand is a context with a framebuffer, so it requires dimensions and a PixelFormat to be specified when creating it. You could specify a 1x1 size, to minimize the extra cost, but it's just simpler to use SharedDrawable. Besides, p-buffers are kinda obsolete these days with Framebuffer Objects, you're better off avoiding them altogether.