Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, howardR, EternallyCurious, 1 invisible), 770 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
[sub] world space reconstruction from a view space depthmap #420821
04/04/13 09:13
04/04/13 09:13
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
I finally found a way to reconstruct the world space coords of a scene from a prerendered depthmap in view space. It has been quiet hard for me to find the proper info so I decided to share it. It is probably not the best way but it works!

I wanted to render the depthmap in view space because its linear distribution.

The view space depth is computed in the object shader
Code:
// vs
outDepth = mul ( inPos, matWorldView ).z;
// ps floating point bitmap
return float4 ( inDepth, 0, 0, 1.0f );
// ps packed for 8 bits bitmap channel
float viewDepth = ( inDepth - camera.clip_near ) / camera.view_fustrum_depth; //*


*view_fustrum_depth = clip_far - clip_near;

The deferred pass needs to convert the screen coords to any point in clip space in order to convert it to view space by inverse projection matrix multiply.
Code:
float4 Proj = float4 ( 2.0f * float2 ( inTex.x, 1.0f - inTex.y ) - 1.0f, fAnyDepth, 1.0f );
float3 View = mul(Proj, matProjInv).xyz;



This randomly selected point certainly describes the camera projection ray for the actual screen pixel, so we can scale its coords by the relation between the point depth and the depthmap value, and get the renderered coords in view space.
Code:
float fDepth = tex2Dlod ( DepthSampler, inTex ).r;
View = float3 ( View.xy * fDepth / View.z, fDepth );



Finally we only need to convert the view space coords to world space with the inverse view matrix.
Code:
float3 World = mul ( float4(View.xyz,1.0f), matViewInv ).xyz;



Hope it helps.
Salud!

Re: [sub] world space reconstruction from a view space depthmap [Re: txesmi] #420828
04/04/13 11:19
04/04/13 11:19
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
thanks a lot! I'm currently trying to make my deferred renderer better and wanted to use z-Reconstrunction for the world coords in order to keep the gBuffer slim.

oh and talking about useful shader code (and if you don't mind contributing), this might be helpful as well (especially for SSAO or RLR):
convert world- to screen-space coordinates
Code:
float2 worldToScreenSpace(float3 worldPos)
{
	float3 temp = mul(worldPos - vecViewPos.xyz, matViewProj);
	
	temp *= 1 / temp.z; // normalize to z = 1
	return temp.xy * float2(0.5, -0.5) + 0.5 + vecViewPort.zw * 0.5; // final coords
}



POTATO-MAN saves the day! - Random
Re: [sub] world space reconstruction from a view space depthmap [Re: Kartoffel] #420839
04/04/13 14:14
04/04/13 14:14
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
This is what I use for wPos-reconstruction:
Code:
void VS(		
in float4 vPos		   : POSITION,

out float2 OutTex	      : TEXCOORD0,			
out float3 npPos	      : TEXCOORD1,						
out float4 Pos	         : POSITION
)
{
	Pos.xyzw = float4(vPos.xy, 1.f, 1.f);
	
	float2 XY = tan(radians(30)) * float2(1, vecViewPort.y * vecViewPort.z);

	npPos = mul(float4((vPos.xy) * XY, 1, 0), matViewInv).xyz;
	
	OutTex.xy = Pos.xy * float2(1,-1) * 0.5 + 0.5;
	OutTex.xy += vecViewPort.zw * 0.5f;
}

void PS(	
in float2 Tex	      : TEXCOORD0,			
in float3 npPos	   : TEXCOORD1,
//in float4 vPos	      : TEXCOORD2,

out float4 COL :COLOR0
) 
{	
	float Depth = tex2Dlod(DSmp, float4(Tex.xy, 0, 0)).x;
	
	COL.xyz = npPos.xyz * Depth + vecViewPos.xyz;

	//	COL.xyz = tex2D(WPosSmp, Tex).xyz;
	COL.xyz = length(COL.xyz - tex2Dlod(WPosSmp, float4(Tex.xy, 0, 0)).xyz) * 1000;
	COL.w=1;
}

technique Shading
{
	pass P0
	{	
		VertexShader = compile vs_3_0 VS();
		PixelShader  = compile ps_3_0 PS();
	}
}


I'm not using the GS standard pp-stuff since you can't use a custom vertex shader with it and the texture coordinates are weird sometimes. The FOV is hardcoded to 60° here. Note that this is not the actual default arc for views even it is documented this way in the manual (15 is also not the default clip_near..should probably post that in the manual forum too).

Re: [sub] world space reconstruction from a view space depthmap [Re: Hummel] #420873
04/05/13 11:13
04/05/13 11:13
Joined: Jun 2004
Posts: 655
to your left
BoH_Havoc Offline
User
BoH_Havoc  Offline
User

Joined: Jun 2004
Posts: 655
to your left
This is how i do it in Shade-C EVO

Code:
//Return position in view space
//inTex: texcoords in screenspace
//inDepth: linear depth * clip_far
float3 CalculatePosVSQuad(float2 inTex, float inDepth)
{
	float4 viewRay;
	viewRay.x = inTex.x*2-1;
	viewRay.y = (1-inTex.y)*2-1;
	viewRay.z = 1;
	viewRay.w = 1;
		
	viewRay.xyz = mul(viewRay, matProjInv).xyz;
	return (viewRay.xyz * inDepth);
}



Shade-C EVO Lite-C Shader Framework

Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1