In addition to Mouse Selection using Ray Casting , cf. Answer Yang, another widely used method is a collection buffer, explained in detail (with C ++ code) here
The 3D assembly trick is very simple. We attach a pointer to each triangle, and for FS, the index of the triangle to which the pixel belongs is displayed. The end result is that we get a โcolorโ buffer that does not actually contain colors. Instead, for each pixel that is covered by some primitive, we get the index of this primitive. When we click on the window, we will read back this index (depending on the location of the mouse) and select the red triangle. By combining the depth buffer in the process, we guarantee that when several primitives overlap the same pixel, we get the index of the most primitive element (closest to the camera).
So, in a nutshell:
- For each method of drawing objects, a constant index and a boolean value are required for whether this drawing displays a pixel buffer or not.
- The rendering method converts the index to grayscale and the scene is displayed
- After all the rendering is done, extract the pixel color at the touch position
GL11.glReadPixels(x, y, /*the x and y of the pixel you want the colour of*/) . Then translate the color back to the index and the index back to the object. Voilร , you have your object with a click.
To be fair, for a mobile usecase, you probably should read a 10x10 rectangle, cut it and select the first non-phonon found, because touches are never accurate.
This approach works regardless of the complexity of your objects.

source share