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

Recommended Posts

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);
m_Camera.Look();

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

// Render the depth information
glDepthFunc(GL_LESS);
glCullFace(GL_BACK);
mesh.Render();

// Draw the front shadow volume faces

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

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

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

mesh.Render();

glDisable(GL_STENCIL_TEST);
}



Share on other sites
I really dont undertand what I am seeing...

Share on other sites
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?

Share on other sites
Quote:
 Original post by TomasHOh, 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.

Share on other sites
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?

Share on other sites
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

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• Forum Statistics

• Total Topics
632912
• Total Posts
3009199
• Who's Online (See full list)

There are no registered users currently online

×