Jump to content
  • Advertisement
Sign in to follow this  
skyemaidstone

Depth Map problem in forward pass

This topic is 1305 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'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
Advertisement

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

Thanks for the great help guys, that works perfectly. I still get confused by clip space, texture space and all the other coordinate spaces.

 

I'll remember the code tags next time I have a question too biggrin.png

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!