I get error “Exception in thread ”main“ org.lwjgl.opengl.OpenGLException: Invalid operation (1282)”, what it is?, how I can fix it?

蓝咒 提交于 2019-12-13 09:16:20

问题


I use NetBeans Programming language:Java OS:Windows 8.1 x64

I try to make game engine with TheBennyBox tutorial. I'm in part 11 and i try to run my project but i get this error message:

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

shader.java

public void compileShader()
{
    glLinkProgram(program);

    if(glGetProgram(program, GL_LINK_STATUS) == 0)
    {

        System.err.println(glGetShaderInfoLog(program, 1024));
        System.exit(1);
    }

    glValidateProgram(program);

    if(glGetProgram(program, GL_VALIDATE_STATUS) == 0)
    {

        System.err.println(glGetShaderInfoLog(program, 1024));
        System.exit(1);
    }

}

private void addProgram(String text, int type)
{
    int shader = glCreateShader(type);

    if(shader == 0)
    {
        System.err.println("Shader creation failed: Could not find valid file location when adding shader");
        System.exit(1);
    }
    glShaderSource(shader, text);
    glCompileShader(shader);

    if(glGetShader(shader, GL_COMPILE_STATUS) == 0)
    {

        System.err.println(glGetShaderInfoLog(shader, 1024));
        System.exit(1);
    }

    glAttachShader(program, shader);
}

public void setUniformi(String uniformName, int value)
{
    glUniform1i(uniforms.get(uniformName), value);
}

public void setUniformf(String uniformName, float value)
{
    glUniform1f(uniforms.get(uniformName), value);
}

public void setUniform(String uniformName, Vector3f value)
{
    glUniform3f(uniforms.get(uniformName), value.getX(), value.getY(), value.getZ());
}

public void setUniform(String uniformName, Matrix4f value)
{
    glUniformMatrix4(uniforms.get(uniformName), true, Util.createFlippedBuffer(value));
}


  }

game.java

public void update()
{
    temp += Time.getDelta();

    shader.setUniformf("uniformFloat", (float)Math.abs(Math.sin(temp)));
}

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

}

I don't know what error it, and how I can fix it? If you need codes to help me fix this ask in comments.


回答1:


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.



来源:https://stackoverflow.com/questions/28993104/i-get-error-exception-in-thread-main-org-lwjgl-opengl-openglexception-invali

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!