Shadow mapping strangeness

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
Post Reply
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Shadow mapping strangeness

Post by Lonesome Ducky »

I've been working on some shadow mapping (copied from rendermonkey), and it seemed to be working great. But on closer inspection, there are small circles in the shadows that are lit when they should be unlit and vice versa. Here is a screenshot illustrating my point:Image As you can see, there are a few spots where shadows are in correct, and I have no idea why it is. Here is the code for my shaders:
Shadow Map:

Code: Select all

float4x4 LightsWorldViewProjection;
float4x4 WorldViewProjection;
float4x4 World;
float3 LightPos;
float distanceScale = 0.4;

struct VS_OUTPUT {
   float4 Pos: POSITION;
   float3 lightVec: TEXCOORD0;
};

VS_OUTPUT vertexMain(float4 Pos: POSITION){
   VS_OUTPUT Out;

   float4 pos;

   Out.Pos = mul(Pos,LightsWorldViewProjection);
   Pos.xyz -= LightPos; 
   Out.lightVec = distanceScale * Pos;

   return Out;
}

float4 pixelMain(float3 lightVec: TEXCOORD0) : COLOR {
   // Output radial distance
   float4 output = 0;
   output = length(lightVec);
   return output;
}

Drawing of object with shadow:

Code: Select all

float4x4 LightsWorldViewProjection;
float4x4 WorldViewProjection;
float3 LightPos;
float Ambient;
float distanceScale = 0.4;

sampler2D tex0;
sampler2D tex1;


struct VS_OUTPUT {
   float4 Pos:       POSITION;
   float2 texCoord: TEXCOORD0;
   float3 normal:    TEXCOORD1;
   float3 lightVec : TEXCOORD2;
   float4 shadowCrd: TEXCOORD3;
};

VS_OUTPUT vertexMain(float2 texCoord: TEXCOORD0, float4 Pos: POSITION, float3 normal: NORMAL){
   VS_OUTPUT Out;

   Out.Pos = mul( Pos, WorldViewProjection);
   Out.normal = normal;
   Out.lightVec = distanceScale * ( Pos.xyz-LightPos );
   Out.texCoord = texCoord;
   Pos.xyz -= LightPos; 
   float4 sPos = mul( Pos, LightsWorldViewProjection);



   Out.shadowCrd.x = 0.5 * (sPos.z + sPos.x);
   Out.shadowCrd.y = 0.5 * (sPos.z - sPos.y);
   Out.shadowCrd.z = 0;
   Out.shadowCrd.w = sPos.z;

   return Out;
}

float4 pixelMain(float2 texCoord : TEXCOORD0, float3 normal: TEXCOORD1, float3 lightVec: TEXCOORD2, float4 shadowCrd: TEXCOORD3) : COLOR {
   normal = normalize(normal);

   float depth = length(lightVec);

   float4 base = tex2D(tex0, texCoord);
   float shadowMap = tex2Dproj(tex1, shadowCrd).r;
  
   float shadow = (depth < shadowMap);

   return shadow*base+base*Ambient;
}
netpipe
Posts: 670
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Post by netpipe »

if you post a more complete project people would be willing to tinker with it more. upload to www.xup.in
Live long and phosphor!
-- https://github.com/netpipe/Luna Game Engine Status 95%
Post Reply