FBO, problems clearing depth buffer

This has been solved now: I had to call glDepthMask(GL_TRUE) before clearing.

Hello,
I am having trouble with the depth buffer and my fbo. Everything renders correctly if I disable depth test or if I render directly to the frame buffer instead of the fbo. It looks like the depth buffer that is attached to the fbo is not cleared correctly, I can also see some flickering of the rendered object when I move the camera. My box is rendered correctly in the first frame when I do frame by frame stepping in CodeXL but it is not visible in the following frames.

I get no errors from CheckFrameBufferStatus or GetError and the fbo looks ok when I check it in CodeXL (1 texture and 1 render buffer, format = GL_DEPTH_COMPONENT24, type = GL_FLOAT).

All OpenGL calls below have been extracted using AMD’s CodeXL.

Creation of the fbo:

// Texture setup
glGenTextures(1, {0})
glBindTexture(GL_TEXTURE_2D, 2) [Context 4 - Texture 2: No preview available]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 512, 512, 0, GL_RGBA, GL_FLOAT, 0x000000) [Context 4 - Texture 2: No preview available]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) [Context 4 - Texture 2: Context 4 - Texture 2]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) [Context 4 - Texture 2: Context 4 - Texture 2]
glFinish()
glGetError()

// Frame buffer setup
glGenFramebuffers(1, 0x29A83C4)
glBindFramebuffer(GL_FRAMEBUFFER, 1)

// Attach texture
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 2, 0)

// Create and attach depth render buffer
glGenRenderbuffers(1, 0x29A83C8)
glBindRenderbuffer(GL_RENDERBUFFER, 1)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 512, 512)

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 1)
glBindRenderbuffer(GL_RENDERBUFFER, 0)

glCheckFramebufferStatus(GL_FRAMEBUFFER)
glBindFramebuffer(GL_FRAMEBUFFER, 0)

Below are all the draw calls for a complete frame:

// Render scene to texture
glBindFramebuffer(GL_FRAMEBUFFER, 1)
glDrawBuffers(1, {GL_COLOR_ATTACHMENT0})
glViewport(0, 0, 512, 512)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glUseProgram(33) [Context 3 - program 33: shader 31, shader 32]
glDisable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
glDepthMask(TRUE)
glEnable(GL_CULL_FACE)
glBlendFunc(0, 1)
glCullFace(GL_BACK)
glDepthFunc(GL_LESS)
glUniformMatrix4fv(0, 1, FALSE, {0.47494337, -1.4067751, 0.32057661, 0.32045144} {5.5997848e-008, 0.6356858, 0.93057901, 0.9302156} {0.85067475, 0.78542173, -0.17898236, -0.17891248} {1.7013494, 0.80802053, -1.6746986, -1.4740837})
glBindVertexArray(4)
glDrawElements(GL_TRIANGLES, 612, GL_UNSIGNED_INT, 0x000000)

// Render texture to fullscreen quad
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glDrawBuffer(GL_BACK)
glViewport(0, 0, 1280, 720)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glUseProgram(6) [Context 3 - program 6: shader 4, shader 5]
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, 2) [Context 3 - Texture 2: Context 3 - Texture 2]
glDisable(GL_BLEND)
glDisable(GL_DEPTH_TEST)
glDepthMask(FALSE)
glEnable(GL_CULL_FACE)
glBlendFunc(0, 1)
glCullFace(GL_BACK)
glDepthFunc(GL_LESS)
glUniform1i(0, 0)
glBindVertexArray(5)
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0x000000)
wglGetCurrentDC()
wglSwapBuffers(0xE40123A5)

Anyone got any ideas? I have searched on google but have not found anything that helps so far. It feels like I’m missing something obvious in my opengl calls.