Search code examples
opengltexturesglsldepth-bufferrender-to-texture

How is glDrawBuffers associated to drawing to a depth texture


You can specify what buffers to draw to using
glDrawBuffer()

example
GLenum buffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers( 1, buffers );

ok so that makes sense.

how about the depth texture though?

so we have here our creation of a frame buffer

    glGenFramebuffers(1, &m_uifboHandle);

    // Initialize FBO
    glBindFramebuffer(GL_FRAMEBUFFER, m_uifboHandle);

    unsigned int m_uiTextureHandle[2];
    glGenTextures( 2, m_uiTextureHandle );

    glBindTexture( GL_TEXTURE_2D, m_uiTextureHandle[x]);

    // Reserve space for our 2D render target
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    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, iInternalFormat, uiWidth, uiHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_uiTextureHandle[0], 0); 

    //now the depth     
    glBindTexture(GL_TEXTURE_2D, m_uiTextureHandle[1]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, uiWidth, uiHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_uiTextureHandle[1], 0);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

so if these steps basically:
1) make an frame to manipulate to render to
2) gives us a location for attachment 0 to write to
3) creates our depth attachment

now how can i write to it? i seen examples where rendering automatically renders to it but i want to understand how binding to write to it works.


Solution

  • glDrawBuffers is not related to the depth buffer at all. It allows selecting the color buffers to render into, and there can be more than one color buffer per framebuffer. But there can be at most one depth buffer. So either there is no depth buffer at all, or there is one and the depth value is written to the depth buffer (assuimg depth test is enabled and the depth mask is set to GL_TRUE).