OpenGL多采样多渲染目标FBO

kse8i1jr  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(161)

我正在尝试延迟着色,但我在尝试对多重渲染目标进行多重采样时遇到了问题。看起来我只复制了一个抗锯齿纹理,而不是3个(漫反射、位置、法线)。只复制了漫反射。
这是为我的几何通道创建的抗锯齿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到我的目标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,而没有多采样调用,再次似乎我只复制了一个纹理。我的问题的根源是错误地将一个MRT复制到另一个MRT。

wr98u20j

wr98u20j1#

我的主要问题是我的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的一般设置是正确的,所有的纹理都被成功地复制过来,并且在锯齿上有了重大的改进。

相关问题