Advertisement

Framebuffer is black while trying to write depth value to it

Started by November 20, 2016 12:17 PM
6 comments, last by cgrant 8 years ago

Framebuffer and related texture initialization:


glGenTextures(1, &depth_map_tex);
glBindTexture(GL_TEXTURE_2D, depth_map_tex);

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);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, WIDTH, HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);

glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &depth_map_fbo);

glBindFramebuffer(GL_FRAMEBUFFER, depth_map_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_map_tex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
     
glBindFramebuffer(GL_FRAMEBUFFER, 0);

Drawing some scene to it:


glViewport(0, 0, WIDTH, HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depth_map_fbo);

glClearDepth(1.0);
glClear(GL_DEPTH_BUFFER_BIT);

glUseProgram(program);

{
    mat4 model(1.0f);

    glUniformMatrix4fv(ul_model, 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(ul_view, 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(ul_proj, 1, GL_FALSE, glm::value_ptr(proj));

    glBindVertexArray(cube_vao); 
    glDrawArrays(GL_TRIANGLES, 0, CUBE_VERTICES_NUM);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

Vertex shader:


layout (location = 0) in vec3 position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    gl_Position = proj * view * model * vec4(position, 1.0f);
}  

Fragment shader:


void main(void)
{
   gl_FragColor = vec4(0, 0, 0, 1.0f);
}

The result I get via AMD codeXL is totally black texture attached to framebuffer.

So, how could I fix this?

Sorry for my English.

I would start by checking return values of the various function calls.

Advertisement

The fragment shader returns black "vec4(0, 0, 0, 1.0f)" so it might be hard to see the result.

Try displaying a green color and then sample a texture or show the vertex colors.

Clearing the target color to blue while clearing the depth buffer usually makes it easier to debug when everything you see is black.

Hi, I think that you forget to use the glFramebufferTexture2D funcion, which allow you to attach a texture to a frame buffer. Here you have an example, in the OpenGL wiki, where two textures are attached, the first one is attached to a color component, and the other one is attached to the depth component.

I catch no errors on that piece.

I tried to set different output colors.

Framebuffer appears to be valid according to glCheckFramebufferStatus.

Please, more options :(

Sorry for my English.

Sorry I have seen your code again and now I saw that you are using glFramebufferTexture2D function.

Advertisement

I catch no errors on that piece. I tried to set different output colors. Framebuffer appears to be valid according to glCheckFramebufferStatus.

I cannot see anything jumping out at me as being wrong. This might not fix anything but might be worth trying, use glFramebufferTexture rather then glFramebufferTexture2D.

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_map_tex, 0);

So the question is, where is your break point in CodeXL? . If the framebuffer is still attached prior to the breakpoint, you will not get any valid values( in fact I think it does give a warning when you try to view the attachments ). Also you are setting the viewport before you bind the framebuffer..iirc the viewport dim is a part of the framebuffer state( internal ). What happens if you manually write a depth value in your fragment shader ?

This topic is closed to new replies.

Advertisement