iDevGames Forums
Shadowing bug - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Shadowing bug (/thread-377.html)

Shadowing bug - TomorrowPlusX - Jan 31, 2010 08:20 AM

A while back I implemented omnidirectional shadow maps, and thought that they worked.Rolleyes They didn't! But they looked like they were working but in reality, there were some odd bugs resultant from my managing of near/far depth precision. And then I had to spend a month working double time for my job and sort of lost track of things.

Anyway, I think I have it worked out now, and shadowing seems quite robust, but for a small likely offset-related bug. Here are three screenshots in sequence. You'll see a cylinder in the foreground. Its shadow on the floor has a sort of half-moon area missing where the cylinder touches the floor. As the cylinder is raised, the half-moon fills in.

[Image: ShaderUnification-2010-01-31-00.png]

[Image: ShaderUnification-2010-01-31-01.png]

[Image: ShaderUnification-2010-01-31-02.png]

I tried adding a bias to the depth sampling, but it didn't help, it just caused horrific self-shadowing acne.

Here's some example GLSL code. I'm showing only the single-tap version for clarity.

#define BIAS 0.0

float positional_shadow_sample( in vec3 dir, in float fragDepth )
    float shadowDepth = FixedToFloat( textureCube( ShadowMap, dir ));
    return fragDepth < shadowDepth ? 1.0 : 0.0;

float shadow_coefficient_low( in vec3 ecPos )
    #ifdef SHADOWED

        float dist = length( LightDirWorld );
        vec3 lightDir = LightDirWorld / dist;
        float depth = ((dist - MinDepth) / DepthRange) + BIAS;
        float shadow = positional_shadow_sample( lightDir, depth );

        #ifdef SELF_SHADOW_FIX
            vec3 ec_normal = normalize(cross(dFdx(ecPos), dFdy(ecPos)));
            float lambertian = dot(ec_normal,normalize( gl_LightSource[0] - ecPos ));

            return step( 0.004, lambertian ) * shadow;
            return shadow;
        return 1.0;

If I set BIAS to a small value > 0, the half-moon goes away, but the acne shows up.

Any thoughts? Should I just live with it?LOL

Shadowing bug - akb825 - Jan 31, 2010 05:33 PM

I've usually seen this kind of artifact when you render only backfaces into the shadow map. This is because the triangles on the back of the cylinder are very close to the triangles on the ground, and the polygon offset pushes the triangles outside the threshold for shadowing. Assuming this is the case, you essentially have two options: one is to try to find a polygon offset that gives you an optimal balance between the artifacts of having too much or too little biasing, and the other is to render front faces into the shadow buffer. Rendering front faces will show more acne if your polygon offset isn't right, but it should get rid of these light seams. I would recommend going for the rendering front faces rout since no matter how much you tune it, gaps of light in your shadows are very noticeable.

Shadowing bug - TomorrowPlusX - Jan 31, 2010 05:52 PM

That's pretty much what I was expecting, but I was under the impression that front-faced shadow rendering was almost impossible to tune effectively to prevent acne. Worth a looksy, though.

Shadowing bug - akb825 - Jan 31, 2010 10:55 PM

It's definitely not impossible, but it will probably take a bit longer to get a good set of values. Filtering can also help reduce any acne might pop up occasionally.

Shadowing bug - DoG - Feb 1, 2010 01:03 AM

I don't quite think its just depth precision. After all, the shadows seem to work much better for the pig et al. It seems to me that you may have a large values set for the in glPolygonOffset()?

If you have a working binary, it might be interesting to see how it performs on other graphics hardware, too.

Shadowing bug - TomorrowPlusX - Feb 1, 2010 04:53 AM

Actually, right now I'm not polygon-offsetting during shadow rendering at all.

EDIT: I've been thinking about this, and it really seems not to be a polygon offset issue. The distance I drag the cylinder up away from the floor to ge tthe shadow to render correctly is way too far for this to be a simple offset matter, even if I'm using back-facing polys. I have to assume I'm incorrectly computing the depth when writing to the depth buffer, or else I'm incorrectly comparing in the lit shadow sampling code.

Shadowing bug - DoG - Feb 1, 2010 12:00 PM

The reason I thought polygon offset was wrong is because of the crescent shaped shadow. It seems that the slope of the polygon to the light rays affects the depth, eg the side walls, nearly tangential to the light, seem to produce a correct shadow, while the back wall, which is more perpendicular, seems to be offset too far back.