Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Shadows Problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Edited by Medo3337

Share this post


Link to post
Share on other sites

@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);

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!