javaopengloff-screendepth-testing

Why depth test does not work in offscreen rendering for color pick?


I'm using coloring and off-screen rendering to implement a selection tool for QUADS on OpenGL. But the depth test seems to not work in the off-screen rendering and at certain angles and rotations, the QUAD behind the front one is selected. Any thoughts on how I should set up my Buffers and/or attachments? Does this problem have anything to do with my normals??? Or is just depth test?? I personally think it's depth test, I just can't get the damn thing working!!!

Here is My init function for buffers:

public void init(final GL2 gl) {
    // setup
    gl.glGenFramebuffers(frameBufferIds.capacity(), frameBufferIds);
    gl.glGenRenderbuffers(renderBufferIds.capacity(), renderBufferIds);
    gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, renderBufferIds.get(0));
    // 
    ViewPort viewPort = ViewPort.getCurrent(gl);
    gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_RGBA, viewPort.getWidth(), viewPort.getHeight());

    /**
     * Comment 1
     */
    //  gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT, viewPort.getWidth(), viewPort.getHeight());
    
    // attach the render buffer to color attachment point
    gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferIds.get(0));
    gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0, GL2.GL_RENDERBUFFER, renderBufferIds.get(0));
    /**
     * Comment 2
     */
    // gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT, GL2.GL_RENDERBUFFER, renderBufferIds.get(0));
    //
    if (gl.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER) != GL2.GL_FRAMEBUFFER_COMPLETE) {
        System.out.println("Render Buffer problem!");
    }
}

When I uncomment the "Comment 1" and "Comment 2" parts, I get a render buffer problem. But I've heard that I should attach "a render buffer to the GL_DEPTH_ATTACHMENT in addition to the color attachment." (Source: Look at this Q. I wonder How can I do that.

Also, here is my usage code:

gl.glDisable(GL2.GL_DITHER);

// 1. render in offscreen
init(gl);
gl.glEnable(GL2.GL_DEPTH_TEST);
offscreenRender(gl, selectionType);

// 2. read pixel
gl.glReadBuffer(GL2.GL_DEPTH_ATTACHMENT);
gl.glReadBuffer(GL2.GL_COLOR_ATTACHMENT0);

gl.glReadPixels((int) (pos.x - pickWidth / 2), (int) (viewPort.getHeight() - pos.y - pickWidth / 2), pickWidth, pickWidth,  GL2.GL_RGBA, GL2.GL_FLOAT, pointSelectionPixels);

Solution

  • I read couple of hours about frame and render buffers all over the internet. Finally I find my problem on this site. This site says we must render the color into the Texture and the depth into the RenderBuffer. That's the point!

    And after setup the buffers and texture, everything is prepared for reading depth and color value with glReadPixels method.

    // reading color
    glReadBuffer( GL_COLOR_ATTACHMENT0_EXT );
    glReadPixels( 0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, color );
    
    // reading depth
    glReadBuffer( GL_DEPTH_ATTACHMENT_EXT );
    glReadPixels( 0, 0, WIDTH, HEIGHT, GL_DEPTH_COMPONENT, GL_FLOAT, depth );
    

    Thank you Wano Choi! :D