gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:43 37 4
gpt4 key购买 nike

我在 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");。```私有(private)静态长窗口;

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

最佳答案

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

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

编辑:

从最近的 nightly (3.0.0b #11) 开始,这不再有效,因为 GLContext 类不再存在。相反,在 createWindow 方法的末尾添加 GL.createCapabilities()

关于java - lwjgl 3、如何获取当前线程中的OpenGL上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659729/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com