2017-04-21 80 views
0

我試圖使用LWJGL使用入門示例(從https://www.lwjgl.org/guide {這未改變很好地工作}),但改變它,因此它使用OpenGL ES 3.0而不是(與這個問題無關的原因)。在當前線程中沒有OpenGL ES上下文當前線程 - 使用OpenGL ES 3.0的LWJGL

我正在使用最新的LWJGL版本3.1.1,選擇Minimal OpenGL ES作爲我的預設(從https://www.lwjgl.org/download)並使用Windows本機。 (我正在運行Windows 10 64位)

我被困 - 我不知道如何解決它,並讓它運行。

我得到的錯誤是:

異常線程「main」 java.lang.IllegalStateException:有在當前線程沒有OpenGL ES的背景電流。

完整的錯誤:

Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL ES context current in the current thread. 
at org.lwjgl.opengles.GLES.createCapabilities(GLES.java:222) 
at com.test.desktop.HelloWorld.loop(HelloWorld.java:93) 
at com.test.desktop.HelloWorld.run(HelloWorld.java:31) 
at com.test.desktop.HelloWorld.main(HelloWorld.java:112) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

完整的源:

package com.test.desktop; 

import org.lwjgl.*; 
import org.lwjgl.glfw.*; 
//import org.lwjgl.opengl.*; 
import org.lwjgl.opengles.GLES; 
import org.lwjgl.system.*; 

import java.nio.*; 

import static org.lwjgl.glfw.Callbacks.*; 
import static org.lwjgl.glfw.GLFW.*; 
//import static org.lwjgl.opengl.GL11.*; 
import static org.lwjgl.opengles.GLES20.*; 
import static org.lwjgl.opengles.GLES30.*; 
import static org.lwjgl.system.MemoryStack.*; 
import static org.lwjgl.system.MemoryUtil.*; 

public class HelloWorld { 

    // The window handle 
    private long window; 

    static final int WIDTH = 1024; 
    static final int HEIGHT = 768; 

    public void run() { 
     System.out.println("Hello LWJGL " + Version.getVersion() + "!"); 

     init(); 
     loop(); 

     // Free the window callbacks and destroy the window 
     glfwFreeCallbacks(window); 
     glfwDestroyWindow(window); 

     // Terminate GLFW and free the error callback 
     glfwTerminate(); 
     glfwSetErrorCallback(null).free(); 
    } 

    private void init() { 
     // Setup an error callback. The default implementation 
     // will print the error message in System.err. 
     GLFWErrorCallback.createPrint(System.err).set(); 

     // Initialize GLFW. Most GLFW functions will not work before doing this. 
     if (!glfwInit()) 
      throw new IllegalStateException("Unable to initialize GLFW"); 

     // Configure GLFW 
     glfwDefaultWindowHints(); // optional, the current window hints are already the default 
     glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation 
     glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable 

     // Create the window 
     window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL); 
     if (window == NULL) 
      throw new RuntimeException("Failed to create the GLFW window"); 

     // Setup a key callback. It will be called every time a key is pressed, repeated or released. 
     glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { 
      if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) 
       glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop 
     }); 

     // Get the resolution of the primary monitor 
     GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 

     // Center the window 
     glfwSetWindowPos(
       window, 
       (vidmode.width() - WIDTH)/2, 
       (vidmode.height() - HEIGHT)/2 
     ); 

     // Make the OpenGL context current 
     glfwMakeContextCurrent(window); 

     // Enable v-sync 
     glfwSwapInterval(1); 

     // Make the window visible 
     glfwShowWindow(window); 
    } 

    private void loop() { 
     // This line is critical for LWJGL's interoperation with GLFW's 
     // OpenGL context, or any context that is managed externally. 
     // LWJGL detects the context that is current in the current thread, 
     // creates the GLCapabilities instance and makes the OpenGL 
     // bindings available for use. 
     GLES.createCapabilities(); 
     // GL.createCapabilities(); 

     // Set the clear color 
     glClearColor(1.0f, 0.0f, 0.0f, 0.0f); 

     // Run the rendering loop until the user has attempted to close 
     // the window or has pressed the ESCAPE key. 
     while (!glfwWindowShouldClose(window)) { 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer 

      glfwSwapBuffers(window); // swap the color buffers 

      // Poll for window events. The key callback above will only be 
      // invoked during this call. 
      glfwPollEvents(); 
     } 
    } 

    public static void main(String[] args) { 
     new HelloWorld().run(); 
    } 
} 

無論值得注意的是...如果我取消:

import org.lwjgl.opengl.*; 

而且,變化:

GLES.createCapabilities(); 

而且,使用替代以下:

GL.createCapabilities(); 

我得到這個錯誤:線程 「main」 java.lang.IllegalStateException

例外:無GLESCapabilities例如當前線程設置。可能的解決方案:

回答

1

管理找到解決方案(從http://bedroomcoders.co.uk/gles2-0-everywhere-thanks-to-lwjgl3/)。

我取消:在glfwCreateWindow函數被調用的的init()功能

import org.lwjgl.opengl.*; 

前,說:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); 

而且,加入到結束以下init()函數:

// Bypasses the default create() method. 
Configuration.OPENGLES_EXPLICIT_INIT.set(true); 
GLES.create(GL.getFunctionProvider()); 

這工作,因爲它使用org.lwjgl.opengles.GL函數的地址,而不是org.lwjgl.opengles.GLES。在Windows上,他們使用OpenGL本地函數地址,無論(有一些例外)。

「OpenGL 4.3提供了與OpenGL ES 3.0完全兼容」(來自https://en.wikipedia.org)。

相關問題