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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27659729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:11:34  来源:igfitidea点击:

Lwjgl 3, How to get OpenGL context current in the current thread?

javaopengllwjgl

提问by Hyman

I am using OpenGL in LWJGL 3 and I get the following error;

我在 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)

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)

This is the RenderUtil class where initGraphics is called from the constructor of my main class. I have also tried to call initGraphics after creating a window with GLFW which has also generated a similar error message.

这是 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);
    }
}

Also, I am not using multithreading. To create a window I call the method Window.createWindow(1366, 768, "Test");from my main method.

另外,我没有使用多线程。要创建一个窗口,我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.";
    }

I have tried putting RenderUtil.initGraphics();two different position in my main method, both resulting in errors.

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

回答by Sri Harsha Chilakapati

Add a call to GLContext.createFromCurrent()at the end of the createWindowmethod.

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

This method is needed to set the context used by the LWJGL GL** classes under the hood.

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

EDIT:

编辑:

Since the latest nightly (3.0.0b #11) this no longer works, as the GLContextclass no more exists. Instead, add GL.createCapabilities()at the end of the createWindowmethod.

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

回答by Alex

I know that this thread is 4 years old but if someone of you still needs a solution here you go:

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

回答by kles4eko

To init and use LWJGL 3 you need to do next (code in Kotlin):

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

For more information, please, see official get-started guide: http://www.lwjgl.org/guide
Or Wiki: https://github.com/LWJGL/lwjgl3-wiki/wiki

有关更多信息,请参阅官方入门指南:http: //www.lwjgl.org/guide
或 Wiki:https: //github.com/LWJGL/lwjgl3-wiki/wiki