Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Depth Buffer to texture issues

  1. #1
    Newbie Newbie
    Join Date
    Aug 2013
    Posts
    3

    Depth Buffer to texture issues

    Hello world,

    I attached a depth buffer to my FBO and want to render the corresponding depth texture for debugging and need it for advanced rendering techniques like ssao.
    Therefore I am doing the following

    FBO init

    Code :
    glGenFramebuffers(1, &framebufferHandle);
    glBindFramebuffer(GL_FRAMEBUFFER, framebufferHandle);
     
    // pos, normal, color
    // ...
     
     
    // depth
    glGenTextures(1, &depthTextureHandle);
    glBindTexture(GL_TEXTURE_2D, depthTextureHandle);
     
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, windowWidth, windowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
     
    // ...
     
    //set attachments
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionTextureHandle, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normalTextureHandle, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorTextureHandle, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_TEXTURE_2D, depthTextureHandle, 0);
     
    /set the list of draw buffers.
    GLenum drawBufferHandles[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
    glDrawBuffers(3, drawBufferHandles);

    Rendering the geometry:

    Code :
    glBindFramebuffer(GL_FRAMEBUFFER, framebufferHandle);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
     
    gBufferShader->bind();
    render();
     
    // Compositing
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
     
    finalCompositingShader->bind();
     
    drawScreenFillingTri();

    Inside drawScreenFillingTri()

    Code :
    glDisable(GL_DEPTH_TEST);
     
    // array init stuff ...
     
    // Activate textures
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, positionTextureHandle);
    glUniform1i(positionMapHandle, 0);
     
    // ... color and normal ...
     
    glActiveTexture(GL_TEXTURE3);
    glBindTexture(GL_TEXTURE_2D, depthTextureHandle);
    glUniform1i(depthTextureHandle, 3);
     
    glDrawArrays(GL_TRIANGLES, 0, 3);
     
    // ...
     
    glEnable(GL_DEPTH_TEST);

    Inside my GBuffer fragment shader

    Code :
    layout(location = 0) out vec4 positionOutput;
    layout(location = 1) out vec3 normalOutput;
    layout(location = 2) out vec4 colorOutput;
     
    void main(){
        positionOutput = pass_Position;
        normalOutput = normalize(pass_Normal);
     
        colorOutput = texture(tex,pass_TexCoord);
    }



    and during my compositing

    Code :
    in vec2 passUV;
     
    uniform sampler2D positionMap;
    uniform sampler2D normalMap;
    uniform sampler2D colorMap;
    uniform sampler2D depthMap;
     
    out vec4 fragmentColor;
     
    void main() {
        vec4 position = texture(positionMap, passUV);
        vec4 normal = texture(normalMap, passUV);
        vec4 color = texture(colorMap, passUV);
     
        float depth = texture(depthMap, passUV).x;
     
     
     
        fragmentColor = vec4(depth,depth,depth,1);
    }

    I cannot find my mistake here. The rendered image looks like this:
    https://dl.dropboxusercontent.com/u/...4-42-09-29.jpg

    I would be thankful if some nice person could help me out with this issue.

    Greetings from Germany
    Sc4v0r

  2. #2
    Newbie Newbie
    Join Date
    Aug 2013
    Posts
    3
    Mh ok it seems to be working if I change the depth texture to texture unit GL_TEXTURE0. Therefore the positionMap isn't working anymore.
    This is some strange behavior as it suspects my graphics card only supports 3 texture units but I am using a GTX 580.
    Any suggestions whats wrong with my code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •