• Advertisement
Sign in to follow this  

Stenciled Shadow volumes - after the shadows are rendered

This topic is 1900 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

I have been following this tutorial carefully. Usually these tutorials are very clear but this one has me confused.

The main steps are handled in the RenderSceneCB() function. After setting up a few uniform variables for the shaders, it calls the 3 functions in sequence:


You can find the definitions of each function in the tutorial-they're not that long.

First the entire scene is rendered (a box and a floor quad) depth-only.

Then the shadow-volumes are rendered which populates the stencil buffer based on when the depth-test fails for front and back faces. Cool. Makes sense.

The last bit confuses me. Take a look at the RenderedShadowScene() function:

void RenderShadowedScene()
glStencilFunc(GL_EQUAL, 0x0, 0xFF);
m_pointLight.AmbientIntensity = 0.0f;
m_pointLight.DiffuseIntensity = 0.8f;
m_LightingTech.SetPointLights(1, &m_pointLight);
Pipeline p;
p.SetCamera(m_pGameCamera->GetPos(), m_pGameCamera->GetTarget(), m_pGameCamera->GetUp());
p.Rotate(0, m_scale, 0);
p.Scale(10.0f, 10.0f, 10.0f);
p.WorldPos(0.0f, 0.0f, 0.0f);
p.Rotate(90.0f, 0.0f, 0.0f);

Mostly the real meat and potatoes happens in the first two calls I think. glDrawBuffer(GL_BACK) and glDepthMask(GL_FALSE). From my understanding. I have never scene the glDrawBuffer(GL_BACK) call before and the tutorial doesn't explain it. As for glDepthMask, my understanding here is that this means the depth test is still active, but writing new values does not occur.

Apart from not knowing what the point of glDrawBuffer(GL_BACK) is, I don't understand how the depth-test can work here. If the depth test is enabled, we should get Z-fighting, since "RenderSceneIntoDepth();" already filled the depth buffer. On the other hand, if we disable the depth buffer, than the pixels might be rendered out of order.

So confused... Edited by mv348

Share this post

Link to post
Share on other sites
Is [font=courier new,courier,monospace]glDrawBuffer(GL_NONE)[/font] used at some point to disable colour-writes? If so, then [font=courier new,courier,monospace]glDrawBuffer(GL_BACK)[/font] would re-enable colour-writes.
Yes, [font=courier new,courier,monospace]glDepthMask(GL_FALSE)[/font] disables depth-writes.
What is the depth comparison function at the time that [font=courier new,courier,monospace]RenderShadowedScene[/font] is called? If it's EQUAL or LEQUAL, then the scene will draw fine -- the depth values in this pass should be exactly the same as the depth values written in the first pass, so there'll be no z-fighting.

Share this post

Link to post
Share on other sites
Thanks for that very prompt and clear response, Hodgeman! I saw your response earlier but didn't have time to respond until now.

I just have one more question on a slightly different area of this topic. The primitives that form the shadow volume in the above tutorial are created using the geometry shader, Each triangle given to the Geometry Shader comes paired with 3 neighboring vertices, and these are used to determine which of its edges are silhouette edges. These edges are projected (as a quad) an infinite distance behind the occluder and they form the 'sides' of the volume. The triangle (if front facing) is rendered at its present location and it forms part of the front cap of the volume, and another triangle (projected backwards) forms the back cap.

Makes sense. What I'm confused about is a small tweak used to avoid floating point inaccuracies. Here is the code from the Geometry Shader that creates the front cap:

// front cap
vec3 LightDir = (normalize(GSin[0].WorldPos - gLightPos)) * EPSILON;
gl_Position = gVP * vec4((GSin[0].WorldPos + LightDir), 1.0);
LightDir = (normalize(GSin[2].WorldPos - gLightPos)) * EPSILON;
gl_Position = gVP * vec4((GSin[2].WorldPos + LightDir), 1.0);
LightDir = (normalize(GSin[4].WorldPos - gLightPos)) * EPSILON;
gl_Position = gVP * vec4((GSin[4].WorldPos + LightDir), 1.0);

As you can see, the vertex is shifted slightly by a distance of EPSILON, in the direction of the light. I don't quite understand the need for this. The tutorial explains this tweak by stating:
The reason is that due to floating point errors we might encounter bizarre corruptions where the volume hides the front cap. Moving the cap away from the volume by just a bit works around this problem[/quote]

I don't really understand how the sides of the volume could hide the cap but even so, that would probably occur if the sides of the volume somehow got in front of the cap. But what this does is move the vertex along the vector from the light to the occluder, placing the cap deeper inside the volume, and so I would think this would only make matters worse.

Why exactly is that tweak needed and why does it work?
. Edited by mv348

Share this post

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

  • Advertisement