Deferred shadowmapping

Started by
5 comments, last by _Silence_ 5 years, 3 months ago

Hello. I am writing a simple shadowmapping algorithm with one directional light. I understand the basics but I can't make it work. Can you look at my code snippet and tell me what am I doing wrong?

Shadowmap is correct (1024x1024, one directional light looking at (0,0,0) from (0,10,0)).


float3 PositionFromDepth(in float depth, in float2 pixelCoord, float aspectRatio, float4x4 invView, float4x4 invProj)
{
	float2 cpos = (pixelCoord + 0.5f) * aspectRatio;
	cpos *= 2.0f;
	cpos -= 1.0f;
	cpos.y *= -1.0f;
	float4 positionWS = mul(float4(cpos, depth, 1.0f), invProj);
	positionWS /= positionWS.w;
	positionWS = mul(positionWS, invView);
	return positionWS.xyz;
}

float GetShadow(float3 posWS)
{
	float4 lpos = mul(float4(posWS, 1), gLightViewProj);

	//re-homogenize position after interpolation
	lpos.xyz /= lpos.w;

	//if position is not visible to the light - dont illuminate it
	//results in hard light frustum
	if (lpos.x < -1.0f || lpos.x > 1.0f ||
		lpos.y < -1.0f || lpos.y > 1.0f ||
		lpos.z < 0.0f  || lpos.z > 1.0f) return 0.0f;

	//transform clip space coords to texture space coords (-1:1 to 0:1)
	lpos.x = lpos.x / 2.0f + 0.5f;
	lpos.y = -lpos.y / 2.0f + 0.5f;

	//sample shadow map - point sampler
	float shadowMapDepth = ShadowMap.Sample(Sampler, lpos.xy).r;

	const float bias = 0.001f;
	if (shadowMapDepth < lpos.z - bias) return 0.0f;

	return 1.0f;
}

float4 ps_main(PixelShaderInput pin) : SV_Target
{
	float2 pixelCoord = pin.texcoord.xy;

	float depth = Depth.SampleLevel(Sampler, pixelCoord, 0).x;

	// Outgoing light direction (vector from world-space pixel position to the "eye").
	float3 posWS = PositionFromDepth(depth, pixelCoord, gScreenDim.w, gInvView, gInvProj);
	float shadow = GetShadow(posWS);
	return float4(shadow.xxx, 1);
}

The results I get are on the attached image. To me it looks like world space position extracted from depth is not really world space position but I don't know if that's the case.

I'll appreciate any kind of help on this.

 

shadowmapping.png

Advertisement
10 hours ago, Vincent Chase said:

Shadowmap is correct (1024x1024, one directional light looking at (0,0,0) from (0,10,0)).

Do you mean that when looking at your scene with the same view and projection, you are seeing your full scene correctly ?

Because at first glance your image looks like as if your light view is not properly set.

I was right as it comes to world space position. It was wrong because of this line:


float2 cpos = (pixelCoord + 0.5f) * aspectRatio;

First of all pixelCoord is already in [0,1] range so adding 0.5 to it made no sense. After changing it to:


float2 cpos = pixelCoord;

world position is fine. The shadow map still looks broken though. Do you have any further hints on this? I don't like this self shadowing thing:

 

shadowmapping.png

On you first post, you wrote:

On 1/17/2019 at 11:20 PM, Vincent Chase said:

I am writing a simple shadowmapping algorithm with one directional light

But looking at your shadows you don't look to apply any orthographic projection. It looks like if your light is coming from a point at the long of the blue line, at the top of your screen. Try first to fix this. Ensure that your scene if visible from your light point of view. Your cone has no shadows, this is also very weird.

Put some ambient term, this will allow to see better how your shadows are looking.

Finally, do you follow any tutorials ?

I've fixed the ortographic projection for the light camera and now shadows look a litle better. There's still one artifact left that I don't know how to solve. Any idea why it happens? The shadow map cuts the plane for some reason and I don't think it has anything to do with far/near planes.

shadowmapping.png

shadowmap.png

36 minutes ago, Vincent Chase said:

The shadow map cuts the plane for some reason and I don't think it has anything to do with far/near planes.

Normally this should be that. Test for shadows only if the fragment is not behind your near plane.

This topic is closed to new replies.

Advertisement