2017-04-15 68 views
1

我目前正在使用Java和LWJGL 3,並且正在爲頂點數組對象,頂點緩衝對象等寫東西。 現在,刪除它是個好習慣這些對象在程序退出之前,所以我創建了一個應該清理的關閉鉤子。LWJGL - OpenGL上下文在關機鉤丟失

但是,當我在關閉鉤子內調用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) 

有誰知道爲什麼上下文被自動銷燬?

如果你需要任何額外的信息就這麼說。

+0

關閉掛鉤的目的是什麼? 'glfwWindowShouldClose'不夠好嗎? –

+1

請注意,OpenGL上下文綁定到一個特定的線程。由於您正在產生一個新線程,因此您必須在使用之前綁定上下文。 – BDL

+0

@NicolBolas不,我想將此接口與其他類分離,所以我不想調用disposeAll方法或其他類。 – RagingRabbit

回答

1

OpenGL上下文總是與一個線程(或沒有線程)關聯。函數只能在上下文綁定的線程的特定上下文中調用。

由於關閉鉤子啓動一個新的線程,你必須在發出任何命令之前將OpenGL上下文綁定到該線程。