I am implementing a shadow volume scheme, where I need to change the projection matrix after rendering the scene with ambient light (both to color and depth buffer). Then depth write is disabled, shadow volumes are rendered to the stencil buffer and the scene is rendered lit with glDepthFunc(GL_EQUAL).
When inserting the following four lines of code after rendering the scene in shadow, but before rendering shadow volumes the final rendered image get distorted:
According to my knowledge these should not change anything, but apparantly something happens to the depth buffer precision when rendering the scene for the second time. It is crucial that nothing has changed since I render with glDepthFunc(GL_EQUAL).
I tried reading the depth and the stencil buffer before and after executing the following above four lines. Nothing changes.
Doing the following:
Does not create distortion.
What is the problem here? Can anyone please help me?
glColor3f(0.2f, 0.2f, 0.2f); //ambient light
glStencilFunc(GL_ALWAYS, 0, ~0);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// Render shadow volumes
glLoadIdentity(); // THIS LINE SCREWS THINGS UP ??? Without it everything is OK.
// Render scene lit
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glColor3f(0.6f, 0.6f, 0.6f); //light on.
glStencilFunc(GL_EQUAL, 0, ~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);