Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
Please do not double-post (stackoverflow.com/questions/26328666/…) –  Krom Stern Oct 13 '14 at 5:45
    
note that in your image, the object-local depth is view oriented. –  v.oddou Oct 15 '14 at 7:25

2 Answers 2

up vote 2 down vote accepted

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;
...
share|improve this answer
1  
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 –  GuyRT Oct 15 '14 at 14:33
    
thanks GuyRT - my bad, corrected –  Qwerty Oct 20 '14 at 13:38
  • 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.

share|improve this answer
    
Thank you, is there some easier way to achieve such effect maybe just by playing with the matrix ? –  MaT Oct 13 '14 at 8:25
    
@MaT only if you don't need the actual "normalization", you could simply subtract object position projection from vertex distance –  snake5 Oct 13 '14 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 Nov 5 '14 at 22:54
    
any precisions concerning your solution ? –  MaT Nov 25 '14 at 13:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.