Jump to content

  • Log In with Google      Sign In   
  • Create Account


Stenciled Shadow volumes - after the shadows are rendered


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 mv348   Members   -  Reputation: 249

Like
0Likes
Like

Posted 07 November 2012 - 09:25 PM

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:


RenderSceneIntoDepth();
	glEnable(GL_STENCIL_TEST);
	RenderShadowVolIntoStencil();
	RenderShadowedScene();


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()
{
	glDrawBuffer(GL_BACK);
	glDepthMask(GL_FALSE);
	glStencilFunc(GL_EQUAL, 0x0, 0xFF);
	m_LightingTech.Enable();
	m_pointLight.AmbientIntensity = 0.0f;
	m_pointLight.DiffuseIntensity = 0.8f;
	m_LightingTech.SetPointLights(1, &m_pointLight);
	m_pGroundTex->Bind(GL_TEXTURE0);
	Pipeline p;
	p.SetPerspectiveProj(m_persProjInfo);
	p.SetCamera(m_pGameCamera->GetPos(), m_pGameCamera->GetTarget(), m_pGameCamera->GetUp());
	p.WorldPos(m_boxPos);
	p.Rotate(0, m_scale, 0);
	m_LightingTech.SetWVP(p.GetWVPTrans());
	m_LightingTech.SetWorldMatrix(p.GetWorldTrans());
	m_box.Render();
	p.Scale(10.0f, 10.0f, 10.0f);
	p.WorldPos(0.0f, 0.0f, 0.0f);
	p.Rotate(90.0f, 0.0f, 0.0f);
	m_LightingTech.SetWVP(p.GetWVPTrans());
	m_LightingTech.SetWorldMatrix(p.GetWorldTrans());
	m_quad.Render();
}

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, 07 November 2012 - 09:29 PM.


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 27585

Like
1Likes
Like

Posted 07 November 2012 - 09:37 PM

Is glDrawBuffer(GL_NONE) used at some point to disable colour-writes? If so, then glDrawBuffer(GL_BACK) would re-enable colour-writes.
Yes, glDepthMask(GL_FALSE) disables depth-writes.
What is the depth comparison function at the time that RenderShadowedScene 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.

#3 mv348   Members   -  Reputation: 249

Like
0Likes
Like

Posted 09 November 2012 - 01:00 AM

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);
	    EmitVertex();
	    LightDir = (normalize(GSin[2].WorldPos - gLightPos)) * EPSILON;
	    gl_Position = gVP * vec4((GSin[2].WorldPos + LightDir), 1.0);
	    EmitVertex();
	    LightDir = (normalize(GSin[4].WorldPos - gLightPos)) * EPSILON;
	    gl_Position = gVP * vec4((GSin[4].WorldPos + LightDir), 1.0);
	    EmitVertex();
	    EndPrimitive();

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


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, 09 November 2012 - 01:08 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS