How to detect which 3d object has been touched?
Assume I have 3 cubes in 开发者_开发问答random location/rotation that are visible in the viewport. I want to be able to detect if which of the cube has been touched if I touch ( or click on emulator) a specific cube. If I just touch empty space instead of a cube, this should also be properly detected. I know there is onTouchEvent callback that you over-ride on a View but this only gives me x,y coordinate of where the touch occurred. I am looking for OpenGL-ES based solution and more specifically for Java based Android application.
THere could be several objects (or none) that are on that x / y coordinate. You should find them all, and then if there are any, check the z-coordinate to see which one is the closest to the viewport (and so the one that was touched).
Often wondered about best way to do this without glSelect()... this answer looks good: iPhone OpenGL ES - How to Pick
(You can ignore the fact it's an iOS question... the OpenGL ES API is the same on both platforms AFAIK).
精彩评论