Light map generation

So I am going mental trying to sort this out

Lights work, shadows don’t

I have gone through the code line by line and cannot see anything wrong, so there must be something fundamentally wrong with my technique

My approach is this

  1. Create a rendertarget for each object instance
  2. for each light
    Create a rendertarget of type single
    Render scene into rendertarget using a depth only shader
  3. for each object
    Render into the objects rendertarget using additive blending
    Calculate the lighting contribution from the light
    Check the depth buffer for shadowing

after a lot of testing I know this function does not work

float CalcShadowTermPCF(float light_space_depth, float ndotl, float2 shadow_coord)
float variableBias = clamp(0.001 * tan(acos(ndotl)), 0, DepthBias);
 	light_space_depth -= variableBias;

	float size = 1.0 / 1024.0;
float samples = 0;

if (light_space_depth < tex2D(ShadowSampler, shadow_coord).r)
	samples = 0.25;
if (light_space_depth < tex2D(ShadowSampler, shadow_coord + float2(size, 0)).r)
	samples += 0.25;
if (light_space_depth < tex2D(ShadowSampler, shadow_coord + float2(0, size)).r)
	samples += 0.25;
if (light_space_depth < tex2D(ShadowSampler, shadow_coord + float2(size, size)).r)
	samples += 0.25;

return samples;


But I have no idea why

I know the depth buffer is good, I have saved them to disk to check.

I am using linear depth and in the code to save a depth buffer I print the value range for the depth values , which is correct

I have checked all parameters are getting set correctly, the light view projection matrix is correct

I have made sure all the cone angles are correct and fit within the depth buffers field of view

I am out of ideas

Anyone see anything stupid I am doing?


spot the bug

float4 lightingPosition = mul(input.WorldPos, LightViewProj);


took me far too long

float4 lightingPosition = mul(float4(input.WorldPos,1), LightViewProj);

I wasn’t happy with the above scene. I have improved it since that screen shot, but I am still not happy with it.

So I decided to do a test.

I recreated the same scene in Unity and UE4

The results were stunning.

Unity… failed. I could only get one spotlight to work. As soon as I added another, everything fell apart

UE4 much better, but still wrong.

The shadows and spot lights are great, but the walls are all wrong. Look at them!

So I think I just need to continue to work on my code adding more lighting terms