Bug drawing to depth-only framebuffer

I’m having a hard time implementing shadow maps using framebuffer objects. If I clear the buffer (either using glClear(GL_DEPTH_BUFFER_BIT) or by drawing a quad at the far plane after calling glDepthFunc(GL_ALWAYS)) nothing else appears to be rendered into the framebuffer. If I don’t clear the buffer, then the scene gets drawn, but of course accumulates between frames. If I clear only part of the buffer by drawing a quad covering only part of the buffer, that part which is cleared has nothing else drawn on it, whilst the part which is not cleared accumulates between frames. It seems as if one of two things is happening:

1: The scene really is not being drawn over the portion which has been cleared - but I cannot for the life of me see why: I have disabled stencil buffers (which my framebuffer shouldn’t havce anyway), alpha testing etc.

or (I think, more likely)

2: The glClear or quad drawing is happening after the scene drawing.

Details:

I create the framebuffer with the following code:

glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
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_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, m_size, m_size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);

glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffersEXT(1, &m_framebuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_framebuffer);
// Attach depth texture
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_texture, 0);

// There are no color buffers attached
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

I draw to the framebuffer with the following code:

glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_framebuffer );
glPushAttrib( GL_VIEWPORT_BIT );
glViewport( 0, 0, m_size, m_size );

glDepthFunc( GL_ALWAYS );
clear( 1.0f ); //This function either calls glClear or draws a quad at the far plane
glDepthFunc( GL_LESS );

// Render the scene
for( RenderPairList::iterator rp = m_render_pairs.begin(); rp != m_render_pairs.end(); ++rp )
{
	rp->m_packet->render();
}

glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
glPopAttrib();

I would be very gratefull if anyone could shed some light on this

Guy

It would be better if you could provide us with a test case that illustrates the problem. That way we can check out if it’s a bug (works on our hardware) or if you’re missing some function calls.

e.g. I don’t see glEnable(GL_DEPTH_TEST) anywhere.

or if you created a bug yourself:

e.g. in glOrtho(…,znear,zfar) the far plane is at zfar while in
gluPerspective(…,znear,zfar) the far plane is at -zfar.

Thanks for having a look at this. Your reply somehow inspired me to find the bug - it turns out the code in question works fine, but I made an incorrect assumption about the (rather large) application I was working with (it calls my code more than once per frame).