Search code examples
javaopengllwjgl

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282) - LWJGL


I tried to use lwjgl to create a game. I did something wrong and got error below. How I can fix it? What it actually means?

Exception in thread "main" org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
at org.lwjgl.opengl.GL20.glUniform1f(GL20.java:338)
at com.base.engine.Shader.setUniformf(Shader.java:119)
at com.base.engine.Game.update(Game.java:63)
at com.base.engine.MainComponent.run(MainComponent.java:85)
at com.base.engine.MainComponent.start(MainComponent.java:38)
at com.base.engine.MainComponent.main(MainComponent.java:131)
Java Result: 1

Solution

  • From the OpenGL 3.3 Reference Pages:

    GL_INVALID_OPERATION is generated if there is no current program object.

    You bind your shader program in the render() method of Game:

    public void render()
    {
        shader.bind();
        mesh.draw();
    }
    

    However, as seen in the MainComponent class from Tutorial 11:

    private void run()
    {
        // ...
            while(unprocessedTime > frameTime)
            {
                // ...
                game.update();
                // ...
            }
            if(render)
            {
                render();
                frames++;
            }
            // ...
    }
    
    private void render()
    {
        RenderUtil.clearScreen();
        game.render();
        Window.render();
    }
    

    Here game.update() gets called before render() (and therefore game.render()).

    Because the shader program is only bound in game.render(), at the first call of game.update() there is no program bound, which means GL_INVALID_OPERATION is thrown.

    This isn't really an issue because from the second frame on the program is bound and so all will be working perfectly from there. However, you probably have debug mode switched on, which means LWJGL won't silently ignore OpenGL errors, instead throwing exceptions.

    So you can either switch debug mode off or, what I would recommend, bind your shader program once at the end of the Game constructor instead of every frame. As long as you only have one shader program, it will work perfectly.