# Shader - Calculate depth relative to Object

I am trying to calculate depth relative to the object.
Here is a good solution to retrieve depth relative to camera : Depth as distance to camera plane in GLSL

varying float distToCamera;

void main()
{
vec4 cs_position = glModelViewMatrix * gl_Vertex;
distToCamera = -cs_position.z;
gl_Position = gl_ProjectionMatrix * cs_position;
}


With this example the depth is relative to the camera.
But I would like get the depth relative to the object. I would like the same depth and value if I am near from the object or if I am far.

Here is an example of what I am trying to achieve. On the left you can see that the depth is relative to the camera. And on the right even if the camera moves back from the object, the depth remains the same because it is dependant to the object.

• Please do not double-post (stackoverflow.com/questions/26328666/…) Commented Oct 13, 2014 at 5:45
• note that in your image, the object-local depth is view oriented. Commented Oct 15, 2014 at 7:25

What you essentially want is the depth in camera coordinates, but relative to model, not camera. the easiest way to achieve that is to calculate the position of the origin of a model coordinate system in camera coordinates.

...
vec4 cs_position = glModelViewMatrix * gl_Vertex;
vec4 origin_position = glModelViewMatrix * vec4(0,0,0,1);
distToOrigin = origin_position.z - cs_position.z;
...

• I think you have a mistake here: origin_position should be glModelViewMatrix * vec4(0,0,0,1), This simplifies to: glModelViewMatrix[3]. In fact you can avoid the temporary altogether and just have: distToOrigin = glModelViewMatrix[3].z - cs_position.z Commented Oct 15, 2014 at 14:33
• calculate a bounding volume of the object (sphere, convex hull, box - depends on your speed/accuracy needs), this needs to be done only once - at initalization
• while rendering, for each object:
• transform camera (origin, direction) to object space
• project bounding volume on camera direction axis and subtract camera origin projection to retrieve min/max distance
• calculate local distance in the shader: d_local = (d_world - min) / (max - min)

This solution may not be completely accurate with all volume types (expect pixels not reaching limits at all times for bounding spheres/boxes) but it should be fast enough for dynamic objects.

There's also the issue of never reaching the maximum distance in case of overlap but to fix that, serious resources would have to be spent on this (double rendering, clearing buffers between renders etc.) so for whatever you're doing, there should be a better way if you feel this might be an important issue to solve.

• Thank you, is there some easier way to achieve such effect maybe just by playing with the matrix ?
– MaT
Commented Oct 13, 2014 at 8:25
• @MaT only if you don't need the actual "normalization", you could simply subtract object position projection from vertex distance Commented Oct 13, 2014 at 9:28
• Could you elaborate your system and some of the steps needed to achieve the effect ? Maybe with some pseudo-code or code example.
– MaT
Commented Nov 5, 2014 at 22:54
• any precisions concerning your solution ?
– MaT
Commented Nov 25, 2014 at 13:10