When starting Lwjgl I got
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread.at
org.lwjgl.opengl.GL.createCapabilities(GL.java:363)
at org.lwjgl.opengl.GL.createCapabilities(GL.java:312)
at rr.industries.OpenGLTest.<init>(OpenGLTest.java:42)
at rr.industries.OpenGLTest.main(OpenGLTest.java:97)
When looking for answers for this everything said to either use GLContext.createFromCurrent()
, which no longer exists in Lwjgl 3.1.2, or to add GL.createCapabilities()
to your code, which was the command that was creating this error. I enabled debug mode on Lwjgl and I got this error:
[LWJGL] An OpenGL context was in an error state before the creation of its capabilities instance. Error: 0x502
I tried restarting my IDE and my computer. What can I do to troubleshoot / fix this.
EDIT:
After doing some trouble shooting this is not the root of the problem. If I completely avoid starting Lwjgl and don't call GL.createCapabilities()
I get this error:
Exception in thread "main" java.lang.RuntimeException: Failed to create the GLFW window
at rr.industries.OpenGLTest.<init>(OpenGLTest.java:49)
at rr.industries.OpenGLTest.main(OpenGLTest.java:102)
at this point I have no idea what is wrong. I checked that my drivers were updated and GL Viewer says my gpu supports OpenGL 3.0 - 4.4. Here is the code I am using (mostly copied from Lwjgl Getting Started page)
public class OpenGLTest {
private long window;
public static Logger LOG = LoggerFactory.getLogger(OpenGLTest.class);
public OpenGLTest(){
// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");
// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
// Create the window
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window"); //Stacktrace points to THIS line
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});
}
public void start(){
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();
// Set the clear color
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
loop();
finish();
}
public void finish(){
// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
public void loop(){
while ( !glfwWindowShouldClose(window) ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
}
public static void main(String[] args){
Configuration.DEBUG.set(true);
GLFWErrorCallback.createPrint(System.out);
LOG.info("Starting Lwjgl v{}", Version.version());
new OpenGLTest()/*.start()*/;
}
}
start()
, loop()
, and finish()
are never called trying to find the root of the problem. When I run the above code I always get the RuntimeException posted earlier. Any ideas on how to fix it?