OpenGL Mutlisampling多个渲染目标FBO

时间:2015-08-25 20:23:21

标签: opengl multisampling

我正在尝试延迟着色,我在尝试MultiSample多个渲染目标时遇到了麻烦。它看起来好像我只是复制一个抗锯齿纹理而不是3(漫反射,位置,法线)。仅复制漫反射。

这是我的Geometry Pass的Anti Alias FBO创建:

IntBuffer drawBuffs = BufferUtils.createIntBuffer(3);
private void CreateFBOAntiAlias() {

    AntiAliasFrameBuffer = glGenFramebuffers();
    GL30.glBindFramebuffer(GL_FRAMEBUFFER, AntiAliasFrameBuffer);

    textureColorMultiSampled = generateMultiSampleTexture(4);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
    GL32.GL_TEXTURE_2D_MULTISAMPLE, textureColorMultiSampled,
    0);

    texturePositionMultiSampled = generateMultiSampleTexture(4);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
    GL32.GL_TEXTURE_2D_MULTISAMPLE, texturePositionMultiSampled ,
    0);

    textureNormalMultiSampled = generateMultiSampleTexture(4);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2,
    GL32.GL_TEXTURE_2D_MULTISAMPLE, textureNormalMultiSampled ,
    0);


    drawBuffs.put(0, GL30.GL_COLOR_ATTACHMENT0);
    drawBuffs.put(1, GL30.GL_COLOR_ATTACHMENT1);
    drawBuffs.put(2, GL30.GL_COLOR_ATTACHMENT2);


    GL20.glDrawBuffers(drawBuffs);

    AntiAliasRenderBufferObject = glGenRenderbuffers();

    glBindRenderbuffer(GL_RENDERBUFFER, AntiAliasRenderBufferObject);
    glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4,
            GL_DEPTH24_STENCIL8, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
            GL_RENDERBUFFER, AntiAliasRenderBufferObject);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        System.err
                .println("ERROR::FRAMEBUFFER:: Framebuffer is not complete!");
    } else {
        System.err.println("FrameBuffer AntiAliasFBO success");
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

 private int generateMultiSampleTexture(int samples) {
    int texture;
    texture = GL11.glGenTextures();

    GL11.glBindTexture(GL32.GL_TEXTURE_2D_MULTISAMPLE, texture);
    GL32.glTexImage2DMultisample(GL32.GL_TEXTURE_2D_MULTISAMPLE, samples,
            GL11.GL_RGBA8, width, height, true);
    GL11.glBindTexture(GL32.GL_TEXTURE_2D_MULTISAMPLE, 0);

    return texture;

}

这是我的目标FBO的创作:

public void CreateFBO() {

    fbo_handle = glGenFramebuffers();
    diffuse_handle = GL11.glGenTextures();
    position_handle = GL11.glGenTextures();
    normal_handle = GL11.glGenTextures();
    glBindFramebuffer(GL_FRAMEBUFFER, fbo_handle);

    //////////////////DIFFUSE////////////////////////////////////
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, diffuse_handle);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height,0, 
            GL11.GL_RGBA, GL11.GL_INT, (ByteBuffer) null);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
            GL11.GL_TEXTURE_2D, diffuse_handle, 0);

    /////////////////POSITION////////////////////////////////////
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, position_handle);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0,
            GL11.GL_RGBA, GL11.GL_INT, (ByteBuffer) null);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
            GL11.GL_TEXTURE_2D, position_handle, 0);

    ///////////////NORMALS/////////////////////////////////////////
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, normal_handle);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0,
            GL11.GL_RGBA, GL11.GL_INT, (ByteBuffer) null);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2,
            GL11.GL_TEXTURE_2D, normal_handle, 0);
    //////////////////////////////////////////////////////////////////


    drawBuffs.put(0, GL30.GL_COLOR_ATTACHMENT0);
    drawBuffs.put(1, GL30.GL_COLOR_ATTACHMENT1);
    drawBuffs.put(2, GL30.GL_COLOR_ATTACHMENT2);

    GL20.glDrawBuffers(drawBuffs);

    rbo_depth_buffer_handle = glGenRenderbuffers();
    glBindRenderbuffer(GL_RENDERBUFFER, rbo_depth_buffer_handle);
    glRenderbufferStorage(GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT32,
            width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
            GL_RENDERBUFFER, rbo_depth_buffer_handle);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        System.err.println("Framebuffer configuration error");
    } else {

        System.err.println("Deferred configuration Success");
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

我在几何传球过程中绑定我的AA FBO,解除绑定并从AA FBO执行Blit到我的目标FBO并渲染全屏四边形并产生不良结果。

public void blit() {
    GL11.glEnable(GL13.GL_MULTISAMPLE);

    glBindFramebuffer(GL_READ_FRAMEBUFFER, AntiAliasFrameBuffer);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_handle);
    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
            GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

编辑:我已经尝试从正常的MRT fbo到另一个普通的MRT FBO进行blitting而没有Multi-Sampling调用,而且似乎我只复制了一个纹理。我问题的根源是错误地将MRT打到另一个MRT。

1 个答案:

答案 0 :(得分:1)

哇,好吧我的主要问题是我的blitting是不正确的。 blitting方法应该是

public void blit() {

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glBindFramebuffer(GL_READ_FRAMEBUFFER, AntiAliasFrameBuffer);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_handle);



    GL11.glReadBuffer(GL_COLOR_ATTACHMENT0);
    GL11.glDrawBuffer(GL_COLOR_ATTACHMENT0);

    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
            GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST);


    GL11.glReadBuffer(GL_COLOR_ATTACHMENT1);
    GL11.glDrawBuffer(GL_COLOR_ATTACHMENT1);
    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
            GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST);



    GL11.glReadBuffer(GL_COLOR_ATTACHMENT2);
    GL11.glDrawBuffer(GL_COLOR_ATTACHMENT2);

    glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
            GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST);


    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

我的AA FBO / FBO的一般设置是正确的,并且所有纹理都被成功复制并且与Aliasing相比有了重大改进。