【问题标题】:Lwjgl 3, How to get OpenGL context current in the current thread?Lwjgl 3、如何在当前线程中获取当前的OpenGL上下文?
【发布时间】: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 也会很有趣。
  • 好的,我在上面添加了。

标签: java opengl lwjgl


【解决方案1】:

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

需要此方法来设置 LWJGL GL** 类在后台使用的上下文。

编辑:

自从最新的 nightly (3.0.0b #11) 以来,这不再有效,因为 GLContext 类不再存在。而是在createWindow 方法的末尾添加GL.createCapabilities()

【讨论】:

    【解决方案2】:

    我知道这个帖子已经有 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);
    }
    }
    

    【讨论】:

      【解决方案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
      或维基:https://github.com/LWJGL/lwjgl3-wiki/wiki

      【讨论】:

      • 这没有帮助,官方的例子给了我同样的错误。即使使用最新的 lwjgl (3.0.0a build 1) 也无法编译...
      • @user2084865 网站已经使用beta版的新API,见the other answer的编辑。
      • 是的,这为我解决了。已经试过了,但无论如何,谢谢;-)
      • 原帖有一个“java”标签,那为什么你在Kotlin中的答案是无用的?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2020-04-22
      • 1970-01-01
      相关资源
      最近更新 更多