Search code examples
javaopengllwjglglfw

Error while creating basic GLFW window with LWJGL 3.2.3 build 13


I get this error There is no OpenGL context current in the current thread when trying to display a frame. The line where it occurs may be this one GL.createCapabilities(); (in the private method Window.init) and i think it fails because the glcontext is not initialized or something like this.

here's the code :

package com.engine.window;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.NULL;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;

/**
 * The class {@code Window} represents a graphical frame.
 * This is an abstract class that cannot be instantiated directly.
 */
public  class Window { //abstract
    public enum DisplayMode { FULLSCREEN, WINDOWED, BORDERLESS };

    private DisplayMode mode;
    private long window;
    private java.awt.Rectangle bounds;

    public Window(String title, int x, int y, int width, int height) {
        GLFWErrorCallback.createPrint(System.err).set();
        init();

        if ((window = glfwCreateWindow(width, height, title, NULL, NULL)) != NULL) {
            bounds = new java.awt.Rectangle(x, y, width, height);
            glfwSetWindowPos(window, x, y);
            glfwSetWindowSize(window, width, height);

            glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
                if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
                    glfwSetWindowShouldClose(window, true);
                }
            });

            glfwMakeContextCurrent(window);
            GL.createCapabilities();
            glfwShowWindow(window);
            glfwFocusWindow(window);
        } else {
            throw new RuntimeException("Error: Failed to create the GLFW window.");
        }
    }

    private void init() throws RuntimeException {
        if (glfwInit()) {
            glfwDefaultWindowHints();
            glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
            long tmp = glfwCreateWindow(1, 1, "", NULL, NULL); //creating a temporary window to get the available opengl version
            if (tmp != NULL) {
                glfwMakeContextCurrent(tmp);
                GL.createCapabilities();
                GLCapabilities capabilities = GL.getCapabilities();
                glfwDestroyWindow(tmp);
            
                glfwDefaultWindowHints(); //resets window hints
                if (capabilities.OpenGL32) {
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
                    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoids using old opengl
                    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); //for mac os user
                } else if (capabilities.OpenGL21) {
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
                    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
                } else {
                    throw new RuntimeException("Error: Neither OpenGL 3.2 nor OpenGL 2.1 is supported.");
                }
                glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
                glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
                glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE);
                glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
                glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE);
                glfwWindowHint(GLFW_SAMPLES, 4); //enables anti-aliasing
            } else {
                throw new RuntimeException("Error: Failed to create the GLFW window.");
            }    
        } else {
            throw new RuntimeException("Error: Failed to Initialize GLFW library.");
        }
    }

    public void setMode(DisplayMode displayMode) {
        long monitor = glfwGetPrimaryMonitor();
        GLFWVidMode videoMode = glfwGetVideoMode(monitor);
        if (displayMode == DisplayMode.FULLSCREEN) {
            glfwSetWindowMonitor(window, monitor, 0, 0, videoMode.width(), videoMode.height(), 0);
        } else if (displayMode == DisplayMode.WINDOWED) {
            glfwSetWindowMonitor(window, NULL, bounds.x, bounds.y, bounds.width, bounds.height, 0);
        } else if (displayMode == DisplayMode.BORDERLESS) {
            glfwWindowHint(GLFW_RED_BITS,videoMode.redBits());
            glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits());
            glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits());
            glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate());
            glfwSetWindowMonitor(window, monitor, 0, 0, videoMode.width(), videoMode.height(), videoMode.refreshRate());
        }
        mode = displayMode;
    }

    public void setMode(String displayMode) {
        setMode(DisplayMode.valueOf(displayMode));
    }

    public void setVSync(boolean verticalSync) {
        glfwMakeContextCurrent(window);
        glfwSwapInterval(verticalSync ? 1 : 0);
    }

    public void pollEvents() {
        glfwPollEvents();
    }

    public void swapBuffers() {
        glfwSwapBuffers(window);
    }

    public boolean shouldClose() {
        return glfwWindowShouldClose(window);
    }

    /**
     * Release the resources and destroys windows and cursors.
     */
    public void dispose() {
        glfwDestroyWindow(window);
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }
}

The trace:

[java] [LWJGL] GLFW_NO_WINDOW_CONTEXT error
     [java]     Description : Cannot make current with a window that has no OpenGL or OpenGL ES context
     [java]     Stacktrace  :
     [java]             org.lwjgl.glfw.GLFW.glfwMakeContextCurrent(GLFW.java:4522)
     [java]             com.engine.window.Window.<init>(Window.java:42)
     [java]             com.engine.window.Stage.<init>(Stage.java:14)
     [java]             com.engine.window.Stage.create(Stage.java:9)
     [java]             com.engine.application.Application.onstart(Application.java:73)
     [java]             com.engine.application.Application.setup(Application.java:60)
     [java]             com.engine.application.Plateform.launchApplication(Plateform.java:27)
     [java]             com.engine.application.Application.launch(Application.java:39)
     [java]             Launcher.main(Launcher.java:9)
     [java] [LWJGL] An OpenGL context was in an error state before the creation of its capabilities instance. Error: 0x502
     [java] Exception in thread "main" java.lang.RuntimeException: Error: Failed to create and initialize a new instance of the constructor's declaring class : class Launcher.
     [java]     at com.engine.application.Plateform.launchApplication(Plateform.java:29)
     [java]     at com.engine.application.Application.launch(Application.java:39)
     [java]     at Launcher.main(Launcher.java:9)
     [java] Caused by: java.lang.IllegalStateException: There is no OpenGL context current in the current thread.
     [java]     at org.lwjgl.opengl.GL.createCapabilities(GL.java:378)
     [java]     at org.lwjgl.opengl.GL.createCapabilities(GL.java:322)
     [java]     at com.engine.window.Window.<init>(Window.java:43)
     [java]     at com.engine.window.Stage.<init>(Stage.java:14)
     [java]     at com.engine.window.Stage.create(Stage.java:9)
     [java]     at com.engine.application.Application.onstart(Application.java:73)
     [java]     at com.engine.application.Application.setup(Application.java:60)
     [java]     at com.engine.application.Plateform.launchApplication(Plateform.java:27)
     [java]     ... 2 more
     [java] Java Result: 1

Solution

  • In init() you explicitly tell GLFW to not create any OpenGL context for any further created window by calling glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);.

    So, the window you create after init() returned will not have an OpenGL context created for it.

    The default for the GLFW_CLIENT_API window hint is GLFW_OPENGL_API, so if you want the second window you create to also have an OpenGL context, simply not set that window hint.