-2

I'm trying to visualize the depth texture attached to a custom fbo in opengl. I expected to see a "foggy" looking, greyscale output - the problem is that the output seems like it's the same as the color texture - I mean it's the exact same color texture. Am I actually rendering the same thing into 2 different textures?

Creating the depth texture:

    glGenTextures(1, &m_DepthTxId);
    glBindTexture(GL_TEXTURE_2D, m_DepthTxId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, (void*)nullptr);
    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);

Creating the fbo & attaching the color/depth textures:

    glGenFramebuffers(1, &m_Id);
    glBindFramebuffer(GL_FRAMEBUFFER, m_Id);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ColorTxId, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_TEXTURE_2D, m_DepthTxId, 0);
    m_DrawBuffers[0] = GL_COLOR_ATTACHMENT0;
    glDrawBuffers(1, m_DrawBuffers);

The interesting thing is that without adding the depth texture, the objects are displayed in the order they were rendered (scene looks messed up) - but when I add the depth texture, everything looks fine as you would expect with depth testing.

But how come when I give the depth texture to the shader it just displays the color-texture? Am I rendering color data into my depth texture, or..?

Apologies, but I'm fairly new to working with fbos :P

Additional info:

Rendering the fbo-textures onto a quad:

    glActiveTexture(GL_TEXTURE0); // Color
    glBindTexture(GL_TEXTURE_2D, m_Fbo->getColorTxId()); // ColorTexture ID
    glUniform1i(m_Shader->getColorSampler(), 0); // ColorSampler Location

    glActiveTexture(GL_TEXTURE1); // Depth
    glBindTexture(GL_TEXTURE_2D, m_Fbo->getDepthTxId()); // DepthTexture ID
    glUniform1i(m_Shader->getDepthSampler(), 0); // DepthSampler Location

    glBindVertexArray(m_Fbo->getVaoId());
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)0);
    glBindVertexArray(0);

Getting the sampler locations from the shader:

    m_Location_ColorTxSampler = glGetUniformLocation(m_ProgramId, "colorSampler");
    m_Location_DepthTxSampler = glGetUniformLocation(m_ProgramId, "depthSampler");

Shader:

    in vec2 uv;

    uniform sampler2D colorSampler;
    uniform sampler2D depthSampler;

    out vec4 color;

    void main()
    {
        color = vec4(texture2D(depthSampler, uv).rgb, 1.0);
    }

To me the whole thing seems correct, unless the 2 samplers are at the same location.. I'm sure that's not possible

  • 2
    Are you checking for gl errors? The first thing I see is that `GL_DEPTH_ATTACHMENT` is not one of the allowed values for `glDrawBuffers` ([ref](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawBuffers.xhtml)) and should cause a `GL_INVALID_ENUM`. But if you have colors in your depth texture, it is highly likely that you are rendering the wrong texture. Since you use `GL_DEPTH_COMPONENT` this is a one-channel texture and cannot store any color information. Showing the code where you render the texture would actually be a good idea. – BDL Apr 05 '17 at 15:39
  • 1
    As BDL already pointed out, `glDrawBuffers()` is not related to the depth buffer. Your question is missing a lot of context. If you want to "see" the depth buffer. Then are you sure you're binding the depth buffer as a texture and using it in your shader? – vallentin Apr 05 '17 at 15:42
  • Hey guys, I've updated the post above with additional info, and I've got rid of the GL_DEPTH_ATTACHMENT in the drawBuffers, I honestly don't see where the problem is or how this code produces a color-texture-output... please have a look, thank you in advance! – HelloWorldCpp Apr 05 '17 at 16:24
  • AS Soon As I posted this - found it... glUniform1i(m_Shader->getDepthSampler(), 0); - Last parameter should have been 1... I can not believe it... this happens everytime.. I post a question then I find the issue... literally minutes after... every single time. It's actually getting embarrassing.. :( Thanks guys and sorry for the inconvenience, – HelloWorldCpp Apr 05 '17 at 16:29

1 Answers1

0

If you bind the depth texture to unit 1...

glActiveTexture(GL_TEXTURE1); // Depth
glBindTexture(GL_TEXTURE_2D, m_Fbo->getDepthTxId()); // DepthTexture ID

... you should not tell the shader to sample from unit 0:

glUniform1i(m_Shader->getDepthSampler(), 0); // DepthSampler Location
derhass
  • 43,833
  • 2
  • 57
  • 78
  • Yeah I know as soon as I posted the snippet and read through it I noticed it :P It's weird that I didn't see it in VS but I instantly noticed here on S.O... it's happened to me before too, posting a problem and pretty much solving it straight after... embarrassing – HelloWorldCpp Apr 05 '17 at 18:55