Lwjgl 3,如何获取当前线程中的OpenGL上下文?

我正在使用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"); 从我的主要方法。

  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."; } 

我曾尝试把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(); } 

createWindow方法的末尾添加对GLContext.createFromCurrent()的调用。

这个方法需要设置LWJGL GL **类所使用的上下文。

编辑:

由于最新的夜晚(3.0.0b#11),这不再起作用,因为GLContext类不再存在。 相反,在createWindow方法的末尾添加GL.createCapabilities()

要初始化和使用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