gpt4 book ai didi

java - LWJGL - OpenGL 上下文在关闭 Hook 中丢失

转载 作者:行者123 更新时间:2023-11-30 06:47:36 24 4
gpt4 key购买 nike

我目前正在使用 Java 和 LWJGL 3,并且正在为顶点数组对象、顶点缓冲区对象等内容编写一些包装器。现在,在程序退出之前删除这些对象是一个好习惯,因此我创建了一个关闭 Hook 来执行清理工作。

但是当我在关闭 Hook 内调用 OpenGL 函数时,我收到非法状态异常,表明 OpenGL 上下文尚未初始化。

我编写了一个测试程序来重现此行为:

public static void main(String[] args) {
GLFW.glfwInit();
long window = GLFW.glfwCreateWindow(100, 100, "", 0, 0);

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
GL15.glDeleteBuffers(0);
GLFW.glfwTerminate();
}
});

while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
}
}

堆栈跟踪:

Exception in thread "Thread-0" java.lang.IllegalStateException: No GLCapabilities instance set for the current thread. Possible solutions:
a) Call GL.createCapabilities() after making a context current in the current thread.
b) Call GL.setCapabilities() if a GLCapabilities instance already exists for the current context.
at org.lwjgl.opengl.GL.getCapabilities(GL.java:241)
at org.lwjgl.opengl.GL15.nglDeleteBuffers(GL15.java:152)
at org.lwjgl.opengl.GL15.glDeleteBuffers(GL15.java:178)
at core.Main$1.run(Main.java:11)

有谁知道为什么上下文会自动销毁?

如果您需要任何额外信息,请直接说出来。

最佳答案

OpenGL 上下文始终与一个线程关联(或不与任何线程关联)。只能从上下文绑定(bind)到的线程在特定上下文上调用函数。

由于关闭 Hook 启动一个新线程,因此您必须在发出任何命令之前将 OpenGL 上下文绑定(bind)到该线程。

关于java - LWJGL - OpenGL 上下文在关闭 Hook 中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425697/

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