Search code examples
javaopengllwjglglfw

Why doesn't LWJGL recognize my GLFW version?


I have written a simple program with LWJGL. The problem is that every time I try to run the app, I encounter this error:

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
    at org.lwjgl.opengl.GL11.glBegin(Native Method)
    at Main.main(Main.java:24)

The error is misleading, as I did actually call glfwMakeContextCurrent(window) and GL.createCapabilities().

I tracked the error to the glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3). It seems that when I remove this and the glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) line, the app will run.

What I do not understand is why does it happen, as I do actually have a GLFW 3.3 dependency in pom.xml.

Here is my code:

pom.xml:

<dependencies>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-glfw</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-opengl</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl</artifactId>
            <version>3.3.0</version>
            <classifier>natives-windows</classifier>
        </dependency>
        
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-glfw</artifactId>
            <version>3.3.0</version>
            <classifier>natives-windows</classifier>
        </dependency>
    </dependencies>

Main.java:

public class Main {
    public static void main(String[] args) {
        glfwInit(); // Initialize GLFW

        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // GLFW version 3
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // GLFW version 3.3
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // GLFW uses CORE profile

        long window = glfwCreateWindow(800, 600, "Hello Triangle", 0, 0);

        glfwMakeContextCurrent(window);
        GL.createCapabilities();

        while(!glfwWindowShouldClose(window)) {
            glfwPollEvents();
            glClearColor(0.05f, 0.1f, 0.2f, 1.0f); // Wipe drawing from previous frame with a black color
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color buffer and depth buffer

            glBegin(GL_TRIANGLES);
            glVertex2f(-0.5f, -0.5f);
            glVertex2f(0.5f, -0.5f);
            glVertex2f(0f, 0.5f);
            glEnd();

            glfwSwapBuffers(window); // Swap front and back buffer
            glFlush(); // Empties buffers - improves performance
        }

        glfwDestroyWindow(window);
        glfwTerminate();
    }
}

Solution

  • You are requesting OpenGL 3.3 core (the "3.3" is not the LWJGL or GLFW version 3.3 - it is the OpenGL context version you are requesting), which does not have the deprecated OpenGL functions available, that you are trying to use - glBegin and glVertex2f.

    The error message hints at that:

    No context is current or a function that is not available in the current context was called.

    There is also a misunderstanding. To clarify, the following comments in your code are misleading/wrong:

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // GLFW version 3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // GLFW version 3.3
    

    The window hints GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR do not specify which version of the GLFW native library to use, but which OpenGL context version GLFW will request from the OS/driver for the OpenGL context created by the next call to glfwCreateWindow().

    Either use modern OpenGL or request a lower OpenGL version, e.g. 2.1, by omitting the glfwWindowHint() calls. In that case, it is up to the driver which OpenGL context version it is giving you. On Windows (with NVidia) it will be the highest available OpenGL compatibility version (such as 4.6) supported. On macOS it will be 2.1.