stencil Shadow Volume zpass

i use this silhuette detection function:

for(loop=0;loop<part[frame].planezahl*3;loop+=3) // loop through all triangles
			n = fetchnormal;             // set plane normal
			NDotL = n.dotProduct(lPos);  // caclulate angle beetween normal and lightdir
			if(NDotL>0.0)                // Triangle faces the light
				pT[0] = fetchfirstvertex;
				pT[1] = fetchsecondvertex;
				pT[2] = fetchthirdvertex;
				for(int i=0;i<3;i++)    // loop through vertices
					p1 = pT[i];
					p2 = pT[(i+1)%3];
					// Loop through silhuetteEdges vector
					bool killed = false;
					for(int i=0;i<silhuetteEdges.size();i++)
					       // if there neighbour already pushed bac:
						    // kill neighbour
							killed = true;
					// if nothing was killed -> push current edge back
						edge temp;
						temp.p1 = p1;
						temp.p2 = p2;

as shadowvolume i loop through the silhuetteEdges and render the quads with extruded vertices.

it looks like this:

everything works fine with simple objects.

but with complex models i get this result:

i know there is antoher thread about stencil shadows here but i don’t understand my problem.

(look at the silhuette determine function. i use Lightdirection dot Normalvector so only light facing triangles are submitted)

a possible problem i could think about is the orientation of the vertices. imagine some triangles have other oriented vertices and the shadowvolume quad is not clockwise anymore.

thanks a much

You need to extrude all silhouettes. Also geometry must be well formed (no cracks no random crap in the model hull.

It is best to use INC_WRAP and DEC_WRAP to avoid overflow and underflow issues.

Finally the stencil buffer needs sufficient bits to handle values you total to stencil by the time you use the contents for the test.

I think you may have several of these problems judging by the screenshot.

stencil wrap was the trick