2 registered members (VoroneTZ, TipmyPip),
1,333
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
restore position from depth
#294250
10/17/09 14:43
10/17/09 14:43
|
Joined: Jan 2007
Posts: 1,619 Germany
Scorpion
OP
Serious User
|
OP
Serious User
Joined: Jan 2007
Posts: 1,619
Germany
|
hello everyone, I am storing the distance to the camera (length of the position in viewspace) in a fp-texture and want to restore the position for each pixel later. To do so I get the projected point at the distance of 1, normalize it (So I've got the 'direction') and scale it by the looked up depth. This works all in all well. If I output the position as color, the grid is aligned to the camera axes and stays at its position (so it's cameraspace, isn't it?) So it looks like without the matViewInv applied Now I thought it would be easy, if I have the coordinates in cameraspace it's enough to multiply them with matViewInv to get everything alright. But that didn't worked out and I have no idea what could be wrong. Here is the relevant code: -get the depth
//VS:
OUT.vPos = mul(IN.pos, matWorldView);
//PS:
OUT.depth = float4(length(IN.vPos), 0.f, 0.f, 1.f);
-try to restore it
float depth = tex2D(depthMap, IN.tex).x;
float3 viewDir = float3(IN.tex.xy*2-1, 0.5773);//tan(camera.arc*0.5)
viewDir.y *= 0.75;//768/1024 = aspect
float4 pos = float4(normalize(viewDir)*depth, 1.f);
pos = mul(pos, matViewInv);
return pos;
I'm pretty desperate right now, because I tried to find a solution for it for a few days now... I hope you can help me with that. Thank you =) Scorpion
|
|
|
Re: restore position from depth
[Re: Hummel]
#294988
10/22/09 12:18
10/22/09 12:18
|
Joined: Jan 2007
Posts: 1,619 Germany
Scorpion
OP
Serious User
|
OP
Serious User
Joined: Jan 2007
Posts: 1,619
Germany
|
Hi Hummel, erstmal danke für die schnelle Antwort =)
Von w oder z wollte ich eigentlich keinen Gebraucht machen...Entschuldige meine Schlampigkeit^^
Deine Methode mit einem Lookup find ich auf jeden Fall super und sie scheint kaum etwas extra zu kosten werde ich mir bestimmt nochmal ansehen. Aber ich muss sagen ich bin zur Zeit nur am rumspielen und sehe, was ich so erreichen kann. Deswegen hab ich versucht, den Punkt in Viewcoordinaten zu bekommen (könnte z.B. für deferred Rendering Geschwindigkeit raushauen). Die eigentliche Frage im meinem Post war aber wohl eher: wieso tut matViewInv nicht das Gegenteil von matView? Nämlich vom Viewspace zurück in den Worldspace zu transformieren. Es müsste doch dann auch klappen, Screencoordinaten(mit z) direkt in den Worldspace umzuwandeln - auch wenn Matrixmultiplikationen ja eeher langsamer sind^^
edit: oouuppss! >.< Kein Wunder, dass hier keiner schreibt, wenn ich nur Mist von mir gebe... inverse Matrizen sind nicht exakt das, was ich dachte. Das macht jetzt wenig Sinn, weiter übers Problem zu reden, wenn da schon nen Problem drinsteckt... O.o
Last edited by Scorpion; 10/22/09 12:27. Reason: Die Schule ist schuld!
|
|
|
Re: restore position from depth
[Re: Hummel]
#294996
10/22/09 13:22
10/22/09 13:22
|
Joined: Jan 2007
Posts: 1,619 Germany
Scorpion
OP
Serious User
|
OP
Serious User
Joined: Jan 2007
Posts: 1,619
Germany
|
*verwirrtbin* Wenn das jetzt doch stimmt mit der inversen Matrix, dann würde mein Beispiel doch funktionieren. so weit ich das sehe, sollte es - vorausgesetzt das mit den Matrizen stimmt =) - dann im Worldspace sein, da ja nur die Viewtransformation rückgängig gemacht wird. Mit dem Objektspace hab ich gar nichts mehr zu tun. Und nochwas spricht dagegen: wenn ich es versuche, bewegen sich die Farben(welt xyz->rgb), anstatt wie ne Textur klebenzubleiben.
|
|
|
Re: restore position from depth
[Re: Hummel]
#295091
10/22/09 22:38
10/22/09 22:38
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
User
|
User
Joined: Jun 2004
Posts: 655
to your left
|
depth:
//Vertex Shader:
Output.Position = mul(inPos, matWorldViewProj);
Output.Position2D = Output.Position;
//Pixel Shader
depth = ((PSIn.Position2D.z-0)/maxDepth); //maxdepth = camera.clip_far
reconstruct world space position from depth:
float4x4 matMtl : matWorldViewProjInv;
//Pixel Shader
//compute world position
float4 vp = float4(inTex.xy * float2(2, -2) - float2(1, -1), depth, 1);
float4 v = mul(vp, matMtl);
float3 position = v.xyz/v.w;
//calculate ROUGH normals from position
float3 normal = cross(ddx(position),ddy(position));
might not be the fastest method, but works just fine
|
|
|
Re: restore position from depth
[Re: Scorpion]
#295191
10/23/09 12:27
10/23/09 12:27
|
Joined: Jun 2008
Posts: 151 Ukraine
XD1v0
Member
|
Member
Joined: Jun 2008
Posts: 151
Ukraine
|
@Scorpion you probably read my thoughts, i also wanted to know how to restore the world coordinate, basically you did my life much easier but you're converting them to -1..1, so: why? I guess this is projected coordinates to screen from -1 to 1 (left to right and from down to top) @BoH_Havok thank you for this code i was searching for his
Last edited by XD1v0; 10/23/09 12:41.
A7 Commercial Celeron 1700, GeForce 5500 FX 256mb, 1 Gb Ram
|
|
|
|