Shadow Volume problem

Started by
4 comments, last by Neophyte 19 years, 7 months ago
I've read loads of articles on shadow volumes and I've no problem with the theory but I can't seem to get it to work. I'm using the z-pass method in the code below. Here's a pic of the mesh and and the shadow volume(in red). And here's a pic of what I get when I use the code below. I'm not sure about the last value in the glStencilFunc()'s, but I don't know if they're causing it. Can anyone see what I'm doing wrong, because I don't know what else to try?

bool Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	glLoadIdentity();
        m_Camera.Look();

	// Set the light pos
	glLightfv(GL_LIGHT0, GL_POSITION, (float*)&lightPos);

	// Render the depth information
	glDepthFunc(GL_LESS);
	glDepthMask(1);
	glCullFace(GL_BACK);
	glColorMask(0, 0, 0, 0);
	mesh.Render();


	// Draw the front shadow volume faces
	CalculateShadowVolume();

	glDepthFunc(GL_LESS);
	glDepthMask(0);
	glCullFace(GL_BACK);
	glEnable(GL_STENCIL_TEST);
	glStencilFunc(GL_ALWAYS, 0, ~0);
	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
	drawSVQuads();


	// Draw the back faces
	glCullFace(GL_FRONT);
	glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);


	// Draw the mesh
//	glDepthMask(0);
	glDepthFunc(GL_EQUAL);
	glColorMask(1, 1, 1, 1);
	glCullFace(GL_BACK);
	glStencilFunc(GL_NOTEQUAL, 0, 1);

	mesh.Render();

	glDisable(GL_STENCIL_TEST);
}



Never accept failure when dealing with things that are within your control.
Advertisement
I really dont undertand what I am seeing...
Shouldn't the last stencilfunc (the one used when rendering the mesh) be glStencilFunc(GL_EQUAL, 0, 1) ?
You increase for front faces and decrease for back faces. So stencil would be zero for unshadowed areas, right?

Oh, and I just noticed that you don't actually draw the back faces of the shadow volume in the code below - there should be another call to drawSVQuads() there, right?
Quote:Original post by TomasH
Oh, and I just noticed that you don't actually draw the back faces of the shadow volume in the code below - there should be another call to drawSVQuads() there, right?


Haha, how did I forget that.


Quote:
Shouldn't the last stencilfunc (the one used when rendering the mesh) be glStencilFunc(GL_EQUAL, 0, 1) ?
You increase for front faces and decrease for back faces. So stencil would be zero for unshadowed areas, right?


O.K. I've changed the glStencilFunc but it still doesn't seem to be working.
I've noticed a weird thing too. If I disable the stencil test before the final rendering of the mesh, so in effect it should just render normally, I'm getting the same problem as I had before. Ie. The shadow doesn't appear and as I move the camera around the mesh doesn't render correctly and eventually doesn't render at all. I'm clearing all the state buffers each frame so I can't see how this could be happening.
Never accept failure when dealing with things that are within your control.
Well, I found another problem. I assumed I had enabled GL_CULL_FACE in my initialization code but I hadn't.

I have it working now except for one thing, the winding of the vertices for the shadow volume quads, so some faces that should be back faces are interpreted as front faces and vice versa.

There must be a standard way of ensuring the winding is correct during the creation of the faces, anyone know of one?
Never accept failure when dealing with things that are within your control.
When you build the shadow volume you can look at which of the edge's two faces are pointing towards the lightsource, and which is pointing away. Switch the winding if face2 is facing the lightsource and face1 is facing away (or possibly the other way around depending on the winding of your edges).

- Neophyte

EDIT: Minor type

This topic is closed to new replies.

Advertisement