-1

I am attempting to render a scene's depth to a cubemap texture, but upon checking the framebuffer status it returns GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT and of course no rendering occurs. Obviously I am missing something but I cannot seem to figure that out, I was hoping somebody here could help me with that. My video card is a NVIDIA GeForce 840M that supports OpenGL 4.5. Below is how I set up the rendering to the fbo.

UPDATE: Attach all cubefacetextures before rendering loop

GLuint texhandle;
glGenTextures(1, &texhandle);
glBindTexture(GL_TEXTURE_CUBE_MAP, texhandle);
for (int i = 0; i < 6; i++) {
    glTexImage2D(
        GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
        GL_DEPTH_COMPONENT24, 1600, 900,
        0, GL_DEPTH_COMPONENT, GL_FLOAT, 0
    );
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
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);

GLuint fbohandle;
glGenFramebuffers(num, &fbohandle);
glBindFramebuffer(GL_FRAMEBUFFER, fbohandle);
glCullFace(GL_BACK);

for (int i = 0; i < 6; i++) {   
    glFramebufferTexture2D(
        GL_FRAMEBUFFER, 
        GL_DEPTH_ATTACHMENT, 
        GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,    
        fbohandle, 
        0
    );
}

for (int i = 0; i < 6; i++) {
    GLenum cubemapSide = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, cubemapSide, fbohandle, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawBuffer(GL_NONE);
    CheckFramebufferStatus();
    RenderScene()
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Daniel N
  • 53
  • 1
  • 11
  • Shouldn't you be doing everything except `glFramebufferTexture2D` outside of the loop? When you just have attached the first face, it is not surprising that the framebuffer is incomplete. – BDL Dec 13 '16 at 09:41
  • Right, makes sense, but don't I use glFramebufferTexture2D to specify which face I'm rendering to? Anyways, I added loop to attach faces before the rendering loop, still get the same error though. – Daniel N Dec 13 '16 at 11:18
  • glDrawBuffer(GL_NONE); makes no sense... – starmole Dec 13 '16 at 11:35
  • 2
    Also cube map side have to be square – starmole Dec 13 '16 at 11:37
  • It does, since frag shader only outputs a single float which is depth. Why write to color buffers when color is not desired? This has worked fine before when rendering to Texture2D, and it works now too ,) You're right that thee sides have to be square though, that fixed it. Thanks! – Daniel N Dec 13 '16 at 12:55

1 Answers1

0

Let's handle the easy mistake first:

glFramebufferTexture2D(..., fbohandle, ...);

This is wrong, you should of course use texhandle to attach the specific texture object (to the currently bound GL_FRAMEBUFFER, which will be fbohandle at that time).

However, you seem to have misconceptions about how layered framebuffers work. There is only one GL_DEPTH_ATTACHMENT per FBO, hence you can only attach one thing texture object (or render buffer) to it.

What your code conceptually would do (after fixing the mistake above) is binding each face of the cube map as the single, unlayered, depth attachment, so after the loop, the last cube face is attached, and the loop itself is completely pointless.

To use layered rendering, you need to

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texobj, 0);

which will in the case of a cubemap create a depth attachments with 6 layers refering to the sides of the cube map, and more specifically to the mimap level 0 of all of these.

derhass
  • 43,833
  • 2
  • 57
  • 78