Obtain reliable values of
min_depth in everywhere I see models in the scene. Actually I obtain reliable
values of min_depth only in a great level of zoom (= Page Down).
Seems picking is working . . .
Also, the depth seems to be working fine too. Do however remember that it's returning the depth value in z-buffer units. These measure the part difference of the fragment from the near plane to the far one. For example, in your code, the near and far planes are at 0.1 and 2.0, respectively. If a fragment is 1.05 OpenGL units away from the camera, then in Z, your fragment is 0.5, because it is halfway between the near and far planes. However, in practice, the Z-buffer is mapped to be non-linear, so the conversion is more difficult.
Do note that for selecting, you can just use the values returned from the
selection buffer "as-is". Conversion is not necessary for a
comparison--only for getting actual OpenGL coordinates.
If you still need to convert to OpenGL units, you can do the following:
mpos = #mouse position--ensure that it is measured in
#pixels from the *lower right* of the screen!
returned_z_value = #whatever value you want to convert
viewport = glGetIntegerv(GL_VIEWPORT)
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
#pos is the actual
position of the object in OpenGL coordinates
pos = gluUnProject(mpos,mpos,returned_z_value,modelview,projection,viewport)
#tells how far away,
in OpenGL units, the object is
distance = length(pos-camerapos)
Other than this possible pitfall, I don't see any problem.