Jump to content
  • Advertisement
Sign in to follow this  
miggs

OpenGL shadow buffer depth not reset properly

This topic is 2236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:


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));



and thats how i use them

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));


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

vxj0c6.png

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!