I implemented planar shadows with the function D3DXMatrixShadow.
As you know, we need plane and light factor to calculate a shadow matrix.
The problem is that when I set the plane as D3DXPLANE p(0, -1, 0, 0.1f), the shadows by directional light are correctly rendered, but the shadows by point light are not rendered.
However, if I use D3DXPLANE p(0, 1, 0, 0.1f), the situation is reversed, shadows by directional light are not drawn, the shadows by point light are ok.
I cannot understand why it happens. Is it normal or am i missing something? Please explain to me why this happens.
Thanks in advance.
Code
//main.cpp
ID3D10EffectTechnique* oldTech = meshes[i]->technique;
D3DXPLANE groundPlane(0, -1, 0, 0.01f);
D3DXMATRIX S;//shadow matrix
D3DXVECTOR4 light;
if(lightType != 2) //2 here means point light
{
light.x = lightDirection.x;
light.y = lightDirection.y;
light.z = lightDirection.z;
light.w = 0;
}
else
{
light.x = lightPosition.x;
light.y = lightPosition.y;
light.z = lightPosition.z;
light.w = 1;
groundPlane.b = 1;
}
D3DXMatrixShadow(&S, &light, &groundPlane);
D3DXMATRIX W = meshes[i]->GetWorldMatrix();
D3DXMATRIX matWorld = W * S;
meshes[i]->technique = shadowTechnique;
meshes[i]->render(&matWorld);
//Meshes here are drawn by ID3DX10Mesh,
//but I tested other code with LPD3DXMESH, it has the same problem.
meshes[i]->technique = oldTech;
//shader code(.fx file)
PS_Input VS_Shadow(VS_Input input)
{
PS_Input output = (PS_Input)0;
output.pos = mul(float4(input.pos, 1), matWorld);
output.pos.y = 0.1;
output.pos = mul(mul(output.pos, matView), matProjection);
output.normal = mul( input.normal, (float3x3)matWorld );
output.color = input.color;
output.worldPos = (float3)mul(input.pos, matWorld);
output.tex = input.tex;
return output;
}
float4 PS_Shadow( PS_Input input) : SV_Target
{
return float4(0,0,0,0.5f);
}
DepthStencilState StencilIncreaseOnce {
DepthEnable = true;
DepthWriteMask = ALL;
DepthFunc = LESS_EQUAL;
StencilEnable = true;
FrontFaceStencilFunc = EQUAL;
FrontFaceStencilPass = INCR;
FrontFaceStencilFail = Keep;
};
BlendState BlendAlpha {
AlphaToCoverageEnable = FALSE;
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA ;
DestBlend = INV_SRC_ALPHA ;
BlendOp = ADD;
SrcBlendAlpha = ONE;
DestBlendAlpha = ZERO;
BlendOpAlpha = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
technique10 ShadowRender
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS_Shadow() ) );
//SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS_Shadow() ) );
SetDepthStencilState( StencilIncreaseOnce, 0x0 );
SetBlendState( BlendAlpha, float4( 0.0f, 0.0f, 0.0f, 0.0f ),
0xFFFFFFFF ); //blendState, blendFactor, sampleMask
}
}