Search code examples
c++opengltexturesframebuffer

Is it possible to use a depth renderbuffer with cubemap color attachment?


I am trying to create a Framebuffer, with a GL_TEXTURE_CUBE_MAP as the Color Attachment, and GL_DEPTH24_STENCIL8 as the Depth Attachment for the Framebuffer's Renderbuffer.

I was confident that this would work, as it did for a Framebuffer with a GL_TEXTURE_2D Color Attachment and a Renderbuffer with GL_DEPTH24_STENCIL8 as the Depth Component. However, it does not seem to work for a GL_TEXTURE_CUBE_MAP color attachment, because I get a Framebuffer Incomplete error. Also, note that I am trying to create multiple Framebuffers all at once.

glGenFramebuffers(5, &FBO[0]);
glGenTextures(5, &FBO_texture[0]);
glGenRenderbuffers(5, &FBO_Renderbuffer[0]);

for (unsigned int i = 0; i < 5; i++)
{
    glBindTexture(GL_TEXTURE_CUBE_MAP, FBO_texture[i]);
    for (unsigned int j = 0; j < 6; j++)
    {
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, 0, GL_R16F, shadow_map_width, shadow_map_height, 0, GL_RED, GL_FLOAT, NULL);
    }

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    glBindFramebuffer(GL_FRAMEBUFFER, FBO[i]);

    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, FBO_texture[i], 0);

    glBindRenderbuffer(GL_RENDERBUFFER, FBO_Renderbuffer[i]);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, shadow_map_width, shadow_map_height);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, FBO_Renderbuffer[i]);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        LOGGER->log(ERROR, "Renderer :  createFrameBuffers ", "Framebuffer is incomplete!");

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
}

I can't understand why I get a Framebuffer incomplete error. Am i missing something here?


Solution

  • No.

    You have to create a GL_TEXTURE_CUBE_MAPtexture with the format GL_DEPTH24_STENCIL8 for the depth buffer attachment. For instance:

    glGenFramebuffers(5, &FBO[0]);
    glGenTextures(5, &FBO_texture[0]);
    glGenTextures(5, &FBO_depth_texture[0]);
    
    for (unsigned int i = 0; i < 5; i++)
    {
        glBindTexture(GL_TEXTURE_CUBE_MAP, FBO_texture[i]);
        for (unsigned int j = 0; j < 6; j++)
        {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, 0, GL_R16F, shadow_map_width, shadow_map_height, 0, GL_RED, GL_FLOAT, NULL);
        }
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    
        glBindTexture(GL_TEXTURE_CUBE_MAP, FBO_depth_texture[i]);
        for (unsigned int j = 0; j < 6; j++)
        {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, 0, GL_DEPTH24_STENCIL8, shadow_map_width, shadow_map_height, 0, GL_DEPTH_STENCIL, GL_FLOAT, NULL);
        }
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    
        glBindFramebuffer(GL_FRAMEBUFFER, FBO[i]);
        glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, FBO_texture[i], 0);
        glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, FBO_depth_texture[i], 0);
    
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            LOGGER->log(ERROR, "Renderer :  createFrameBuffers ", "Framebuffer is incomplete!");
    
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }