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?