Shadows Problem

Started by
42 comments, last by Medo Mex 10 years, 4 months ago

I have been trying to get shadows to work for days, but I'm getting half of the scene darker instead of shadows:

[attachment=18737:scene.png]

What's going wrong?

C++:


// NOTICE: The camera used for shadow map is not lighting view point, I'm trying to get shadows to work first
D3DXMatrixLookAtLH(&lightView, &D3DXVECTOR3(0.0f, 10.0f, 340.0f), &D3DXVECTOR3(0.0f, 10.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
lightProj = camera->getProjection();

float fTexOffs = 0.5 + (0.5 / (float)SHADOW_MAP_SIZE);
D3DXMATRIX matTexAdj( 0.5f, 0.0f, 0.0f, 0.0f,
                      0.0f,    -0.5f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      fTexOffs, fTexOffs,  0.0f, 1.0f );

D3DXMATRIX lightWorldViewProj = lightWorld * lightView * lightProj;
D3DXMATRIX matTexture = lightWorldViewProj * matTexAdj;
effect->SetMatrix("g_matTexture", &matTexture);

Generating depth map shader:


VS_OUT VS(VS_IN IN)
{
   ...
    OUT.Z = mul(IN.Pos, WorldViewProj).z;
    return OUT;
}

PS_OUT PS(VS_OUT IN)
{
    OUT.Color = ...
    OUT.Z = float4(IN.Z, IN.Z, IN.Z, 1.0f);
    return OUT;
}

Final scene shader:


texture shadowMapTexture;
sampler shadowMap = sampler_state
{
    texture = <shadowMapTexture>;
    MipFilter = Linear;
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

VS_OUT VS( VERTEX IN )
{
     ...
     OUT.vTexCoord = mul( IN.Position, g_matTexture );
     return OUT;
}

PS_OUT Shadow ( VS_OUT IN )
{
     ...
     float fShadowTerm = tex2Dproj( shadowMap, IN.vTexCoord ) < (IN.vTexCoord.z - 0.001f) ? 0.1f : 1.0f;
     return color * fShadowTerm;
}

Advertisement

You might want to look at my writeup of the shadow mapping code I implemented a couple weeks ago: http://www.richardssoftware.net/2013/10/shadow-mapping-with-slimdx-and-directx.html. It's written for C# and SlimDX, but the core concepts should transfer back to C++ fairly straightforwardly.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

The vertices in your forward pass needs to be transformed into light space to do the shadow map lookup...you cannot just use object space position.

@cgrant:

Yes, I'm already multiplying IN.Position with WorldViewProj, but still same problem

@ericrrichards22:

I'm currently relying on this tutorial:

http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/soft-edged-shadows-r2193

I'm still having this problem for days and it's really troubling, I see that there is nothing wrong with my code, any idea what's going on?

Here is what I do:

- Render the scene from another camera view to get depth texture (shadow map)

- Render the scene normally while passing the depth texture to the shader

- Multiply the vertex position with g_matTexture

- Compare the depth in Pixel Shader to determine the shadow term: fShadowTerm = tex2Dproj( shadowMap, IN.vTexCoord ) < (IN.vTexCoord.z - 0.001f) ? 0.1f : 1.0f;

Any help would be greatly appreciated.

Does your depth texture look right? It might be helpful to render it out to a small quad, just as a sanity check.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

@ericrrichards22: I tried rendering the depth values to the color surface as grayscale and I see it's working well, however the shadow is not working


OUT.Color = float4(IN.Z / fFarPlane, IN.Z / fFarPlane, IN.Z / fFarPlane, 1.0f);

Maybe change your shadow map texture sampler to use BORDER and set a wacky border color. That may pinpoint if your projective texture matrix is off. With CLAMP, if you are generating texture coordinates outside of the shadow map, and the edge of the shadow map is black, it will show up as in shadow.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

I tried working on my own code and now I see that the shadow appear but the shadow is tiled many times and not even adjusted in the correct place


...shadow is tiled many times...

You might want to change texture addressing mode to clamp or border.

This topic is closed to new replies.

Advertisement