为什么glDrawBuffer会生成GL_INVALID_OPERATION?

时间:2014-12-25 21:22:18

标签: java opengl jogl opengl-3 msaa

我有一个使用OpenGL 3到JOGL的Java应用程序,我正在尝试实现多次采样。我正在关注this教程,到目前为止,它似乎有效,除了一行。当我致电gl.glBackBuffer(GL3.GL_BACK)时,会产生GL_INVALID_OPERATION错误。如果程序继续,它似乎只绘制一个缓冲区,无论它当前是前缓冲区还是后缓冲区,并且只要重绘它就在所需的输出和垃圾数据之间切换。

根据文件:

  

如果默认帧缓冲区受影响并且buf指示的缓冲区都不存在,则会生成GL_INVALID_OPERATION。

     

如果帧缓冲对象受到影响并且buf不等于GL_NONE或GL_COLOR_ATTACHMENT $ m $,则生成GL_INVALID_OPERATION,其中$ m $是介于0和GL_MAX_COLOR_ATTACHMENTS之间的值。

我不认为其中任何一个都是我的问题,但其中一个必须是,因为它们是生成错误的唯一方案。

以下是相关代码:

@Override
public void init(GLAutoDrawable drawable) {

    GL3 gl = drawable.getGL().getGL3();
    int[] pointer = new int[1];

    sl = new ShaderLoader();
    sl.loadShader(gl);

    gl.glEnable(GL3.GL_DOUBLEBUFFER);
    gl.glEnable(GL3.GL_MULTISAMPLE);

    gl.glClearColor(1, 1, 1, 1);

    gl.glGenTextures(1, pointer, 0);
    multisampleTex = pointer[0];

    gl.glGenBuffers(1, pointer, 0);
    multisampleFbo = pointer[0];

    gl.glGenRenderbuffers(1, pointer, 0);
    multisampleRbo = pointer[0];

    main.init(drawable);

    gl.glGetIntegerv(GL3.GL_MAX_SAMPLES, pointer, 0);
    System.out.println(pointer[0]);

}

@Override
public void dispose(GLAutoDrawable drawable) {

    GL3 gl = drawable.getGL().getGL3();

    sl.destroy(gl);

}

@Override
public void display(GLAutoDrawable drawable) {

    // GL3 gl = new DebugGL3(drawable.getGL().getGL3());
    GL3 gl = drawable.getGL().getGL3();

    // drawable.setGL(gl);

    gl.glClear(GL3.GL_COLOR_BUFFER_BIT);

    gl.glBindFramebuffer(GL3.GL_FRAMEBUFFER, multisampleFbo);

    gl.glClear(GL3.GL_COLOR_BUFFER_BIT);

    main.render(drawable);

    gl.glBindFramebuffer(GL3.GL_DRAW_FRAMEBUFFER, GL3.GL_NONE);
    gl.glBindFramebuffer(GL3.GL_READ_FRAMEBUFFER, multisampleFbo);
    checkGLError();
    gl.glDrawBuffer(GL3.GL_BACK); // Throws GL_INVALID_OPERATION
    checkGLError("OpenGL error at glDrawBuffer: 0x%h%n");
    gl.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
            GL3.GL_COLOR_BUFFER_BIT, GL3.GL_NEAREST);

    checkGLError();

}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
        int height) {

    GL3 gl = drawable.getGL().getGL3();

    this.width = width;
    this.height = height;

    updateMultisampleObjects(gl);

    gl.glViewport(0, 0, width, height);

    updatePMatrix();

}

private void updateMultisampleObjects(GL3 gl) {

    gl.glBindTexture(GL3.GL_TEXTURE_2D_MULTISAMPLE, multisampleTex);
    gl.glTexImage2DMultisample(GL3.GL_TEXTURE_2D_MULTISAMPLE,
            NUM_OF_SAMPLES, GL3.GL_RGBA8, width, height, true);

    gl.glBindFramebuffer(GL3.GL_FRAMEBUFFER, multisampleFbo);
    gl.glFramebufferTexture2D(GL3.GL_FRAMEBUFFER, GL3.GL_COLOR_ATTACHMENT0,
            GL3.GL_TEXTURE_2D_MULTISAMPLE, multisampleTex, 0);

    gl.glBindRenderbuffer(GL3.GL_RENDERBUFFER, multisampleRbo);
    gl.glRenderbufferStorageMultisample(GL3.GL_RENDERBUFFER,
            NUM_OF_SAMPLES, GL3.GL_DEPTH24_STENCIL8, width, height);
    gl.glFramebufferRenderbuffer(GL3.GL_FRAMEBUFFER,
            GL3.GL_DEPTH_STENCIL_ATTACHMENT, GL3.GL_RENDERBUFFER,
            multisampleRbo);

}

另外,我知道我没有做过多次清理,但这不应该影响这个问题。

编辑:根据要求,这里是创建GLCanvas的代码:

    glp = GLProfile.get("GL3");
    caps = new GLCapabilities(glp);
    caps.setDoubleBuffered(true);
    canvas = new GLCanvas(caps);
    canvas.setPreferredSize(new Dimension(800, 600));
    canvas.addGLEventListener(glListener);

    frame.add(canvas);

1 个答案:

答案 0 :(得分:0)

我怀疑错误来自之前的通话。至少应该有很多错误,例如来自glBindFramebuffer()电话之前的glDrawBuffer()电话:

gl.glBindFramebuffer(GL3.GL_READ_FRAMEBUFFER, multisampleFbo);
checkGLError();
gl.glDrawBuffer(GL3.GL_BACK); // Throws GL_INVALID_OPERATION
checkGLError("OpenGL error at glDrawBuffer: 0x%h%n");

multisampleFbo不是帧缓冲对象。它是这样创建的:

gl.glGenBuffers(1, pointer, 0);
multisampleFbo = pointer[0];

glGenBuffers()创建缓冲区对象,而非 framebuffer 对象。要创建FBO,您需要致电glGenFramebuffers()

gl.glGenFramebuffers(1, pointer, 0);
multisampleFbo = pointer[0];