In my fragment/pixel shader I am trying to calculate a fragment's depth position in percent [0-1] in an given range. What I have is a bunch of shapes (there is no relative data given from them) but all the shapes have one thing in common and that is their depth is always 1000 pixels.
From the depth value I get from a fragment I want to be able to calculate where in this shape's range (1000.0 pixels) it is in a value between [0-1].
This means I could have a depth value for a current fragment of, say 2300, but I need to know where it is in the shape's depth of 1000 pixels.
So if the shape was lying exactly where the camera is I could calculate where the current fragments depth value is by doing:
float depth = (gl_FragCoord.z / gl_FragCoord.w);
float perc = 1.0 - (depth / 1000.0);
This is all well and good but the problem is when the shape is lying further away from the camera. How can I from that depth position calculate where it is in the range of 1000 px. In a way the range would have to be treated as it was also lying further away from the camera but the problem is I only got a fragments depth value and the range available to use here.
An image explains more! Imagine it from being above, so it goes down the Z axis.
So is this even at all possible to work out? Or should I try another approach?