Render depth to texture

Started by
7 comments, last by Lotes 11 years, 5 months ago
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:


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();
}
}
Advertisement
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
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 :-/.
yes, you shall use 200 as far plane,
could you please post your new code ?
Here is it...

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;
}
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)
they are drawn (black cubes on blue background).
changing 200 to 200.0 did not help. i think in hlsl it does not matter.
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 ?
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? :-/

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;
}


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).

This topic is closed to new replies.

Advertisement