Selecting a model instance with point and click
Before we start with the implementation, we will look at the two different approaches to add visual selection to an application: by “shooting” a ray into the virtual scene, and by using a texture holding an index of the instances.
Pros and cons of shooting virtual rays
You may find the following idea of shooting a virtual ray into your scene appealing:
We already have the position of the camera in the virtual world as the first endpoint, and by mapping the mouse pointer positions back from screen positions to scene coordinates, you will get the second endpoint. Mapping coordinates back to the scene is only a couple of matrix inversions and multiplications away.
Sounds promising and easy, doesn’t it?
Sadly, it is common to underestimate the final complexity at this point. As long as you only have a single model in the world, or two, everything is fine. You shoot the virtual ray into the scene,...