Original Post
Hello,
Ok, what my problem is that I have developed deferred rendering engine for my game. Now I want to apply shadows to it. So first I am trying with simple PCF shadows. I thought rather constructing position from depth to apply shadows in the deferred pass why not do shadow calculations when drawing the model as there is only 1 light for which I would be drawing shadows. But I am having strange results:
Image1
Image2
Here is shader for storing depth when drawn from light position:
And here is the code when applying shadow map:
What could be wrong?? Thanks.
Ok, what my problem is that I have developed deferred rendering engine for my game. Now I want to apply shadows to it. So first I am trying with simple PCF shadows. I thought rather constructing position from depth to apply shadows in the deferred pass why not do shadow calculations when drawing the model as there is only 1 light for which I would be drawing shadows. But I am having strange results:
Image1
Image2
Here is shader for storing depth when drawn from light position:
float4x4 lightWorldViewProjection;void shadowMapVS(float3 posL : POSITION0, out float4 posH : POSITION0, out float2 depth : TEXCOORD0){ posH = mul(float4(posL, 1.0f), lightWorldViewProjection); depth = posH.zw;}float4 shadowMapPS(float2 depth : TEXCOORD0) : COLOR{ return depth.x / depth.y; // z / w}technique shadowMapTechnique{ pass P0 { vertexShader = compile vs_2_0 shadowMapVS(); pixelShader = compile ps_2_0 shadowMapPS(); }}And here is the code when applying shadow map:
float4x4 world;float4x4 view;float4x4 projection;float3 eyePos;bool lod;bool leaves;float Time;bool tree;bool light;float4x4 InverseTransposeView;float SMAP_SIZE;float4x4 lightWVP;static const float SHADOW_EPSILON = 0.0001f;float3 lightDir = float3(0.0f, 10000.0f, 2000.0f);float4 lightColor = float4(1.0f, 1.0f, 1.0f, 1.0f);float4 materialAmbient = float4(0.8f, 0.8f, 0.8f, 1.0f);float4 materialDiffuse = float4(0.8f, 0.8f, 0.8f, 1.0f);//-----------------------------------------------------------------------------// Textures.//-----------------------------------------------------------------------------texture shadowMap;sampler shadowMapSampler = sampler_state{ Texture = <shadowMap>; MinFilter = POINT; MagFilter = POINT; MipFilter = POINT; AddressU = CLAMP; AddressV = CLAMP;};texture colorMap;sampler colorMapSampler = sampler_state{ Texture = <colorMap>; MinFilter = Anisotropic; MagFilter = Linear; MipFilter = Point; MaxAnisotropy = 16;};struct OutputVS{ float4 vPos : POSITION0; float2 vTex0 : TEXCOORD0; float3 vWorldPos : TEXCOORD1; float3 vWorldNrm : TEXCOORD2; float3 eyeVec : TEXCOORD3; float4 vPos1 : TEXCOORD4; };struct OutputPS{ float4 vMaterial : COLOR0; float4 vWorldNrm : COLOR1; float4 vWorldPosXY : COLOR2; };OutputVS VS_Lambert(float3 posL : POSITION0, float3 tangentL : TANGENT0, float3 binormalL : BINORMAL0, float3 normalL : NORMAL0, float2 tex0 : TEXCOORD0){ OutputVS outVS = (OutputVS)0; float4 vertex = mul(float4(posL, 1), world); /*if(leaves) { //float amplitude = magnitude * (vertex.y + 20); float4 wave = 0.06 * float4(1.5 * sin(Time + vertex.x), 0, 1.5 * cos(Time + vertex.z), 0); vertex = vertex + wave; }*/ float4x4 ViewSpace = mul(world, view); outVS.vWorldPos = mul(float4(posL, 1), ViewSpace); float3 vWorldPos = mul(float4(posL, 1), ViewSpace); outVS.eyeVec = vertex.xyz - eyePos; float4x4 viewProjection = mul(view, projection); outVS.vPos = mul(float4(outVS.vWorldPos, 1), projection); outVS.vPos1 = mul(float4(posL, 1), lightWVP); outVS.vTex0 = tex0; outVS.vWorldNrm = normalize(mul(float4(normalL, 0), (float3x3) InverseTransposeView)); return outVS; }//-----------------------------------------------------------------------------// Pixel shader.//-----------------------------------------------------------------------------OutputPS PS_Lambert(OutputVS i) : COLOR{ OutputPS o; float3 texColor = tex2D(colorMapSampler, i.vTex0); o.vWorldNrm.xyz = 0.5f * (i.vWorldNrm + 1.0f); o.vWorldNrm.w = 0.0; o.vWorldPosXY = float4(i.vWorldPos.xyz, 0); float4 projTex = i.vPos1; projTex.xy /= projTex.w; projTex.x = 0.5f * (projTex.x + 1.0f); projTex.y = 0.5f * (projTex.y + 1.0f); projTex.y = 1 - projTex.y; float depth = projTex.z / projTex.w; float2 texelpos = SMAP_SIZE * projTex.xy; float2 lerps = frac( texelpos ); float dx = 1 / SMAP_SIZE; float s0 = (tex2D(shadowMapSampler, projTex.xy).r + SHADOW_EPSILON < depth) ? 0.2f : 1.0f; float s1 = (tex2D(shadowMapSampler, projTex.xy + float2(dx, 0.0f)).r + SHADOW_EPSILON < depth) ? 0.2f : 1.0f; float s2 = (tex2D(shadowMapSampler, projTex.xy + float2(0.0f, dx)).r + SHADOW_EPSILON < depth) ? 0.2f : 1.0f; float s3 = (tex2D(shadowMapSampler, projTex.xy + float2(dx, dx)).r + SHADOW_EPSILON < depth) ? 0.2f : 1.0f; float shadowCoeff = lerp(lerp(s0, s1, lerps.x), lerp(s2, s3, lerps.x), lerps.y); o.vMaterial.rgb = texColor*shadowCoeff; o.vMaterial.a = tex2D(colorMapSampler, i.vTex0).a; return o;}technique Lambert{ pass { VertexShader = compile vs_3_0 VS_Lambert(); PixelShader = compile ps_3_0 PS_Lambert(); AlphaTestEnable = true; AlphaFunc = GreaterEqual; AlphaRef = 224; CullMode = None; }}What could be wrong?? Thanks.
