Hi,
I'm trying to get work shadow mapping - exactly rendering light map with shadows and come across following problem:
I have D3DFMT_R32F texture for rendering depths into it. I render depth into R32F texture as render target using shader and then set it as texture to read from it in next pass. But suddenly the value is not here and what I get from every read is 0. And it should be something around one or precisely one.
My code is:
The body from pixel shader writing value to texture:
OUTPUT Main(INPUT Input)
{
OUTPUT Output = (OUTPUT) 0;
Output.Color = Input.Color.x; // writing to red channel of texture
return Output;
}
The body from pixel shader reading value from texture:
sampler TexSamp2: register(s2);
struct LIGHTMAP_PS_INPUT
{
float4 Light: COLOR0; // Color
float3 TexCoord: TEXCOORD0; // first two are tex coords and third is compare depth value
};
float4 LightMapPS(LIGHTMAP_PS_INPUT Input): COLOR0
{
float4 TexCol;
float Shadow;
// Get shadow map depth to x
TexCol = tex2D(TexSamp2, Input.TexCoord.xy);
// compare
Shadow = (TexCol.r >= Input.TexCoord.z);
return Input.Light * Shadow;
};
My code is:
// Render shadow map texture
ShadowMap->GetDynShadowTex()->GetSurfaceLevel(0, &MapSurface);
LightMapView.X = 0;
LightMapView.Y = 0;
LightMapView.Width = ShadowMap->GetLightMapSize();
LightMapView.Height = ShadowMap->GetLightMapSize();
LightMapView.MinZ = 0;
LightMapView.MaxZ = 1;
// Set render target and clear it
D3DDevice->SetRenderTarget(0, MapSurface);
D3DDevice->SetViewport(&LightMapView);
D3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255,255, 255, 255), 1, 0);
SetVertexShader(DepthVS);
SetPixelShader(DepthPS);
// apply bias to view space
Shader->SetViewMatrix(LightViewMat * ShadowMap->GetBiasMat() );
Shader->SetProjMatrix(LightProjMat);
// FIRST PASS - render z-depth map
D3DDevice->BeginScene();
// Render plate and light stand
Plate->Render();
Light->pEmitor->Render();
D3DDevice->EndScene();
SAFE_RELEASE(MapSurface);
// Render shadow map texture
LightMap->GetSurfaceLevel(0, &MapSurface);
D3DDevice->SetRenderTarget(0, MapSurface);
D3DDevice->Clear(0,0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,0, 255), 1, 0);
SetVertexShader(LightMapVS);
SetPixelShader(LightMapPS);
// Set depth texture
D3DDevice->SetTexture(2, ShadowMap->GetDynShadowTex() );
Shader->SetViewMatrix(LightViewMat);
Shader->SetProjMatrix(LightProjMat);
//D3DDevice->SetTransform(D3DTS_VIEW, &LightViewMat);
//D3DDevice->SetTransform(D3DTS_PROJECTION, &LightProjMat);
ShadowMat = LightProjMat * ShadowMap->GetCorrectMat();
Shader->SetVSMatrix(ShadowMatHandle, ShadowMat);
// Set spot light
Light->GetD3DLight(&SpotLight);
Shader->SetVSSingleSpotLight(SpotLight);
// SECOND PASS - render light map with shadows
D3DDevice->BeginScene();
// Render plate
Plate->Render();
D3DDevice->EndScene();
SAFE_RELEASE(MapSurface);
I checked, that correct value is written to texture.
I checked, that correct coords are given to texture read.
Next strange thing is, when I render using REF device, I get
completely black light map, but when I render with HAL
I get circle of light in the center with some z-fight problem, which is not changing when I add or substract any value of bias as it is typical in shadow mapping.
Does anybody know, what it can be caused by?
I'm out of ideas.
I'll provide any other info you want to know, just ask.
I'm running it on ATI Radeon 9700 Pro. DirectX 9.0c - October 2004 update.
Thanks everyone willing me to help.
Pavel Celba
[Edited by - Coder on November 14, 2004 10:30:56 PM]