Skip to main content
GameDev.net gamedev.net
🔒 Locked

Fragment Depth

Started by absriram Nov 18, 2004 at 6:01 PM 3 replies 500+ views
Original Post
absriram
absriram
Hi, How can I get the depth of a fragment in a scene inside a fragment shader? I tried using WPOS semantic but when I set the wpos.z value as the red component of the output color i always get red as the output color.

pixelOutput(float4 color : COLOR, float4 pos : WPOS);
{
pixelOutput OUT;
OUT.color.xyz = float3(pos.z, 0.0, 0.0);
return OUT;
}

Where am I going wrong.
vincoof
vincoof
Unless your viewing frustum is in the [0,1] range, you will hardly be able to use such basic method.
Your pos.z value should range between [near,far] (as defined in the viewing frustum, typically paramaters of gluPerspective or glFrustum) so if you want to display it in a color channel, you will have to map this from this [near,far] range to the traditional color [0,1] range.
absriram
absriram
Thanks, but how do I do it?

Sriram

_DarkWIng_
_DarkWIng_
Pass near and far as some kind of uniform to shader then do:
color.xyz = float3( (pos.z - near)/(far-near), 0.0, 0.0 );

To optimize it a bit you should pass 1/(far-near) as some kind of factor so you have:
color.xyz = float3( (pos.z - near)*factor, 0.0, 0.0 );

PS : You can optimize it a bit furter by replacing operation order (multiply then add to compress into a single MAD instruction)
You should never let your fears become the boundaries of your dreams.
silvermace
silvermace
if you get this to work, could you post your CG code here? i tried this before and just couldnt get it to work...

Topic Locked

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

Sign in to reply to this topic.