3
\$\begingroup\$

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.

enter image description here

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

2 Answers 2

2
\$\begingroup\$

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;
...
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – GuyRT
    Commented Oct 15, 2014 at 14:33
1
\$\begingroup\$
  • 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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .