Sign in to follow this  
skyemaidstone

Depth Map problem in forward pass

Recommended Posts

Hi,

 

I'm hoping someone can help me with a problem I'm struggling with.

 

I have my one little engine going with deferred rendering and shadows etc. Now I'm trying to do a forward rendering pass for transparent models.

 

I've got as far as getting the models to render (and be transparent) but I don't seem able to work out how to use my depth map in the forward pass. I'm possibly getting lost in the various coordinate spaces.

 

Here's a screen shot of the problem (the barrel shouldn't be visible since its behind the wall)

 

depthproblemjpg.jpg

 

The shader is pretty simple for rendering the barrel:

 

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

VertexShaderOutput output;

float4 worldPosition = mul(float4(input.Position.xyz,1), World);

float4 viewPosition = mul(worldPosition, View);

output.Position = mul(viewPosition, Projection);

output.Depth.x = output.Position.z;

output.Depth.y = output.Position.w;

output.TexCoord = input.TexCoord;

?

return output;

}

struct PixelShaderOutput

{

half4 Color : COLOR0;

};

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)

{

//sample depth from depth map

float depthFromMap = tex2D(DepthMapSampler, input.TexCoord).r;

float depthOfPixel = input.Depth.x/input.Depth.y;

if (depthFromMap < depthOfPixel)

discard;

PixelShaderOutput output;

output.Color = tex2D(diffuseSampler, input.TexCoord);

output.Color.a = 0.1f;

return output;

}

 

And the shader for creating the depth map is:

 

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

VertexShaderOutput output;

float4 worldPosition = mul(float4(input.Position.xyz,1), World);

float4 viewPosition = mul(worldPosition, View);

 

output.Depth.x = output.Position.z;

output.Depth.y = output.Position.w;

 

return output;

}

 

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)

{

PixelShaderOutput output;

 

output.Depth = input.Depth.x / input.Depth.y;

return output;

}

 

Which seems essentially the same. So what am I doing wrong, any ideas please?

 

 

Share this post


Link to post
Share on other sites

float depthFromMap = tex2D(DepthMapSampler, input.TexCoord).r;

 

It appears you're sampling the depth texture with texture coordinates. wink.png  Try sampling with (pos.x/pos.w, pos.y/pos.w). [EDIT - incomplete. See unbird's code below ]

 

EDIT: Also, you should use code tags for posting code. The tags will format the code and help others view your posts more easily.

Edited by Buckeye

Share this post


Link to post
Share on other sites
You cannot use the same tex coord for depth sampling like you do for (I guess) the albedo texture. Similar to shadow mapping you have to map the clip space coords (input.Position) to texture space. Something like this.
 
    ...
    float2 depthTex = input.Position.xy / input.Position.w;	
    depthTex.y = -depthTex.y;
    depthTex.xy = depthTex.xy / 2  + 0.5;
    float depthFromMap = tex2D(DepthMapSampler, depthTex).r;
    ...
Though I wonder: If you do the manual depth testing the same way like usual depth test, why not just setup a usual depth test ?

Edit: Ahh, and beaten smile.png

PS: Welcome to gamedev.net.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this