Picking, choosing nearest object


I just learned technique called picking, i learned it from these 2 tutorials
http://glprogramming.com/red/chapter13.html and http://gpwiki.org/index.php/OpenGL:Tutorials:Picking

although I do my program in jogamp, so i code in java instead. Next, I managed to implement the scenario…and my objects now react to the mouse perfectly apart from one condition, which is when there are multiple objects crossed by mouse cursor I am unable to identify which is the nearest one.

From tutorials I have believed the information will be given by returned buffer (array), the same one from which i get object names which are hit by mouse. At the very bottom in this tutorial there is a list_hits method. It does mention Z min and Z max values which should be coming as the second and third elements of buffer.

In my program i always receive same value (-2.14xxxx), for all objects which are picked and regardless of their position in the scene and my lookAt the scene.

How do I solve this?

Because so far all my objects are primitive spheres, it would help if i draw objects in order from farthest to nearest. That would give me always nearest object on picking too. But what if later my objects will carrying more complex shapes? I am not sure if this technique will be enough to get precise results.

What more do i have to do so the z-min and z-max values are retrieved correctly from selection rendering mode?

Finally I found out it was a problem of jogamp.
Version I had didn’t support the depth value properly, I downloaded newer version of jogamp and it began to work, apart the fact that new version has other rendering issues i noticed immediately :))))) nevermind, java is java :slight_smile: