I'm trying to implement picking in my framework but I don't understand how I can do this.
I'm working with:
- OpenGL ES 2.
- GLM mathematic library.
What I have understand, picking can be made with two methods:
- Draw scene with one color per polygon, and read pixel under mouse.
- Ray projection in 3D and finally calculate geometry <-> ray intersection.
My architecture:
- A Geometry class with indices/vertices/texture coords/colors data.
- A Scene class with a Mesh array (using a geometry).
- A Transformable class with Matrix computed with rotation/position/scale.
- A Camera class with methods like getViewport/….
What I have:
My ray calculation:
void Ray::createFromEvent( const Camera& camera )
{
glm::mat4 model = camera.getMatrix();
glm::mat4 projection = camera.getProjectionMatrix();
glm::vec4 viewPort = camera.getViewport();
this->origin = glm::unProject( glm::vec3( x, y, camera.getMinDistance() ),
model,
projection,
viewPort );
this->direction = glm::unProject( glm::vec3( x, y, camera.getMaxDistance() ),
model,
projection,
viewPort );
}
So, my questions are:
- How can I calculate intersection with my scene geometries?
- It's better to calculate if point is inside a bounding box or ray traversing a bounding box?
- How can I compute bounding box with Transformable matrix (rotation, scale, …)?
(I am on mobile so I am limited by hardware performance.)
Thanks!