Jump to content
  • Advertisement
Sign in to follow this  
AmidaBucu

deferred shadow mapping

This topic is 3653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I try to put a shadowmap process to my "engine". (That uses Deferred Rendering) First, I create the matrices of the Light. Second, I save the Depth in The World*LView*Lprojection space.(It's surely working!) Third, I save the necessary stuffs from the scene (depths, normals). Fourth, I draw the scene using the textures where to I save the datas. Fifth, I get a weird result. If I may, I put my shaders here. (Sorry ahead)
//ShadowBuild Pass

float4x4 W;
float4x4 LV;
float4x4 LP;

struct VS_OUT
{
	float4 pos : POSITION;
	float2 depth : TEXCOORD0;
};

VS_OUT VS(float3 in_pos : POSITION)
{
	VS_OUT OUT = (VS_OUT)0;
	OUT.pos = mul(float4(in_pos,1), W);
	OUT.pos = mul(OUT.pos, LV);
	OUT.pos = mul(OUT.pos, LP);
	
	OUT.depth.x = OUT.pos.z;
	OUT.depth.y = OUT.pos.w;	
	return OUT;	
}

float4 PS(VS_OUT IN) : COLOR
{
	float4 color = (float4)0;
	color = IN.depth.x/IN.depth.y;
	return color;
}

technique shadowBuild
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VS();
        PixelShader = compile ps_2_0 PS();
    }
}

// SceneBuild Pass

float4x4 W;
float4x4 V;
float4x4 P;
texture DiffuseMap;
texture NormalMap;
texture SpecularMap;
bool normaled;
bool specular;


sampler DiffuseSampler = sampler_state
{
	Texture = <DiffuseMap>;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	MipFilter = LINEAR;
};

sampler NormalSampler = sampler_state
{
	Texture = <NormalMap>;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	MipFilter = LINEAR;
};

sampler SpecularSampler = sampler_state
{
	Texture = <SpecularMap>;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	MipFilter = LINEAR;
};

struct VS_INPUT
{
    float4  pos            : POSITION;
    float2  tex           : TEXCOORD0;
    float3  tangent            : TANGENT;
    float3  binormal            : BINORMAL;
    float3  normal            : NORMAL;
};

struct VS_OUTPUT
{
	float4	pos			: POSITION;
    float2  tex           : TEXCOORD0;
	float2	depth		: TEXCOORD1;
	float3x3 TBN		: TEXCOORD2;
};

struct PS_OUTPUT
{
	float4	mat		: COLOR0;
	float4	normal		: COLOR1;
	float4	depth		: COLOR2;
};

VS_OUTPUT VS(VS_INPUT IN)
{
	VS_OUTPUT OUT;

	OUT.pos = mul(IN.pos, W);	
	OUT.pos = mul(OUT.pos, V);
	OUT.pos = mul(OUT.pos, P);
	OUT.tex = IN.tex;
	OUT.TBN = float3x3(
				mul(float4(IN.tangent,0), W).xyz, 
				mul(float4(IN.binormal,0),W).xyz, 
				mul(float4(IN.normal,0),W).xyz);
		
	OUT.depth.x = OUT.pos.z;
	OUT.depth.y = OUT.pos.w;
	return OUT;
};

PS_OUTPUT PS(VS_OUTPUT IN)
{
	PS_OUTPUT OUT = (PS_OUTPUT)0;

	OUT.mat.rgb = tex2D(DiffuseSampler, IN.tex);
	OUT.mat.a = 1.0;
	float4 normal;
	if(normaled)
	{
		normal.xyz = (2*tex2D(NormalSampler, IN.tex)-1).rgb;
		float3x3 ITBN = transpose(IN.TBN);
		normal.xyz = mul(ITBN, normalize(normal.xyz));
	}
	else
	{
		normal.xyz = (IN.TBN[2]);
	}
	
	normal.w = 0.0;
	normal = mul(normal,V);
	OUT.normal.r = normal.x;
	OUT.normal.g = normal.y;//G16R16F-ba nyomom
	OUT.depth = IN.depth.x/IN.depth.y;
		
	return OUT;
};

technique Build
{
	pass p0
	{
		VertexShader = compile vs_1_1 VS();
		PixelShader = compile ps_2_0 PS();
	}
}

//Final Pass

float4x4 W;
float4x4 V;
float4x4 P;
float4x4 IVP;
float4x4 IV;
float4x4 LV;
float4x4 LP;
texture tex;
texture normal;
texture depth;
texture shadowmap;
float3 lPos;
float3 cPos;
float4 lDiff;
float3 lDir;
float lRad;
float2 halfpixel;

sampler difftex = sampler_state
{
	Texture = <tex>;
	MinFilter = ANISOTROPIC;
	MipFilter = ANISOTROPIC;
	MagFilter = ANISOTROPIC;
};

sampler normaltex = sampler_state
{
	Texture = <normal>;
	MinFilter = ANISOTROPIC;
	MipFilter = ANISOTROPIC;
	MagFilter = ANISOTROPIC;
};

sampler depthtex = sampler_state
{
	Texture = <depth>;
	MinFilter = ANISOTROPIC;
	MipFilter = ANISOTROPIC;
	MagFilter = ANISOTROPIC;
};

sampler shadowtex = sampler_state
{
	Texture = <shadowmap>;
	MinFilter = ANISOTROPIC;
	MipFilter = ANISOTROPIC;
	MagFilter = ANISOTROPIC;
};

struct VS_INPUT
{
	float4 pos : POSITION;
    float2 tex : TEXCOORD0;	
};

struct VS_OUTPUT
{
	float4 pos : POSITION;
	float2 tex : TEXCOORD0;
};

VS_OUTPUT VS(VS_INPUT IN)
{
	VS_OUTPUT Output;
	Output.pos = mul(IN.pos, W);
	Output.pos = mul(Output.pos, V);
	Output.pos = mul(Output.pos, P);
	Output.tex = IN.tex;
	return Output;
}

float4 PS(VS_OUTPUT IN) : COLOR0
{ 
	IN.tex -= halfpixel;
	float3 normal;
	normal.xy = tex2D(normaltex, IN.tex.xy);
	normal.z = -sqrt(1 - dot(normal.xy, normal.xy));
	normal = mul(float4(normal,0), IV).xyz;
	normal = normalize(normal);
	float4 diff = tex2D(difftex, IN.tex.xy);
	float depth = tex2D(depthtex, IN.tex.xy).r;
	
	float4 pos;
	pos.x = (IN.tex.x-0.5)*2;
	pos.y = -((IN.tex.y-0.5)*2);
	pos.z = depth;
	pos.w = 1;
	
	pos = mul(pos, IVP);
	pos /= pos.w;
	
	float4 lpos = mul(mul(pos,LV),LP);
	lpos /= lpos.w;
	float2 sTexC = float2((1+lpos.x)/2,-(1+lpos.y)/2);
		
	float c;
	
	float SDepth = tex2D(shadowtex, sTexC).r;
	
	if(SDepth >= lpos.z) c = 1;
	else c = 0;	
		
		
	float3 tL = normalize(lPos-pos.xyz);
	float3 tC = normalize(cPos-pos.xyz);
	
	float att = 1-saturate(distance(pos.xyz, lPos)/lRad);
	att *= c;
	float3 h = normalize(tC+tL);
	float d = max(dot(normal, tL),0);
	float s = (d > 0)?pow(dot(h, normal), 10):0;
	float4 color = (diff*(d+s))*att;
    return color;
}

technique Light
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VS();
        PixelShader = compile ps_2_0 PS();
    }
}
What's wrong here?

Share this post


Link to post
Share on other sites
Advertisement
This line:

float2 sTexC = float2((1+lpos.x)/2,-(1+lpos.y)/2);

Should be this instead:

float2 sTexC = float2((1+lpos.x)/2,1-(1+lpos.y)/2);

Otherwise your V texture coordinate will be negative.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!