views:

25

answers:

1

I'd like to compute the intersection between a ray and the models the user sees on the screen of my app. I'm following the OpenGL guide but I have many models in the scene that I draw independently and dynamically in a push-pop context applying a translate to the current modelview matrix (ie for each model I do a

glPushMatrix,

glTranslatef,

draw the model,

glPopMatrix ).

Do I need to compute also different rays, one for each object (for testing later if it intersects with the model's bounding sphere)?

Maybe some concept about ray picking is not so clear to me yet. Any link, guide, etc will be very appreciated ^^

+1  A: 

The picking ray has nothing to do with the model matrix. If the objects in your scene move (i.e. the model matrices change), the ray itself won't move.

Which method do you plan to use between those proposed in the link ? GL_SELECTION, rendering with a special color, home-made intersections ?

I'll edit this answer accordingly

EDIT : let's go for the home-made one then

First, you need to generate a ray. You usually want to express it in world space (i.e. "my ray starts at the same pos than my camera, and its dir is (xyz) ). You have two methods to do that, which are pretty well explained in the openGL Faq (One way to generate a pick ray... Another method...). Just copy 'n paste the code.

If the mouse is in the middle of the screen, you should have your ray pointing towards the same direction than your camera. You can check that with your arguments to gluLookAt.

Now, what you want to do is intersect your ray with your meshes' triangles, but you have a problem, because the triangles are not expressed in the same system than the ray.

So you have to express the ray in the triangle's system.

triangles system : the model system ; ray system : the world system. Fortunately, you have a matrix to go from model to world (the Model matrix, the one you set after your glPushMatrix). So inverse(ModelMatrix) is the matrix from world to matrix.

ray = Ray(cam_pos, dir);
foreach (mesh)
    ModelMatrix = whatever you want
    InverseModelMatrix = inverse(ModelMatrix)
    rayInModelSpace = Ray(InverseModelMatrix x ray.pos, InverseModelMatrix x ray.dir)
    Intersect(rayInModelSpace, mesh)

Your Intersect method could look like this :

foreach (triangle in mesh )
    if ray.intersect(triangle) return true

triangle-ray intersection code can be found at geometrictools.com

This will be quite slow, but it will work. There are several ways to improve the preformance, but the easiest is to compute the AABB of each mesh, and first intersect the ray with the AABB. No intersection with the AABB => no intersection with the mesh

Code can be found on geometrictools too.

I'll be away for a few days, so good luck :)

Calvin1602
@Calvin1602: thanks for answering ^^ I'm trying to do the home made one by transforming it by the inverse of the ModelView matrix. So the ModelView matrix mean something, I guess
rano
@Calvin1602: thank you this really helps and confirms what I was thinking, glad to hear I did learn something about OpenGL even though I am still a noob at it. And that link is also very useful too.
rano