I calculate shadow factor according to the instructions:
float CalcShadowFactor(SamplerComparisonState samShadow,
TextureCube shadowMap,
float3 shadowPosH)
{
float3 l=float3(4.0f,2.0f,8.0f);
float3 d=l-shadowPosH;
float dis=length(d);
d=normalize(d);
float percentLit = 0.0f;
[unroll]
percentLit += shadowMap.SampleCmpLevelZero(samShadow,
d,dis).r;
return percentLit;
}
It simply dont work, it projects shadows to wrong places, Am I doing something wrong?
l-light world position
shadowPosH-world position calculated by mul(float4(vin.PosL, 1.0f), gWorld).xyz
If all shader code is necessery:
#include "LightHelper.fx"
cbuffer cbPerFrame
{
PointLight gDirLights;
float3 gEyePosW;
float gFogStart;
float gFogRange;
float4 gFogColor;
};
cbuffer cbPerObject
{
float4x4 gWorld;
float4x4 gWorldInvTranspose;
float4x4 gWorldViewProj;
float4x4 gTexTransform;
float4x4 gShadowTransform;
Material gMaterial;
};
Texture2D gDiffuseMap;
TextureCube gShadowMap;
TextureCube gCubeMap;
SamplerState samLinear
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
SamplerComparisonState samShadow
{
Filter = COMPARISON_MIN_MAG_MIP_LINEAR;
AddressU = BORDER;
AddressV = BORDER;
AddressW = BORDER;
BorderColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
ComparisonFunc = LESS_EQUAL;
};
struct VertexIn
{
float3 PosL : POSITION;
float3 NormalL : NORMAL;
float2 Tex : TEXCOORD;
};
struct VertexOut
{
float4 PosH : SV_POSITION;
float3 PosL: POSITION0;
float3 PosW : POSITION1;
float3 NormalW : NORMAL;
float2 Tex : TEXCOORD0;
float3 ShadowPosH : TEXCOORD1;
};
VertexOut VS(VertexIn vin)
{
VertexOut vout;
vout.PosW = mul(float4(vin.PosL, 1.0f), gWorld).xyz;
vout.NormalW = mul(vin.NormalL, (float3x3)gWorldInvTranspose);
vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);
vout.Tex = mul(float4(vin.Tex, 0.0f, 1.0f), gTexTransform).xy;
vout.ShadowPosH = vout.PosW;
vout.PosL=vin.PosL;
return vout;
}
float4 PS(VertexOut pin,
uniform int gLightCount,
uniform bool gUseTexure,
uniform bool gAlphaClip,
uniform bool gFogEnabled,
uniform bool gReflectionEnabled) : SV_Target
{
pin.NormalW = normalize(pin.NormalW);
float3 toEye = gEyePosW - pin.PosW;
float3 toEyeW = normalize(gEyePosW - pin.PosW);
float distToEye = length(toEye);
toEye /= distToEye;
float4 texColor = float4(1, 1, 1, 1);
if(gUseTexure)
{
// Próbkuj tekstur?.
texColor = gDiffuseMap.Sample( samLinear, pin.Tex );
if(gAlphaClip)
{
clip(texColor.a - 0.1f);
}
}
float4 litColor = texColor;
if( gLightCount > 0 )
{
float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 spec = float4(0.0f, 0.0f, 0.0f, 0.0f);
float3 shadow = float3(1.0f, 1.0f, 1.0f);
shadow[0] = CalcShadowFactor(samShadow, gShadowMap, pin.ShadowPosH);
// Sumuj udzia? ?wiat?a z ka?dego ?ród?a.
[unroll]
float4 A, D, S;
ComputePointLight(gMaterial, gDirLights, pin.PosW, pin.NormalW, toEyeW, A, D, S);
ambient += A;
diffuse += shadow[0]*D;
spec += shadow[0]*S;
litColor = texColor*(ambient + diffuse) + spec;
if( gReflectionEnabled )
{
float3 incident = -toEye;
float3 reflectionVector = reflect(incident, pin.NormalW);
float4 reflectionColor = gCubeMap.Sample(samLinear, reflectionVector);
litColor += gMaterial.Reflect*reflectionColor;
}
}
if( gFogEnabled )
{
float fogLerp = saturate( (distToEye - gFogStart) / gFogRange );
litColor = lerp(litColor, gFogColor, fogLerp);
}
litColor.a = gMaterial.Diffuse.a * texColor.a;
return litColor;
}
//techniques bla bla bla