我在 LWJGL 3 中使用 OpenGL 并收到以下错误;
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
at org.lwjgl.opengl.GL.getCapabilities(GL.java:157)
at org.lwjgl.opengl.GL11.getInstance(GL11.java:1390)
at org.lwjgl.opengl.GL11.glClearColor(GL11.java:1842)
at com.base.engine.RenderUtil.initGraphics(RenderUtil.java:13)
at com.base.engine.Main.<init>(Main.java:14)
at com.base.engine.Main.main(Main.java:24)
这是 RenderUtil 类,其中从主类的构造函数调用 initGraphics。我还尝试在使用 GLFW 创建窗口后调用 initGraphics,这也生成了类似的错误消息。
package com.base.engine;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;
public class RenderUtil {
public static void clearScreen() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
public static void initGraphics() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_FRAMEBUFFER_SRGB);
}
}
另外,我没有使用多线程。要创建一个窗口,我调用该方法Window.createWindow(1366, 768, "Test");
从我的主要方法。
````
私有静态长窗口;
public static String createWindow(int width, int height, String title) {
if (GLFW.glfwInit() == 0) {
return "GLFW failed to initialise.";
}
GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, 4);
window = GLFW.glfwCreateWindow(width, height, title,
GLFW.glfwGetPrimaryMonitor(), 0);
if (window == null) {
GLFW.glfwTerminate();
return "Failed to create window.";
}
GLFW.glfwMakeContextCurrent(window);
return "GLFW has established a window.";
}
I have tried putting `RenderUtil.initGraphics();` two different position in my main method, both resulting in errors.
private boolean isRunning = false;
private Game game;
// This is the constructor
public Main() {
// Pos 1 - RenderUtil.initGraphics();
isRunning = false;
game = new Game();
}
public static void main(String[] args) {
System.out.println(Window.createWindow(1366, 768, "Test"));
// Pos 2 - RenderUtil.initGraphics();
Main game = new Main();
game.start();
}