我在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);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我不使用多线程.要创建一个窗口,我Window.createWindow(1366, 768, "Test");从main方法中调用该方法.
private static Long window;
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.";
}
Run Code Online (Sandbox Code Playgroud)
我尝试RenderUtil.initGraphics();在我的main方法中放置两个不同的位置,都导致错误.
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();
}
Run Code Online (Sandbox Code Playgroud)
Sri*_*ati 12
GLContext.createFromCurrent()在createWindow方法的末尾添加一个调用.
需要此方法来设置LWJGL GL**类使用的上下文.
编辑:
自从最新的每晚(3.0.0b#11)以来,这已不再适用,因为该GLContext课程不再存在.而是GL.createCapabilities()在createWindow方法的最后添加.