LWJGL opengl no context in function, after defining context error

67 views Asked by At

After encountering errors due to running version opengl 2.1, i managed to get it to load version 4.1; im running on an m2 macbook.

here is the output:

Hello LWJGL 3.3.3+5!
gl version: 4.1 Metal - 83.1
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.glEnd(Native Method)
    at com.Main.Render.DisplayHandler.RenderUpdate(DisplayHandler.java:90)
public class DisplayHandler {
    public static long window;

    public static void RenderMain(){
        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 our window
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable



        int WIDTH = 1920;
        int HEIGHT = 1000;


        // Create the window
        window = glfwCreateWindow(WIDTH, HEIGHT, "Raytrace", NULL, NULL);
        // Attach the input handler

        glfwSetKeyCallback(window, new InputHandler(window));

        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");


        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
        glfwShowWindow(window);


    }
    public static boolean isCloseRequested(){
        return glfwWindowShouldClose(window);
    }

    public static void RenderUpdate(){
        // clear the framebuffer
        GL.createCapabilities();
        //set up display system


        // 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
            glfwPollEvents();
            System.out.println("gl version: " + glGetString(GL_VERSION));
            GL11.glEnable(GL_POINTS);
            GL11.glEnd();


            Main.objectManager.LoadObjects();
            Main.workspace.RenderWorkspace(1920, 1000);



            // Poll for window events. The key callback above will only be
            // invoked during this call.


        }
    }
}

Here is the class that calls the method which is called by the main class

public class Renderloop {
    public Renderloop(){
        try {
            DisplayHandler.RenderMain();
        } catch (Exception e) {
            System.out.println("Could not initialize RenderLoop, JVM gave error: " + e.getMessage());
        }
        while (!DisplayHandler.isCloseRequested()) {
            DisplayHandler.RenderUpdate();


        }
    }
}

Ive tried using GL30 instead of GL11 and it still doesn't work.

1

There are 1 answers

0
Nifil On

You're calling GL11.glEnd(); without ever calling glBegin() In the legacy OpenGL specifications, glEnd() ends a block that begins with glBegin(). Typically it would be something like

glBegin(GL_TRIANGLES);
glVertex3f(...);
glVertex3f(...);
glVertex3f(...);
glEnd();

Also note that glfwSwapBuffers(window); glfwPollEvents(); should be called after rendering (so at the end of the loop).