Hello Guest

How to get the current GL context

  • 13 Replies
  • 19543 Views
How to get the current GL context
« on: June 11, 2012, 09:24:13 »
Hi,

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

Cheers!

Mik
--

Re: How to get the current GL context
« Reply #1 on: June 13, 2012, 01:38:12 »
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.

Re: How to get the current GL context
« Reply #2 on: June 13, 2012, 07:25:23 »
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!

Re: How to get the current GL context
« Reply #3 on: June 14, 2012, 21:14:08 »
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.

Re: How to get the current GL context
« Reply #4 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.

Re: How to get the current GL context
« Reply #5 on: June 18, 2012, 15:37:12 »
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

Re: How to get the current GL context
« Reply #6 on: June 19, 2012, 01:04:13 »
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.

Re: How to get the current GL context
« Reply #7 on: June 19, 2012, 08:14:34 »
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

*

Offline spasi

  • *****
  • 2206
    • WebHotelier
Re: How to get the current GL context
« Reply #8 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.

Re: How to get the current GL context
« Reply #9 on: June 19, 2012, 17:04:56 »
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

*

Offline Matzon

  • *****
  • 2242
Re: How to get the current GL context
« Reply #10 on: June 20, 2012, 06:29:34 »
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 ;)

Re: How to get the current GL context
« Reply #11 on: June 20, 2012, 16:55:41 »
Hi Spasi,

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


Re: How to get the current GL context
« Reply #12 on: August 25, 2012, 21:03:40 »
I believe I have a need similar to the OP's.

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. :)

*

Offline spasi

  • *****
  • 2206
    • WebHotelier
Re: How to get the current GL context
« Reply #13 on: August 26, 2012, 08:17:19 »
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.