How to get the current GL context

Started by MikOfClassX, June 11, 2012, 09:24:13

Previous topic - Next topic

MikOfClassX

Hi,

is there a way to get the current GL context statically ?

Cheers!

Mik
--

avm1979

Display.makeCurrent() and Display.isCurrent() should be enough to manage using OpenGL from different threads - is there a reason you need to get at the context object itself? AFAIK it's internal to LWJGL and not useful externally, but maybe I'm missing something.

MikOfClassX

It's not just matter of Display()

Imagine different threads each one with its own Drawable, shared from a common Drawable (a PBuffer, normally).

Now imagine you want to store some data per-thread. Here you have the ThreadLocal and that's a good solution.

But what happens if you want to store data per-Drawable (i.e. the current FBO binding etc.). Now you have a problem, as you don't know which Drawable is current. We miss a call like GLU.getCurrentDrawable();

This is a good example (JOGL based): http://mt4j.googlecode.com/svn-history/r338/trunk/src/org/mt4j/util/opengl/GLFboStack.java

Cheers!

avm1979

Hmm. Perhaps I'm missing something, but couldn't you keep track of what context is current in your own code? Sounds like what you need is your own context class, with all that per-Drawable data in it - and then you could keep track of which one is current in the same place where you manage it for your different threads.

MikOfClassX

I could do this.

But there is already something in gl that does what I need, (wglGetCurrentContext()). It is simply not mapped in the LWJGL api.

matheus23

Quote from: MikOfClassX on June 18, 2012, 07:29:50
I could do this.

But there is already something in gl that does what I need, (wglGetCurrentContext()). It is simply not mapped in the LWJGL api.

Of course. Its a WGL function. Not supported on mac or linux. LWJGL was written to be platform-independent.
My github account and currently active project: https://github.com/matheus23/UniverseEngine

aldacron

Quote from: matheus23 on June 18, 2012, 15:37:12
Quote from: MikOfClassX on June 18, 2012, 07:29:50
I could do this.

But there is already something in gl that does what I need, (wglGetCurrentContext()). It is simply not mapped in the LWJGL api.

Of course. Its a WGL function. Not supported on mac or linux. LWJGL was written to be platform-independent.

But there are equivalents for Mac and Linux (glxGetCurrentContext and CGLGetCurrentContext) that could be wrapped behind a common interface. It would be a useful thing to have.

MikOfClassX

Yes, as Aldacron outlined this would be perfect cross platform feature of LWJGL. Maybe a look at the JOGL GLU code is worth the time.

http://download.java.net/media/jogl/builds/archive/jsr-231-beta5/javadoc_public/javax/media/opengl/glu/GLU.html#getCurrentGL%28%29

spasi

I don't see why you'd need such an API. It's only useful for native contexts, it has nothing to do with FBOs. Also, these functions return native handles, which will have to be wrapped by LWJGL somehow. Something like that will have to happen in 3.0 anyway (we'll probably expose more low-level stuff than we do now), but until then I don't see any significant benefit to exposing such functionality.

matheus23

Quote from: spasi on June 19, 2012, 10:50:41
I don't see why you'd need such an API. It's only useful for native contexts, it has nothing to do with FBOs. Also, these functions return native handles, which will have to be wrapped by LWJGL somehow. Something like that will have to happen in 3.0 anyway (we'll probably expose more low-level stuff than we do now), but until then I don't see any significant benefit to exposing such functionality.
Some offtopic: is it possible for us to check-out the "3.0-branch"?
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Matzon

It doesn't exist yet - it's some kind of mythical beast that requires a f*ckton of work, that no one wants to do ;)

MikOfClassX

Hi Spasi,

It seems you already have it all. Just make ContextGL.getCurrentContext() public.


pdinklag

I believe I have a need similar to the OP's.

Quote from: spasi on June 19, 2012, 10:50:41
I don't see why you'd need such an API. It's only useful for native contexts, it has nothing to do with FBOs. Also, these functions return native handles, which will have to be wrapped by LWJGL somehow. Something like that will have to happen in 3.0 anyway (we'll probably expose more low-level stuff than we do now), but until then I don't see any significant benefit to exposing such functionality.
I have a GL context created by another application and would like to render on it in a Java application using LWJGL. The only thing preventing me is that there seems no way to use that context in LWJGL - I can only create a new one (by creating a Display, for instance).

I believe I see the problem - in order to wrap LWJGL around a native context, it would have to know about its capabilities to map the functions correctly. I could personally imagine something like GLContext.useCurrent(ContextAttribs) being enough to provide an unchecked and inherently unsafe wrapper around a native context - but that unsafety should be the responsibility of the developer who does shady things like this. It can be really useful.

Though, I might miss something here, I do not know too much about LWJGL's internals. But if something like this is due to be added in 3.0, I'm all for it. :)

spasi

See the method GLContext.useContext(Object context, boolean forwardCompatible). You can use that to pass an external context and the ContextCapabilities will be created for it. Just make sure the external context is current in the current thread before calling this method.

ContextAttribs will not be available for the external context, since LWJGL cannot query its properties from a current context.