当前线程中没有OpenGL

当前线程中没有OpenGL

本文介绍了当前线程中没有OpenGL ES上下文当前-使用OpenGL ES 3.0的LWJGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过入门"示例使用LWJGL(来自 https://www.lwjgl.org/指南 {保持不变的效果很好}),但对其进行了更改,使其改为使用OpenGL ES 3.0(出于与该问题无关的原因).

I am trying to use LWJGL using the Get Started example (from https://www.lwjgl.org/guide {which unaltered works fine}), but altering it so it uses OpenGL ES 3.0 instead (for reasons irrelevant to this question).

我正在使用最新的LWJGL版本3.1.1,选择最小OpenGL ES"作为我的预设(来自 https://www.lwjgl.org/download ),并使用Windows本机. (我正在运行Windows 10 64bit)

I am using the latest LWJGL Release 3.1.1, selecting Minimal OpenGL ES as my preset (from https://www.lwjgl.org/download) and using windows natives. (I am running Windows 10 64bit)

我被卡住了-我不确定如何修复它并使其运行.

I'm stuck--I'm not sure how to fix it, and get it to run.

我得到的错误是:

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

是否值得一提...如果我取消评论:

Whether it is worth noting... if I uncomment:

import org.lwjgl.opengl.*;

然后,更改:

GLES.createCapabilities();

然后,使用以下内容代替:

And, use the following instead:

GL.createCapabilities();

我收到此错误:

线程主"中的异常java.lang.IllegalStateException:当前线程未设置GLESCapabilities实例.可能的解决方案:

推荐答案

设法找到解决方案(来自 http://bedroomcoders.co.uk/gles2-0-everywhere-thanks-to-lwjgl3/).

Managed to find a solution (from http://bedroomcoders.co.uk/gles2-0-everywhere-thanks-to-lwjgl3/).

我取消评论:

import org.lwjgl.opengl.*;

init()函数中调用 glfwCreateWindow 函数之前,已添加:

Before the glfwCreateWindow function is called in the init() function, added:

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

然后,在 init()函数的末尾添加以下内容:

And, added the following to end of the init() function:

// 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本机函数地址,而不管(某些例外情况).

This works because it uses org.lwjgl.opengles.GL function addresses instead of org.lwjgl.opengles.GLES. On Windows they use OpenGL native function addresses regardless (with some exceptions).

"OpenGL 4.3提供与OpenGL ES 3.0的完全兼容性"(摘自 https://en.wikipedia.org ).

"OpenGL 4.3 provides full compatibility with OpenGL ES 3.0" (from https://en.wikipedia.org).

这篇关于当前线程中没有OpenGL ES上下文当前-使用OpenGL ES 3.0的LWJGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:50