Sign in to follow this  
miggs

OpenGL shadow buffer depth not reset properly

Recommended Posts

it seems as if glClear(GL_DEPTH_BUFFER_BIT); does not do it's job properly (probably due to some wrong setup on my side).

when i render the depth into a color attachment (the commented code instead of the uncommented code) using the gl_Position.z component in the fragment shader everything works fine, when my sun is moving and the shadow map is recalculated, it is correctly reset and redrawn and i have correct shadows.

here is the setup code:

[CODE]
CheckGL(glGenFramebuffers(1, &m_Fbo));
CheckGL(glBindFramebuffer(GL_FRAMEBUFFER, m_Fbo));

//glActiveTexture(GL_TEXTURE0);
CheckGL(glGenTextures(1, &m_ShadowTexture));
// depth
CheckGL(glBindTexture(GL_TEXTURE_2D, m_ShadowTexture));
CheckGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, sMapWidth, sMapHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0));
CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
CheckGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE));
CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));//GL_CLAMP_TO_BORDER
CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));//GL_CLAMP_TO_BORDER
CheckGL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_ShadowTexture, 0));
//CheckGL(glBindTexture(GL_TEXTURE_2D, m_ShadowTexture));
//CheckGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sMapWidth, sMapHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0));
//CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
//CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
//CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
//CheckGL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
//CheckGL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ShadowTexture, 0));
// disable writing to the color buffer
CheckGL(glDrawBuffer(GL_NONE));
CheckGL(glReadBuffer(GL_NONE));
CheckGL(glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ));
CheckGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
bool succeeded = OpenGLCheckFramebuffer(__FILE__, __LINE__);
CheckGL(glBindTexture(GL_TEXTURE_2D, 0));
CheckGL(glBindFramebuffer(GL_FRAMEBUFFER, 0));
[/CODE]


and thats how i use them
[CODE]
CheckGL(glBindFramebuffer(GL_FRAMEBUFFER, m_Fbo));

CheckGL(glDrawBuffer(GL_NONE));
CheckGL(glReadBuffer(GL_NONE));

// when using color attachment
//CheckGL(glDrawBuffer(GL_COLOR_ATTACHMENT0));

CheckGL(glViewport(0,0,sMapWidth, sMapHeight));

CheckGL(glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ));
CheckGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); // when using GL_DEPTH_BUFFER_BIT with depth_attachment, i only see black, while when clearing both color and depth i see the depth attachment without beeing cleared (screenshot attached)
CheckGL(glColorMask(0,0,0,0)); // this is 1,1,1,1 if i use color_attachment


// render scene...

CheckGL(glBindFramebuffer(GL_FRAMEBUFFER, 0));
CheckGL(glColorMask(1,1,1,1));
[/CODE]

here's how it looks like:
the top 2 images are using the depth attachment, i can't provide a video, but when the sun is moving, this is what happens (no depth clearing)
the bottom 2 images clear and render correctly

[img]http://i47.tinypic.com/vxj0c6.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this