1

I have two threads running in my program. When I try to use OpenGL functions in the thread that is not my main, it throws an IllegalStateException:No OpenGL context is current in the current thread. So my question is, how do I pass the context from the main thread to a different thread? I am using LWJGL 3.

2 Answers 2

2

I think I solved it but I'm not 100% sure it's the best way to do it. You have to call GLFW.glfwMakeContextCurrent(MemoryUtil.NULL); in the thread you want to depart from, then call GLFW.glfwMakeContextCurrent(window); GLContext.createFromCurrent(); from the thread you want to switch to.

4
  • Usually you just create 2 contexts. Try googling opengl shared context.
    – Jerem
    Nov 5, 2015 at 9:27
  • What you do works too, but will be very slow if you need to change current context often.
    – Jerem
    Nov 5, 2015 at 9:28
  • Thanks. Would you mean showing how to create two different contexts? There are literally no examples I could find. I've tried GLContext.createFromCurrent(); and since that returns a GLContext object, I thought that I could just call it twice and store the GLContext in two different variables, and call GL.setCurrent(otherContext) in the other thread, but none of my OGL calls have any effect.
    – hacksoi
    Nov 6, 2015 at 23:38
  • I'm sorry, it's not 100% clear, is the windowID for the window your want to switch to, its own ID or is the ID of the previous window? Also GLContext.createFromCurrent(); is replaced with GL.createCapabilities()?
    – user5644457
    Aug 3, 2018 at 1:00
0

Take a look at this example. It uses a Drawable that you can use in your two Java Threads. I'm not entirely sure if it still works since a lot of this class is associated with LWJGL 2. I'll test it soon and let you know if this or your method is more efficient (Gonna create a tiny micro-benchmark :) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.