Sign in to follow this  
Followers 0
Lotes

Render depth to texture

8 posts in this topic

Hi, I work with XNA and I want to render the depth information of my scene into a texture. But all I get is my scene (some cubes) completly colored in white. So the depth information I determined is greater or equal 1.0 I guess. How is it done right? I googled a lot, but I can't find my mistake :'-(... Do I have to make any settings in XNA before rendering?

Here are the HLSL shaders I use:

[CODE]
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;

struct VertexToPixel
{
float4 Position : POSITION;
float4 Color : COLOR0;
};

struct PixelToFrame
{
float4 Color : COLOR0;
};

VertexToPixel PositionsVS( float4 inPos : POSITION)
{
float4x4 preViewProjection = mul (xView, xProjection);
float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
VertexToPixel Output = (VertexToPixel)0;
Output.Position = mul(inPos, preWorldViewProjection);
Output.Color.a = 1;
Output.Color.rgb = Output.Position.z/Output.Position.w;
return Output;
}

PixelToFrame PositionsPS(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIn.Color;
return Output;
}

technique Positions
{
pass Pass0
{
VertexShader = compile vs_3_0 PositionsVS();
PixelShader = compile ps_3_0 PositionsPS();
}
}
[/CODE]
0

Share this post


Link to post
Share on other sites
hi,
its ok (because of depth precision) try to output "view" z e.g. :

float4x4 preWorldView= mul (xWorld, xView);

Output.Position = mul(inPos, preWorldView);
Output.Color.rgb = Output.Position.z/zFar; (zFar is far clip plane in projection matrix)

and then

Output.Position = mul(Output.Position,xProjection);

this way you will get linearDepth rendered and you should see it better
0

Share this post


Link to post
Share on other sites
Thanks. My far clip plane has a distance of 200.0f. Is that the value for zFar?
When I tried out your code my cubes became entirely black :-/. Edited by Lotes
0

Share this post


Link to post
Share on other sites
Here is it...
[CODE]
VertexToPixel PositionsVS( float4 inPos : POSITION)
{
VertexToPixel Output = (VertexToPixel)0;

float4x4 preWorldView= mul (xWorld, xView);
Output.Position = mul(inPos, preWorldView);
Output.Color = 1;
Output.Color.rgb = Output.Position.z/200;
Output.Position = mul(Output.Position,xProjection);
return Output;
}
PixelToFrame PositionsPS(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIn.Color;
return Output;
}
[/CODE] Edited by Lotes
0

Share this post


Link to post
Share on other sites
it looks good,
your cubes are drawn but black or not drawn at all ?
try also add 200.0 instead of 200
(in c++ it will ruin your calculation, not shure about hlsl)
0

Share this post


Link to post
Share on other sites
they are drawn (black cubes on blue background).
changing 200 to 200.0 did not help. i think in hlsl it does not matter.
0

Share this post


Link to post
Share on other sites
maybe you values are very close to near plane, try to divide by smaller number (20),
when they are still black try 2. What are positions of your cubes ? and dimensions ?
0

Share this post


Link to post
Share on other sites
It works with this code. But I find it very ugly to use a magic number like 25 as a factor in this case. How can I calculate this factor from my matrices? :-/
[CODE]
VertexToPixel PositionsVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float4 inColor: COLOR)
{
VertexToPixel Output = (VertexToPixel)0;
float4x4 preViewProjection = mul (xView, xProjection);
float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
Output.Position = mul(inPos, preWorldViewProjection);
Output.Color = 1;
Output.Color.rgb = Output.Position.z;
return Output;
}
PixelToFrame PositionsPS(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = PSIn.Color/25;
return Output;
}
[/CODE]

My cubes have a dimension of 0.9x0.9x0.9 at positions (0,0,-10), (0,0,-9) ... (0,0,10).
My camera is at (0,10,-20) looking at (0,0,0).
0

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  
Followers 0