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



我得到这个错误当尝试显示帧时,当前线程中没有当前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不要通过调用glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);为任何进一步创建的窗口创建任何OpenGL上下文。

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

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