我在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);
}
}
另外,我没有使用多线程。要创建一个窗口,我从main方法中调用方法Window.createWindow(1366, 768, "Test");
。
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.";
}
我尝试在我的main方法中放置RenderUtil.initGraphics();
两个不同的位置,这两个位置都会导致错误。
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();
}
答案 0 :(得分:11)
在GLContext.createFromCurrent()
方法的末尾添加对createWindow
的调用。
需要此方法来设置LWJGL GL **类使用的上下文。
修改强>
由于最新的每晚(3.0.0b#11),这不再有效,因为GLContext
类不再存在。相反,请在GL.createCapabilities()
方法的末尾添加createWindow
。
答案 1 :(得分:0)
我知道此线程已有4年历史,但是如果您中有人仍然需要解决方案,您可以继续:
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
public class Main {
private static long window = 0;
public static void main(String[] args) {
if(!GLFW.glfwInit()) {
throw new RuntimeException("Cannot initialize OPenGL");
}
window = GLFW.glfwCreateWindow(1024, 764, "Ya yeet", 0, 0);
if(0 == window) {
GLFW.glfwTerminate();
throw new RuntimeException("Cannot create window");
}
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
String glVersion = GL11.glGetString(GL11.GL_VERSION);
System.out.println(glVersion);
}
}
答案 2 :(得分:-3)
要初始化并使用LWJGL 3,您需要做下一步(Kotlin中的代码):
import org.lwjgl.glfw.GLFW
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL11
import org.lwjgl.system.MemoryUtil.NULL
class VersionInfo {
var window: Long = NULL
fun run() {
initGl()
// use openGL
val glVersion = GL11.glGetString(GL11.GL_VERSION)
println("GL version: $glVersion")
}
private fun initGl() {
// check GLFW
if (!GLFW.glfwInit()) {
throw IllegalStateException("Can not initialize GLFW")
}
// create window
window = GLFW.glfwCreateWindow(1024, 764, "glfw", NULL, NULL)
// check window
if (NULL == window) {
GLFW.glfwTerminate()
throw IllegalStateException("Can not create new GLFW window")
}
// make GL context in the current thread
GLFW.glfwMakeContextCurrent(window)
// create capabilities
GL.createCapabilities()
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
VersionInfo().run()
}
}
}
有关详细信息,请参阅官方入门指南:http://www.lwjgl.org/guide
或Wiki:https://github.com/LWJGL/lwjgl3-wiki/wiki