Reputation: 3931
I am programming in Xcode / OpenGL and have some 3d objects displayed in my view. Is there a way I can retrieve in OpenGL the 3D location / vertex / face I clicked on ?
Upvotes: 2
Views: 363
Reputation: 70106
The usual way to do this is by calling gluUnProject
twice, with your x and y coordinates in both cases, and znear in one, and zfar in the other case.
That gives you two points through which a ray goes. Your mouse click is on that ray.
Now collide objects (first bounding volumes, then per triangle if you want, or parametrically if possible) with that ray. The closest hit is the one you want.
A different possibility would be to read back the z-buffer value (glReadPixels
). This should be done with a pixel buffer object and spread over several frames (or it will be a very nasty stall on your pipeline). That will give you a 3D coordinate, for which you can find the closest object.
Or, you could use occlusion queries (redrawing a 1x1 viewport with color writes disabled) for the same effect.
Lastly, there is of course selection mode, but this is deprecated functionality, so you will probably not want to use it.
Upvotes: 3
Reputation: 187
Another way to do it if you are okay with using something that's deprecated in newer opengl versions is using the picking buffer/selection mode: http://www.lighthouse3d.com/opengl/picking/
Upvotes: 2