Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Since 2 days, i'm trying to figure out whats wrong with my shadow shader. The shadow map is drawn correctly, just the projection on to the scene is not working.

enter image description here

The Vertex Shader Code seems fine:

VertexShaderOutput VertexShaderFunction(float3 Position : POSITION0, float2 TexCoord : TEXCOORD0)
{
    VertexShaderOutput output;
    output.Position = float4(Position,1);
    output.Position.x =  Position.x - 2*halfPixel().x;
    output.Position.y =  Position.y + 2*halfPixel().y;
    output.TexCoord = TexCoord;
    return output;
}

float sampleShadowMap(float2 UV, sampler2D shadowSampler)
{
    if (UV.x < 0 || UV.x > 1 || UV.y < 0 || UV.y > 1)
        return 1;

    return tex2D(shadowSampler, UV).r;
}

In my opinion the real issue should be the matrix multiplications. But i've tried so many things, i just cant get it to work.

       float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
            {
                float depth = tex2D(depthSampler, input.TexCoord).r;
                float4 worldPosition;
                worldPosition.x = input.TexCoord.x * 2 - 1;
                worldPosition.y = (1 - input.TexCoord.y) * 2 - 1;
                worldPosition.z = depth;
                worldPosition.w = 1.0f;

                // Transform position from screen space to world space
                worldPosition = mul(worldPosition, InvViewProjection);
                worldPosition.xyz /= worldPosition.w;
                worldPosition.w = 1;
                float shadow = 1;

                float4 shadowScreenPosition = mul(worldPosition, shadowViewProjection);
                shadowScreenPosition.w = 1;
                float2 shadowTexCoord = shadowScreenPosition .xy / shadowScreenPosition .w;
               shadowTexCoord = 0.5f * (float2(screenPos.x, -screenPos.y) + 1) + 0.5 / float2(width, height);

                float shadowMapDepth = sampleShadowMap(shadowTexCoord, shadowSampler);
                float realDepth = shadowScreenPosition.z / shadowFarPlane;
                if (realDepth < 1 && realDepth - ShadowBias > shadowMapDepth)
                    shadow *= shadowMult;   

                return float4(shadow, shadow, shadow, 1);
            }

Edit:

When i remove the line worldPosition.w = 1; from the pixel shader, the output is following: enter image description here

share|improve this question
    
Have you verified that your worldPosition is calculated correctly? (For instance, output worldPosition.xyz as RGB values and check that the colors correspond correctly to your world coordinates.) Also, why are you forcing shadowScreenPosition.w = 1? –  Nathan Reed Sep 10 '13 at 19:49
    
Well i dont rly know how to debug the worldPosition in HLSL. –  Philipp Schladitz Sep 11 '13 at 6:25
    
You can use PIX (from the DirectX SDK) to debug shaders. Or, like I said, do "visual debugging" by writing worldPosition.xyz to the render target. –  Nathan Reed Sep 11 '13 at 6:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.