使用 LWJGL 3.2.3 build 13 创建基本 GLFW window 时出错

Error while creating basic GLFW window with LWJGL 3.2.3 build 13

我在尝试显示帧时收到此错误当前线程中没有当前的 OpenGL 上下文。它出现的行可能是 GL.createCapabilities(); (在私有方法 Window.init 中)我认为它失败是因为 glcontext 未初始化或像这样。

这是代码:

package com.engine.window;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.NULL;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;

/**
 * The class {@code Window} represents a graphical frame.
 * This is an abstract class that cannot be instantiated directly.
 */
public  class Window { //abstract
    public enum DisplayMode { FULLSCREEN, WINDOWED, BORDERLESS };

    private DisplayMode mode;
    private long window;
    private java.awt.Rectangle bounds;

    public Window(String title, int x, int y, int width, int height) {
        GLFWErrorCallback.createPrint(System.err).set();
        init();

        if ((window = glfwCreateWindow(width, height, title, NULL, NULL)) != NULL) {
            bounds = new java.awt.Rectangle(x, y, width, height);
            glfwSetWindowPos(window, x, y);
            glfwSetWindowSize(window, width, height);

            glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
                if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
                    glfwSetWindowShouldClose(window, true);
                }
            });

            glfwMakeContextCurrent(window);
            GL.createCapabilities();
            glfwShowWindow(window);
            glfwFocusWindow(window);
        } else {
            throw new RuntimeException("Error: Failed to create the GLFW window.");
        }
    }

    private void init() throws RuntimeException {
        if (glfwInit()) {
            glfwDefaultWindowHints();
            glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
            long tmp = glfwCreateWindow(1, 1, "", NULL, NULL); //creating a temporary window to get the available opengl version
            if (tmp != NULL) {
                glfwMakeContextCurrent(tmp);
                GL.createCapabilities();
                GLCapabilities capabilities = GL.getCapabilities();
                glfwDestroyWindow(tmp);
            
                glfwDefaultWindowHints(); //resets window hints
                if (capabilities.OpenGL32) {
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
                    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoids using old opengl
                    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); //for mac os user
                } else if (capabilities.OpenGL21) {
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
                } else {
                    throw new RuntimeException("Error: Neither OpenGL 3.2 nor OpenGL 2.1 is supported.");
                }
                glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
                glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
                glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE);
                glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
                glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE);
                glfwWindowHint(GLFW_SAMPLES, 4); //enables anti-aliasing
            } else {
                throw new RuntimeException("Error: Failed to create the GLFW window.");
            }    
        } else {
            throw new RuntimeException("Error: Failed to Initialize GLFW library.");
        }
    }

    public void setMode(DisplayMode displayMode) {
        long monitor = glfwGetPrimaryMonitor();
        GLFWVidMode videoMode = glfwGetVideoMode(monitor);
        if (displayMode == DisplayMode.FULLSCREEN) {
            glfwSetWindowMonitor(window, monitor, 0, 0, videoMode.width(), videoMode.height(), 0);
        } else if (displayMode == DisplayMode.WINDOWED) {
            glfwSetWindowMonitor(window, NULL, bounds.x, bounds.y, bounds.width, bounds.height, 0);
        } else if (displayMode == DisplayMode.BORDERLESS) {
            glfwWindowHint(GLFW_RED_BITS,videoMode.redBits());
            glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits());
            glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits());
            glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate());
            glfwSetWindowMonitor(window, monitor, 0, 0, videoMode.width(), videoMode.height(), videoMode.refreshRate());
        }
        mode = displayMode;
    }

    public void setMode(String displayMode) {
        setMode(DisplayMode.valueOf(displayMode));
    }

    public void setVSync(boolean verticalSync) {
        glfwMakeContextCurrent(window);
        glfwSwapInterval(verticalSync ? 1 : 0);
    }

    public void pollEvents() {
        glfwPollEvents();
    }

    public void swapBuffers() {
        glfwSwapBuffers(window);
    }

    public boolean shouldClose() {
        return glfwWindowShouldClose(window);
    }

    /**
     * Release the resources and destroys windows and cursors.
     */
    public void dispose() {
        glfwDestroyWindow(window);
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }
}

踪迹:

[java] [LWJGL] GLFW_NO_WINDOW_CONTEXT error
     [java]     Description : Cannot make current with a window that has no OpenGL or OpenGL ES context
     [java]     Stacktrace  :
     [java]             org.lwjgl.glfw.GLFW.glfwMakeContextCurrent(GLFW.java:4522)
     [java]             com.engine.window.Window.<init>(Window.java:42)
     [java]             com.engine.window.Stage.<init>(Stage.java:14)
     [java]             com.engine.window.Stage.create(Stage.java:9)
     [java]             com.engine.application.Application.onstart(Application.java:73)
     [java]             com.engine.application.Application.setup(Application.java:60)
     [java]             com.engine.application.Plateform.launchApplication(Plateform.java:27)
     [java]             com.engine.application.Application.launch(Application.java:39)
     [java]             Launcher.main(Launcher.java:9)
     [java] [LWJGL] An OpenGL context was in an error state before the creation of its capabilities instance. Error: 0x502
     [java] Exception in thread "main" java.lang.RuntimeException: Error: Failed to create and initialize a new instance of the constructor's declaring class : class Launcher.
     [java]     at com.engine.application.Plateform.launchApplication(Plateform.java:29)
     [java]     at com.engine.application.Application.launch(Application.java:39)
     [java]     at Launcher.main(Launcher.java:9)
     [java] Caused by: java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
     [java]     at org.lwjgl.opengl.GL.createCapabilities(GL.java:378)
     [java]     at org.lwjgl.opengl.GL.createCapabilities(GL.java:322)
     [java]     at com.engine.window.Window.<init>(Window.java:43)
     [java]     at com.engine.window.Stage.<init>(Stage.java:14)
     [java]     at com.engine.window.Stage.create(Stage.java:9)
     [java]     at com.engine.application.Application.onstart(Application.java:73)
     [java]     at com.engine.application.Application.setup(Application.java:60)
     [java]     at com.engine.application.Plateform.launchApplication(Plateform.java:27)
     [java]     ... 2 more
     [java] Java Result: 1

init() 中,您 明确地 告诉 GLFW 不要 为任何进一步创建的 window 创建任何 OpenGL 上下文通过调用 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);.

因此,您在 init() 返回后创建的 window 将 不会 为其创建 OpenGL 上下文。

GLFW_CLIENT_API window 提示的默认值是 GLFW_OPENGL_API,因此如果您希望创建的第二个 window 也具有 OpenGL 上下文,只需 设置window提示。