Sign in to follow this  

[RenderMonkey] Position from DepthMap

Recommended Posts


I have problems to get position from depth map.
my code:

VS: ( this code is generated by RenderMonkey for Quad )

struct VS_OUTPUT
float4 pos : POSITION0;
float2 texCoord : TEXCOORD0;

VS_OUTPUT vs_main( float4 inPos: POSITION )

inPos.xy = sign( inPos.xy);
o.pos = float4( inPos.xy, 0.0f, 1.0f);

// get into range [0,1]
o.texCoord = (float2(o.pos.x, -o.pos.y) + 1.0f)/2.0f;
return o;


sampler2D DepthMap;
float4x4 matViewProjectionInverse;

float4 ps_main( float2 texCoord : TEXCOORD0 ) : COLOR
float2 ScreenPos = texCoord;

ScreenPos.x = (ScreenPos.x*2.0)-1.0f;
ScreenPos.y = (ScreenPos.y*2.0)-1.0f;
float4 Screen3DPos = float4(ScreenPos.x,ScreenPos.y, tex2D( DepthMap, texCoord ).x ,1.0f );

Screen3DPos = mul( Screen3DPos, matViewProjectionInverse );
Screen3DPos /= Screen3DPos.w;
Screen3DPos.w = 1.0f;

return Screen3DPos;

So, what i am doing there. I get Screen position in range -1 to 1 with depth of z.
I multiply it with Inverse of ViewProjection matrix to get World matrix. I divided result with w and display position as color.

Color looks ok, but when rotating with objects, color are changing, strange.

Where is a problem ? thank you very much.

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