Stencil Shadow Problems


I have a problem with stencil shadows. I coded up an algorithm that calculates the silhouette of the projection (using the standard algorithm that checks the facing of two adjacent triangles in relation to the light position).

It really seems to work nicely, as the following screenshot shows:

Now when I use a different model, the shadow doesn’t work any more

The question is: What goes wrong?

I already had different ideas that I tried:

  1. The second model uses more than one mesh - this could be a problem as there will be edges the virtually are the same but are in different meshes. I already checked this, but this seems not to be the single problem - even if I only draw the shadow of one mesh, I get these ugly artefacts. I also merged all meshes together, but this doesn’t help much, even when I collapse all identical vertices.

  2. Another guess was some numeric problems when calculating the plane equations of the triangles. So I checked all normals and discarded the triangles with normals below a certian threshold.

  3. I also suspected that I might have some problems with some edges that do not belong to two triangles (that is a hole in the model). I included a special case for these and made sure that they will always be included in the silhouette - but this didn’t help much either.

So here is the question: What bad properties might the model have and how can I work around these problems? Both models have all triangles with correct orientations (ie backface culling really works) and they are both more or less in the same scale.

Any ideas are welcome.


Nice tank. :slight_smile:

Without seeing any code, and without knowledge of your method, it looks to me like you either have a bunch of degenerate triangles in your model, or bad triangle connectivity (lack of complete winged edge info), or both.

Depending on your method (zfail, zpass, one pass with 2 sided stencil test, etc.) and geometry, different cases can arise.

If you can convince yourself that your geometry setup is consistent with your method, then you know it’s your code that’s flawed, or vice versa.

Thanks for you reply. I guess my code works, see the first image - I will try to post my code later, it needs some mangling for the forum.

But I really suspect that I have some problems with the model - is there any way I can fix the model in an algorithmic way (by removing triangles or something like this)?

As I already mentioned, I already tried to remove all double vertices, but this didn’t help much.


You can try a sanity test for a model that should be closed 2-manifold:

  1. Build a list of all unique edges in the model (if you don’t have such a list already)
    During the construction of this list, you need to detect duplicate edges (bad), and degenerate edges as well. All edges should occur once only, something like this figure
             Tri A
  edge-> *------------*  
             Tri B
  1. Walk the list and make sure each edge has exactly 2 triangles associated with it (a winged edge) If you find a bad edge, you’ve found a culprit.

Thanks for your answer again. I didn’t have much time for further investigations, as I first have to work on some other parts of my program.

I know for sure, that the tank model has some bad triangles (I already checked that) - I will try to find some way to work around these, I already have some ideas. I will post my results later, when I (hopefully) found a nice solution.