4
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ Just a note, you pass in world space z coordinates into glm::unProject(). This function was made to handle screen space x&y along with a depth value read from the depth buffer. This is because there is another way of picking, find screen space x&y, read depth value from depth buffer at that x&y, then unproject. \$\endgroup\$ May 2, 2013 at 1:06

1 Answer 1

1
\$\begingroup\$

How can I calculate intersection with my scene geometries?

it varies depending on your performance needs and scene size, there are different approaches the easiest one is checking ray intersection with all bounding volumes containing geometry in the scene, however this may not be good enough if your scene is huge, or you have limited computational power. Other approaches usually use Hierarchical scene structure hence eliminating most of the intersection possibilities, two examples are using K-d trees or bounding volume hierarchies (each one has its own Pros. and Cons.). good resource for this is the book "Real Time Rendering" and its companion website http://www.realtimerendering.com/.

It's better to calculate if point is inside a bounding box or ray traversing a bounding box?

Well a ray has a starting point, and direction you can test if the origin is in the bounding box, but "usually" this will be a redundant step since most well known tests cover this already. One method for testing Ray AABB and OBB is called SLABS Method and is well covered in real time rendering.

How can I compute bounding box with Transformable matrix (rotation, scale, …)?

AABB is Axis Aligned hence cannot be rotated, so you need to use Oriented Bounding Boxes (OBB) you can check a lot of details about intersections tests and bounding volumes here http://www.realtimerendering.com/intersections.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .