Jump to content
  • Advertisement
Sign in to follow this  
AntiMoron

problem about `Shadow Map`

This topic is 1417 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

After my last question.Here comes the second.

 

It 's about shadow map.

 

I've known the math of it.

Here's the shader I used.

Texture2D txDiffuse : register(t0);
Texture2D ProTexutre : register(t1);
SamplerState samLinear : register( s0 );

cbuffer cameraMatrices : register( b0 )
{
    matrix World;
    matrix View;
    matrix Projection;
};

cbuffer PRS : register(b1)
{
    matrix Pos;
    matrix Rot;
    matrix Sca;
}

cbuffer LightMatrices : register(b2)
{
    matrix LightView;
    matrix LightProjection;
}


struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
	float4 viewPosition : TEXCOORED1;
};

PS_INPUT VSEntry( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;

    output.Pos = float4(input.Pos.xyz,1.0f);
    output.Pos = mul( output.Pos, Rot);
    output.Pos = mul( output.Pos, Sca);
    output.Pos = mul( output.Pos, Pos);
    output.Pos = mul( output.Pos, World );

    output.viewPosition = output.Pos;
    output.viewPosition = mul(output.viewPosition, LightView);
    output.viewPosition = mul(output.viewPosition, LightProjection);

	output.Pos = mul(output.Pos, View);
	output.Pos = mul(output.Pos, Projection);

    output.Tex = input.Tex;    
    return output;
}

float4 PSEntry(PS_INPUT input) : SV_Target
{
	float bias = 0.0000001f;
	float4 color = txDiffuse.Sample(samLinear, input.Tex);
	clip(color.a == 0.0f ? -1 : 1);

	float2 projectTexCoord;

	projectTexCoord.x =  input.viewPosition.x / input.viewPosition.w / 2.0f + 0.5f;
        projectTexCoord.y = -input.viewPosition.y / input.viewPosition.w / 2.0f + 0.5f;
	if((saturate(projectTexCoord.x) == projectTexCoord.x) 
		&& (saturate(projectTexCoord.y) == projectTexCoord.y))
	{
		float lightdepth = input.viewPosition.z / input.viewPosition.w;
		lightdepth = lightdepth - bias;
		float cameradepth = input.Pos.z / input.Pos.w;
		float4 depthColor = float4(lightdepth, lightdepth, lightdepth, 1.0f);
		if (lightdepth < cameradepth)
			color = depthColor;
	}
	return color;
}
 

1.Do a projection texturing(which I've implemented) from light position.

2.Compare the depth value from camera position and depth value from light position view.

    if  cameraDepthValue is larger than lightDepthValue which means the scene at that pixel is lighted then return lighted color.

    Otherwise return shadow color.. 

 

 

But in my project it's not working at all.

I don't know what am I doing wrong.

 

Some ideas please?

The whole project is here : https://github.com/AntiMoron/aMazing  .

Really Iooking forward to your help.

Edited by AntiMoron

Share this post


Link to post
Share on other sites
Advertisement

What do you mean it isn't working? No lights or no shadows?

 

In this situation I usually output intermediate values in the shader to make sure they are what I expect them to be. For example, instead of outputting the light color, output the distance each pixel is to the light. I found that to be the best way to debug a shader.

Share this post


Link to post
Share on other sites

What do you mean it isn't working? No lights or no shadows?

 

In this situation I usually output intermediate values in the shader to make sure they are what I expect them to be. For example, instead of outputting the light color, output the distance each pixel is to the light. I found that to be the best way to debug a shader.

Well It's about shadows.

The depthColor of cameraDepth and lightDepth seems wired. And won't do right occlusion. 

In my project Projection Texture is well working .

The problem must be the depthValue of lightView or cameraView of both of them.

But I'm just confused.. I've been thinking this for hours just won't figure this out..Really driving me crazy.

 

(BTW,sorry for not showing you a picture. I'll post one picture later.)

Edited by AntiMoron

Share this post


Link to post
Share on other sites
input.pos is the system value SV_Position, which is already divided by input.pos.w when passed to pixel shader. Remove division by input.pos.w in pixel shader or use an additional value with a different semantic name for the position. I have never used the values from SV_Position in the pixel shader, so I don't know if it works if you just remove the division by input.Pos.w, but it's worth a try.

Share this post


Link to post
Share on other sites
if((saturate(projectTexCoord.x) == projectTexCoord.x) 
&& (saturate(projectTexCoord.y) == projectTexCoord.y))
{

I don't really understand this part. Could you explain why you need this condition?

Share this post


Link to post
Share on other sites
if((saturate(projectTexCoord.x) == projectTexCoord.x) 
&& (saturate(projectTexCoord.y) == projectTexCoord.y))
{

I don't really understand this part. Could you explain why you need this condition?

 

 

That determines if the projected coordinates are in the range 0 to 1 - that is, they are in the view of the light.

 

@AntiMoron: Do you have debug runtime running? (I'm guessing no). It shouldn't make a difference, but this typo:

float4 viewPosition : TEXCOORED1; // TEXCOORD1

should result in lots of complaints in the debug window. When you have a problem, you should always set your debug output level to max.

 

Also, the pix you posted has the shadow rendered. Can you point out more precisely what the problem is? BTW, are those diagonal stripes your texture, or some sort of artifact?

Edited by Buckeye

Share this post


Link to post
Share on other sites

That determines if the projected coordinates are in the range 0 to 1 - that is, they are in the view of the light.

 

Of course. For some reason I assumed that he uses a directional light source, my bad.

Edited by yoshi_lol

Share this post


Link to post
Share on other sites

For some reason I assumed that he uses a directional light source

 

The same technique can be used for a spot or directional light. The only difference in the rendering shader is whether you dot the normal with light position or light direction to determine light intensity (the OP's code doesn't do that, BTW).

Edited by Buckeye

Share this post


Link to post
Share on other sites

I don't want to go off-topic here so I'll keep it short: you usually build the lightspace matrices so that they cover the entire camera frustum anyways so the texture coordinates are always in [0,1]  by definition - but you probably know that already.

Edited by yoshi_lol

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!