• Advertisement
Sign in to follow this  

Deferred rendering - Trouble with depth

This topic is 3490 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 all! I've got a problem at rendering depth to render target. The depth value keeps being in a certain range. Here is my code: VS_OUTPUT VS(VS_INPUT IN) { VS_OUTPUT OUT; OUT.pos = mul(IN.pos, WVP); OUT.depth.x = OUT.pos.z; OUT.depth.y = OUT.pos.w; return OUT; }; PS_OUTPUT PS(VS_OUTPUT IN) { PS_OUTPUT OUT; OUT.depth = IN.depth.x/IN.depth.y; return OUT; }; What's wrong here?

Share this post


Link to post
Share on other sites
Advertisement
Doing that will output a depth value that is always of the range [0,1], where 0 is the near-clipping plane and 1 is the far clipping plane. Is that not what you want?

Share this post


Link to post
Share on other sites
I know that.
But this code dont working:
VS_OUTPUT VS(VS_INPUT IN)
{
VS_OUTPUT Output;
Output.pos = mul(IN.pos, WVP);
Output.tex = IN.tex;
return Output;
}

float4 PS(VS_OUTPUT IN) : COLOR0
{
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;
.......
}
I want to compute the world position from screen space, but it's not correct.

Share this post


Link to post
Share on other sites
For reconstructing position you want something like this:


VS_OUTPUT VS(VS_INPUT IN)
{
VS_OUTPUT Output;
Output.pos = mul(IN.pos, WVP);
Output.posCS = Output.pos;
Output.tex = IN.tex;
return Output;
}

float4 PS(VS_OUTPUT IN) : COLOR0
{
float depth = tex2D(depthtex, IN.tex.xy).r;

float4 pos;
pos.x = IN.posCS.x;
pos.y = IN.posCS.y;
pos.z = depth * IN.posCS.w;
pos.w = IN.posCS.w;

pos = mul(pos, IVP);
pos /= pos.w;
.......
}



Also there was a rather large thread on this topic a while ago in graphics programming & theory...you may want to check that out if you get stuck.

Share this post


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

  • Advertisement