Obtaining OpenGL Context Handle without Passing as a Variable.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Obtaining OpenGL Context Handle without Passing as a Variable.

millerni456
Hey all,

I'm not sure how to do this (or if it is even possible).

Here is what I don't want to do.  I don't want to keep a handle of OpenGL's context (using GL2 data type) and pass it around each function that has to perform OpenGL operations.  Instead, I wanted to know if I can access the current OpenGL context handle via some functions in the JOGL library.

NOTE: I am not talking about switching the context to a different thread!

Also,  if it is possible to grab the current context without passing as a parameter or using a global variable, then is there any significant performance drop when accessing that context's handle?  I understand there is a performance drop when switching the context's thread, but again... this is not what I'm seeking to do.


I'm just looking for some guidance because I don't want to have GL2 in every method declaration in my application
EDIT: I just want to make it clear that I don't want any global variables either.  I'm trying to make the OpenGL operations hidden from the outside world.  (I guess a private global variable would do, but I'm still curious about accessing the thread's current OpenGL context handle.)

Thanks everyone!
- Nick Miller
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining OpenGL Context Handle without Passing as a Variable.

jmaasing
I hope some of the more experienced devs can correct me if I'm wrong.
Depending on circumstances (for example using windowed mode, NEWT/AWT and so on) the GLContext might change between frames. So it is a lot better to get it from JOGL each frame and pass it around rather than hanging on to it in some global variable.

Personally I think global or Thread local variables are a poor design, you should never hide resource usage, but tastes are different :-)
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining OpenGL Context Handle without Passing as a Variable.

gouessej
Administrator
In reply to this post by millerni456
jmaasing is right.

Never store a GL instance into a field, it should be a local variable in the worst case. Why not using GLContext.getCurrentGL()? If the OpenGL context is current on this thread, you'll get a non null value.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining OpenGL Context Handle without Passing as a Variable.

millerni456
Awesome, just what I needed:

GLContext.getCurrentGL()

Thanks much!