Depth Map problem in forward pass

Started by
2 comments, last by skyemaidstone 9 years ago

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?

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

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

This topic is closed to new replies.

Advertisement