Jump to content

  • Log In with Google      Sign In   
  • Create Account


Render depth to texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Lotes   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 November 2012 - 07:30 AM

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


Sponsor:

#2 joeblack   Members   -  Reputation: 260

Like
0Likes
Like

Posted 08 November 2012 - 08:06 AM

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

#3 Lotes   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 November 2012 - 08:28 AM

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, 08 November 2012 - 08:28 AM.


#4 joeblack   Members   -  Reputation: 260

Like
0Likes
Like

Posted 08 November 2012 - 08:32 AM

yes, you shall use 200 as far plane,
could you please post your new code ?

#5 Lotes   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 November 2012 - 08:43 AM

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

Edited by Lotes, 08 November 2012 - 08:44 AM.


#6 joeblack   Members   -  Reputation: 260

Like
0Likes
Like

Posted 08 November 2012 - 09:06 AM

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)

#7 Lotes   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 November 2012 - 09:14 AM

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

#8 joeblack   Members   -  Reputation: 260

Like
0Likes
Like

Posted 08 November 2012 - 09:19 AM

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 ?

#9 Lotes   Members   -  Reputation: 103

Like
0Likes
Like

Posted 08 November 2012 - 09:30 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS