【发布时间】:2015-02-23 22:03:46
【问题描述】:
我在 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)
这是从我的主类的构造函数调用 initGraphics 的 RenderUtil 类。在使用 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);
}
}
另外,我没有使用多线程。为了创建一个窗口,我从我的 main 方法中调用了 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();
}
【问题讨论】:
-
它应该已经是最新的,除非你在不同的线程中创建了你的上下文。尽管如此,每个窗口系统都有自己的
...MakeCurrent函数来执行此操作。 LWJGL 也不例外,GLContext.makeCurrent (long)包装了平台特定的东西。 -
你能展示你创建窗口和上下文的代码吗?
-
@Jack:看到你的
main也会很有趣。 -
好的,我在上面添加了。