本文介绍了我在线程“主"中收到错误“异常". org.lwjgl.opengl.OpenGLException:无效的操作(1282)",这是什么?我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NetBeans编程语言:Java操作系统:Windows 8.1 x64

I use NetBeansProgramming language:JavaOS:Windows 8.1 x64

我尝试使用TheBennyBox制作游戏引擎教程.我在第11部分中,我尝试运行我的项目,但收到以下错误消息:

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

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

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.

推荐答案

来自 OpenGL 3.3参考页面:

您可以在Gamerender()方法中绑定着色​​器程序:

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

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

但是,如 类:

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

private void render()
{
    RenderUtil.clearScreen();
    game.render();
    Window.render();
}

此处game.update()render()(因此是game.render())之前被调用.

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

因为着色器程序仅绑定在game.render()中,所以在第一次调用game.update()时没有程序绑定,这意味着将抛出GL_INVALID_OPERATION.

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.

这并不是真正的问题,因为从程序的第二帧开始就绑定了,因此所有程序都可以从那里完美地工作.但是,您可能已打开调试模式,这意味着LWJGL不会默默地忽略OpenGL错误,而是抛出异常.

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.

因此,您可以关闭调试模式,或者,我建议在Game构造函数的末尾而不是每一帧都绑定一次着色器程序.只要只有一个着色器程序,它就可以完美运行.

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.

这篇关于我在线程“主"中收到错误“异常". org.lwjgl.opengl.OpenGLException:无效的操作(1282)",这是什么?我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:22