Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / World position from depth

Author
Message
Kuper
18
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 9th Oct 2014 21:00 Edited at: 9th Oct 2014 21:06
Hi!
Get some annoying problem.
I have to reconstructing world position from depth which was rendered in A32B32G32R32F format.Im using one of the old examples made by Evolved.
So I render depth ( use alpha chanel only ). I also left only main code.


in geometry shader
Out_Geometry VS_Geometry(In_Geometry IN)
{
Out_Geometry OUT;
OUT.Pos=mul(IN.Pos,WorldVP);
OUT.Depth=OUT.Pos;
return OUT;
}

float4 PS_Geometry(Out_Geometry IN) : COLOR
{
return float4(0,0,0,IN.Depth.z/IN.Depth.w);
}

Ok.Now I try to reconstruct World Position in quad shader.
I DBPro I use this:

set current camera AL_Camera
view matrix4 AL_Matrix(1)
projection matrix4 AL_Matrix(2)
multiply matrix4 AL_Matrix(3),AL_Matrix(1),AL_Matrix(2)
null=inverse matrix4(AL_Matrix(4),AL_Matrix(3))
set effect constant matrix AL_SSAOEffect,"ViewProjInverse",AL_Matrix(4)

in quad shader I have this:

matrix ViewProj:ViewProjection;
matrix ViewInv:ViewInverse;
matrix ViewProjInverse;

float4 Geometry=tex2D(GeometrySampler,IN.Tex);
float4 WPos=mul(float4(IN.ViewProjPos,Geometry.w,1),ViewProjInverse);
WPos.xyz /=WPos.w;

Seems that all work well but:
1) Wpos is correct only in small distance near camera.( using camera range 1-2000)
2) When I set camera range for example 25,2000 - whole thing stop work at all
3) Also when I change camera range in DBPro SSAO shader work differently!Looks like DBPro renders xyz of object differently on different camera ranges This is too weird(

So maybe I need to set camera range 25,2000 and after that set ViewProj manually in DBPro?
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 11th Oct 2014 13:03
To avoid confusion you might want to not call your vertex shader a geometry shader since the geometry shader stage is its own thing introduced with DX10.

The depth value is relative to the camera's position and as such corresponds to the world position "depth" units along the camera's forward vector.
Your posted code is incomplete, what type is OUT.pos and OUT.depth? I'm assuming a float4, in which case you will indeed write to the alpha component of OUT.depth, however... it will correspond to the w component of the position, whereas the depth in view space is stored in the z component.
Also it seems the depth is in screen (projection) space rather than view space since you multiply it by matWVP.
In this case you will need to transform it back into world space by using the inverse view projection matrix. That will only give you the Z coordinate however.

The easiest way to do what you're after is probably to store the Z coordinate in view space; this will be the depth relative to the camera.
Then you transform it into world space like so:
float3 wPos = CameraPosition + CameraForward * depth;



Disclaimer: I was quite tired writing this so I may have overlooked something, just thought I'd share my thoughts though.


"Why do programmers get Halloween and Christmas mixed up?"

Login to post a reply

Server time is: 2026-07-17 23:34:02
Your offset time is: 2026-07-17 23:34:02