Hello everyone,
Ok, well I have successfully uh, correction partially done Parallel Split Variance shadow map. I have got the split working with transition between the shadows. However I am having a problem like this:
You can see that in the above image the palm's shadow appear 2 times. The shadow which is on the bottom of the scrren should not be there. Also it moves as my camera moves. Other shadow(the correct one) is stable.
What could be the problem?? Also the game uses deferred rendering..
Here is the code
DrawShadowOcclusionMap function:
And here is the shader code:
It appears that the shadow map of the secons plit is again drawn which is the problem. Because if I allow the first split to cover the whole are then the problem is gone.
Please help. Thanks.
Ok, well I have successfully uh, correction partially done Parallel Split Variance shadow map. I have got the split working with transition between the shadows. However I am having a problem like this:
You can see that in the above image the palm's shadow appear 2 times. The shadow which is on the bottom of the scrren should not be there. Also it moves as my camera moves. Other shadow(the correct one) is stable.
What could be the problem?? Also the game uses deferred rendering..
Here is the code
Code:
ClearTexture(materialMap, device, 0xFFFFFFFF);
ClearTexture(normalMap, device, 0xFFFFFFFF);
ClearTexture(positionXYMap, device, 0xFFFFFFFF);
ClearTexture(shadowOcclusionMap, device, 0xFFFFFFFF);
ClearTexture(positionZMap, device, 0xFFFFFFFF);
LPDIRECT3DSURFACE9 bb;
device->GetRenderTarget(0, &bb);
SetRenderTarget(0, materialMap, device);
SetRenderTarget(1, normalMap, device);
SetRenderTarget(2, positionXYMap, device);
DrawDefferedGeometry(device, V, projectionMatrix);
device->SetRenderTarget(0, bb);
device->SetRenderTarget(1, NULL);
device->SetRenderTarget(2, NULL);
//device->GetDepthStencilSurface(&oldDepthStencilSurface);
//device->SetDepthStencilSurface(depthStencilSurface);
D3DVIEWPORT9 oldViewPort;
SetRenderTarget(0, positionZMap, device);
for(int splitCount = 0 ; splitCount < _iNumSplits ; splitCount++)
{
float fNear = _pSplitDistances[splitCount];
float fFar = _pSplitDistances[splitCount + 1];
float fScale=1.1f;
D3DXVECTOR3 pCorners[8];
CalculateFrustumCorners(pCorners, TheCamera.cameraPosition, TheCamera.cameraPosition+TheCamera.look,
TheCamera.up, fNear,fFar,45,w/h,fScale, V);
CalculateLightForFrustum(pCorners);
lightVM[splitCount] = lightViewMatrix;
lightPM[splitCount] = lightProjectionMatrix;
D3DVIEWPORT9 CameraViewport;
device->GetViewport(&CameraViewport);
CameraViewport.MinZ=splitCount/(float)_iNumSplits;
CameraViewport.MaxZ=(splitCount+1)/(float)_iNumSplits;
CameraViewport.Width = 1024;
CameraViewport.Height = 1024;
device->SetViewport(&CameraViewport);
DrawShadowedGeometry(device);
}
device->SetViewport(&oldViewPort);
device->Clear(0, 0,D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.f, 0);
//device->SetDepthStencilSurface(oldDepthStencilSurface);
SetRenderTarget(0, shadowOcclusionMap, device);
DrawShadowOcclusionMap(device, V);
device->SetRenderTarget(0, bb);
//Draw Full Screen Quad
//....
//....
Code:
void DrawShadowOcclusionMap(LPDIRECT3DDEVICE9 device, D3DXMATRIX V)
{
static float fWidth = params.BackBufferWidth;
static float fHeight = params.BackBufferHeight;
static PlaneVertex axPlaneVertices[] =
{
{ 0, 0, .5f, 1, 0 + .5f / fWidth, 0 + .5f / fHeight },
{ fWidth, 0, .5f, 1, 1 + .5f / fWidth, 0 + .5f / fHeight },
{ fWidth, fHeight, .5f, 1, 1 + .5f / fWidth, 1 + .5f / fHeight },
{ 0, fHeight, .5f, 1, 0 + .5f / fWidth, 1 + .5f / fHeight }
}; D3DXVECTOR4 lightClipPlanes[_iNumSplits]; for(int splitCount = 0 ; splitCount < _iNumSplits ; splitCount++)
{
lightClipPlanes[splitCount].x = _pSplitDistances[splitCount];
lightClipPlanes[splitCount].y = _pSplitDistances[splitCount + 1];
}
D3DXMATRIX inverseView;
D3DXMatrixInverse(&inverseView, NULL, &V);
effect->SetTechnique("ShadowTech");
effect->SetMatrixArray("lightView", lightVM, _iNumSplits);
effect->SetMatrixArray("lightProjection", lightPM, _iNumSplits);
effect->SetVectorArray("g_vClipPlanes", lightClipPlanes, _iNumSplits);
effect->SetTexture("scenePositionXYMap", positionXYMap);
effect->SetTexture("shadowMap", positionZMap);
effect->SetTexture("sceneMaterialMap", materialMap);
effect->SetMatrix("inverseView", &inverseView);
effect->SetValue("farFrustumCoords", &farFrustumCoords, sizeof(D3DXVECTOR3) * 4);
D3DXMATRIX TexProj;
float tex_offset = 0.5f + (0.5f / 1024);
TexProj = D3DXMATRIX( 0.5f, 0.0f, 0.0, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
tex_offset, tex_offset, 0.0f, 1.0f);
effect->SetMatrix("TexProj", &TexProj);
UINT numPasses;
effect->Begin(&numPasses, 0);
effect->BeginPass(0);
effect->CommitChanges();
device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, axPlaneVertices, sizeof(PlaneVertex));
effect->EndPass();
effect->End();
}
Code:
float4x4 world;
float4x4 view;
float4x4 projection;
float3 eyePos;
bool lod;
bool leaves;
float Time;
bool tree;
bool light;
float4x4 InverseTransposeView;
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 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 = outVS.vPos;
outVS.vTex0 = tex0;
outVS.vWorldNrm = normalize(mul(float4(normalL, 0), (float3x3) InverseTransposeView));
return outVS;
}
//-----------------------------------------------------------------------------
// Pixel shader.
//-----------------------------------------------------------------------------
OutputPS PS_Lambert(OutputVS i) : COLOR
{
OutputPS o;
float distance1 = distance(i.eyeVec, float3(0, 0, 0));
float3 texColor = tex2D(colorMapSampler, i.vTex0);
distance1 /= 460;
//clip(texColor - distance1/2);
/*if(!lod)
{
if(!tree)
distance1 /= 330;
else
distance1 /= 460;
}
if(!lod)
{
if(!leaves)
{
clip(texColor - distance1);
}
else
{
clip(texColor - distance1/2);
}
}
else
{
if(tree)
clip(texColor - 28/distance1);
}*/
//clip(texColor.a - 0.2f);
o.vMaterial.rgb = texColor;
o.vMaterial.a = tex2D(colorMapSampler, i.vTex0).a;
o.vWorldNrm.xyz = 0.5f * (i.vWorldNrm + 1.0f);
o.vWorldNrm.w = 0.0;
o.vWorldPosXY = float4(i.vWorldPos.xyz, 0);
return o;
}
technique Lambert
{
pass
{
VertexShader = compile vs_3_0 VS_Lambert();
PixelShader = compile ps_3_0 PS_Lambert();
AlphaTestEnable = true;
AlphaFunc = GreaterEqual;
AlphaRef = 150;
CullMode = None;
}
}
Please help. Thanks.