hey everyone,
im trying to apply shadows in my d3d9 game.Shadows should be seen only on terrain not object meshes.
my main render func like this;
renderterrain()
renderobjectmeshes()
rendercharacter()
.
.
.
i got the shadow map texture with depth pass to my pixel shader.Shadow map is fully correct i double checked it.
so than next thing to do is show shadows on terrain.
i tried like this;
1-set the depth pass pixel and vertex shader to get shadow map
2-get shadow map with depth pass
3-restore render target and depth stencil buffer
4-set shadow texture to 2nd slot (settexture(2,g_tShadowMap)
5-set the pixel shader for show the shadows and i set the vertex shader null cause i think no need vertex shader for render my terrain at least for now.
6-render terrain
result : all the terrain just dark and i can't debug my pixel shader pix gives me error with D3D_INVALIDCALL
my terrain has multi textures so i used lerp in pixel shader
this is my pixel shader for scene
struct PS_INPUT
{
float4 Position : POSITION;
float2 UV : TEXCOORD0;
float3 Normal : TEXCOORD1;
float4 LightViewPosition : TEXCOORD2;
};
#ifdef USE_SHADOWS
#if defined(SOFT_SHADOWS)
static const float2 PCF_Offset_16x[16] = {
float2(-1.5f, -1.5f),
float2(-1.5f, -0.5f),
float2(-1.5f, 0.5f),
float2(-1.5f, 1.5f),
float2(-0.5f, -1.5f),
float2(-0.5f, -0.5f),
float2(-0.5f, 0.5f),
float2(-0.5f, 1.5f),
float2(0.5f, -1.5f),
float2(0.5f, -0.5f),
float2(0.5f, 0.5f),
float2(0.5f, 1.5f),
float2(1.5f, -1.5f),
float2(1.5f, -0.5f),
float2(1.5f, 0.5f),
float2(1.5f, 1.5f),
};
#define PCF_SAMPLE_COUNT 16
#define PCF_SAMPLE_ARRAY PCF_Offset_16x
#else
#define PCF_SAMPLE_COUNT 1
static const float2 PCF_Offset_1x[1] = {
float2(0.0f, 0.0f),
};
#define PCF_SAMPLE_ARRAY PCF_Offset_1x
#endif
sampler2D ShadowMap : register(s2);
float4 ShadowMapTexelSize : register(c4);
#endif
float3 MaterialDiffuse : register(c0);
float3 LightDirection : register(c1);
float4 LightDiffuse : register(c2);
float4 AmbientColor : register(c3);
sampler2D DiffuseTexture : register(s0);
sampler2D DiffuseTexture2 : register(s1);
float2 clipSpaceToTextureSpace(float4 clipSpace)
{
float2 cs = clipSpace.xy/* / clipSpace.w*/;
return float2(0.5f * cs.x, -0.5f * cs.y) + 0.5f;
}
float4 main(PS_INPUT input) : SV_TARGET
{
float4 Color = tex2D(DiffuseTexture, input.UV);
float4 Color2 = tex2D(DiffuseTexture2,input.UV);
Color = lerp(Color,Color2,0.5);
if (Color.a < 0.33f)
discard;
float LightIntensity = saturate(dot(normalize(input.Normal), LightDirection));
float3 LightResult = ((MaterialDiffuse * LightDiffuse.rgb * LightDiffuse.a) * LightIntensity);
#ifdef USE_SHADOWS
const float ShadowBias = ShadowMapTexelSize.z;
float Shadow = 1.0f;
const float2 LightTexCoord = clipSpaceToTextureSpace(input.LightViewPosition);
const float fCurrDepth = input.LightViewPosition.z;
if (saturate(LightTexCoord.x) == LightTexCoord.x
&& saturate(LightTexCoord.y) == LightTexCoord.y
&& saturate(fCurrDepth) == fCurrDepth)
{
Shadow = 0.0f;
#if PCF_SAMPLE_COUNT > 1
[loop]
for (uint pcf = 0; pcf < PCF_SAMPLE_COUNT; pcf++)
#else
uint pcf = 0;
#endif
{
float SampledDepth = tex2D(ShadowMap, LightTexCoord + (PCF_SAMPLE_ARRAY[pcf] * ShadowMapTexelSize.xy)).r;
if (fCurrDepth - ShadowBias > SampledDepth)
Shadow += 1.0f;
}
Shadow = 1.0f - (Shadow / PCF_SAMPLE_COUNT);
LightResult *= Shadow;
}
#endif
Color.rgb *= LightResult;
Color.rgb += (AmbientColor.rgb * AmbientColor.a);
return Color;
}
hope someone can help me :(
sorry for poor english