c++openglshadowframebufferdepth-buffer

Framebuffer not completing on GL_DEPTH_ATTACHMENT render to texture


I'm attempting shadow mapping in OpenGL, and have been unable to render specifically GL_DEPTH_ATTACHMENT to a frame buffer. glCheckFramebufferStatus does not return complete but only when targeting the depth attachment - it works fine for color attachment0.

    glBindFramebuffer(GL_FRAMEBUFFER, ShadowMapFrameBuffer);
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
    glViewport(0, 0, LightMapResolution, LightMapResolution);
    glClear(GL_DEPTH_BUFFER_BIT);

// Shader works fine with colour, too.
    std::shared_ptr<Shader> mapShader = Shaders["shadowMap"];

    glGenTextures(1, &DEBUGSHADOW);
    glBindTexture(GL_TEXTURE_2D, DEBUGSHADOW);
// I've tried GL_UNSIGNED_INT in the type and GL_DEPTH_COMPONENT16 as the internal format as per my search results to no avail.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DEBUGSHADOW, 0);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    {
        clog::Error(CLOGINFO, "Framebuffer broke!", false);
    }


// Drawing code omitted. Irrelevant to the issue and functions fine.

    glViewport(0, 0, windowWidth, windowHeight);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    return;

For completeness, the shaders are taken from LearnOpenGL and are as follows:

#version 460 core
layout (location = 0) in vec3 aPos;

uniform mat4 lightMatrix;
uniform mat4 modelMatrix;

void main()
{
    gl_Position = lightMatrix * modelMatrix * vec4(aPos, 1.0);
}
#version 460 core

void main()
{
}

The program outputs the error message within the conditional "Framebuffer broke!" any time the buffer is configured for GL_DEPTH_ATTACHMENT and I'm unsure why. As far as I can tell my code does no deviate from the aforementioned LearnOpenGL article (https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping).


Solution

  • It turned out to be a twofold issue. At some point I'd made a typo in glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, LightMapResolution, LightMapResolution, 0, GL_FLOAT, GL_FLOAT, NULL); The first instance of GL_FLOAT should've been GL_DEPTH_COMPONENT. This caused the incomplete framebuffer.

    The second error was found through glGetError(). I was calling glClear() before the framebuffer had an attachment (presumably?) and it was reporting GL_INVALID_FRAMEBUFFER_OPERATION (1286 in decimal for reference).

    After fixing the above issues the framebuffer behaves as expected.