The core functionality of this addon is testing ray intersection against polylines, meshes etc… Mouse picking is just a little functionality added at the end, and it uses internally
screenToWorld as @roymacdonald was suggesting in the forum a while ago. https://github.com/edap/ofxRaycaster/blob/4937ea4f3460d191e41eca2548f3dffbac939814/src/Mousepicker.cpp#L15
There are 2 ways to do mouse picking in openGL, one is ray casting, what I have used in this addon, and the other one is to render the scene in another buffer , and in this buffer each object has an identifier. More info here -> https://www.opengl.org/archives/resources/faq/technical/selection.htm
I prefer the ray picking because it does not involve an additional render pass.
Chapter performances: A ray stores only two glm::vec3, the memory footprint is therefore low. What is not performant at the moment is the ray mesh intersections method. This is because it simply tests the intersection of a ray with every triangle composing a mesh, without any optimization. There are two kind of optimization that I could add:
- A simple bounding box. Instead of testing the intersection of a ray with a triangle, I could simply test the intersection of a ray with the box containing the triangle. This is fast but not so really precise, you can easily imagine all the false positive that you can have testing the intersection of a ray with a tree using a bounding box containing a tree. There is an old PR here https://github.com/openframeworks/openFrameworks/pull/514, any feedback is welcome.
- Using more sophisticated spatial algorithm to represent the mesh, like a A BVH tree, Bounding Volume Hierarchy tree. Basically, smaller details are enclosed into smaller boxes, and those boxes are ordered in a tree structure. There are already other addons covering this topic (like https://github.com/kylemcdonald/ofxBvh by @kylemcdonald and https://github.com/bakercp/ofxSpatialHash by @bakercp ), so I would probably simply add a method that accepts those data structure as a parameter instead of adding those algorithms into this addon.