Lwjgl启动时出现错误代码0x502

时间:2017-07-20 00:48:55

标签: java lwjgl

当我开始Lwjgl时,我得到了

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)

在寻找这个问题的答案时,要么使用GLContext.createFromCurrent()(Lwjgl 3.1.2中不再存在),要么将GL.createCapabilities()添加到代码中,这是创建此代码的命令错误。我在Lwjgl上启用了调试模式,我收到了这个错误:

[LWJGL] An OpenGL context was in an error state before the creation of its capabilities instance. Error: 0x502

我尝试重新启动IDE和计算机。我该怎么做才能解决/解决这个问题。

修改 在做了一些麻烦之后,这不是问题的根源。如果我完全避免启动Lwjgl并且不调用GL.createCapabilities(),我会收到此错误:

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)

此时我不知道出了什么问题。我检查了我的驱动程序是否已更新,GL Viewer说我的gpu支持OpenGL 3.0 - 4.4。这是我正在使用的代码(主要是从Lwjgl入门页面复制)

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()finish()从未被调用,试图找到问题的根源。当我运行上面的代码时,我总是得到之前发布的RuntimeException。关于如何解决它的任何想法?

2 个答案:

答案 0 :(得分:0)

我不确定这是否可行但您可以尝试将类中的构造函数更改为方法,然后在gl.createCapabilities之前的start方法中调用该方法,就像在调用start方法的main方法中一样构造函数。这解决了我的问题。

答案 1 :(得分:0)

您没有在任何地方致电glfwMakeContextCurrent(window);