Skip to main content
gamedev.net
🔒 Locked

Z-buffer depth value extraction

Started by tokaplan Mar 13, 2007 at 12:14 PM 3 replies 950+ views
Original Post
tokaplan
tokaplan
Hi everyone, After projecting a vector into projection space float4 projVec = mul( worldVec, matViewProjection ); I need to extract the depth values that will be written to z-buffer and w-buffer from projVec. How do I do this? It's obvious that projVec.z and/or projVec.w contain the information I need, but I can't figure out how to get it. Thank you
sirob
sirob
Where is it you want to get this information to?

If you're interested in reading it back from the shader to your application, that isn't possible. If you want to use the data in the shader itself, this should be rather trivial, depending on what exactly you want to use the data for.
Sirob Yes.» - status: Work-O-Rama.
tokaplan
tokaplan
I just need to use this data in the shader. I know it's trivial, but after taking a look at z and w components of the resulting vector in Maple, I still can't figure out how to get these depths values. All I need is calculate the number between 0 and 1 which will be written to the depth buffer.
Defrag
Defrag
Just pass an additional float4 texcoord to the pixel shader, then divide the z co-ord by w.

Something like:

struct VS_OUTPUT {    float4 Position	: POSITION;     float4 PositionTex	: TEXCOORD0;};VS_OUTPUT vs_main( in float4 position : POSITION ){float4 projVec = mul( position, matViewProjection ); Out.position = projVec; // clip position for normal usageOut.PositionTex = float4( projVec.zzz, projVec.w ); // same deal, but this is extra so we can pass it to the pixel shaderreturn Out;}float4 ps_main( VS_OUTPUT Input ) : COLOR{float depth = Input.PositionTex.z / Input.PositionTex.w; // we need to divide by w to get the depth in range 0 to 1//whatever else you want}

tokaplan
tokaplan
Thanks a lot, it works.

P.S. It's so much easier with w-buffer...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.