Having lost a couple of hours searching for a working sample of ray picking with an implementation of the gluUnProject function for iPhone OpenGL ES, and several hours making it work, we’ve decided to give it out to the community.

The interesting part of the project can be found in the glu.h / glu.c files and EAGLView.m.

The implementation of the Ray Picking algorithm can be found in the function called: checkCollision().

Here is a little sketch (that works better than a thousand words):

The project can be downloaded here: Ray Picking Project.

Hello there, I been looking for a sample like this for ages, dont suppose you could help me. ?

You've got mail !

hi , this sample code was very helpful to understand ray picking. Thanks. i got a question regarding multiple objects drawn using push/ pop stack.something similar to this.

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(x,y,z);

glRotate(r,1,0,0);

glTranslate(-x,-y,-z);

for (i=0 to 20) objects

glpushMatrix();

draw_object

glpopMatrix();

end

each object is a unit cube (textured to sphere) with its own transformation. In such a case how does ray picking work. How should i keep track of center point of the object to calculate ray intersection. i really appreciate any help.

Thanks

Sowmya

Did you have any issues with glUnproject() ? I found that if you have a translated object (at least in the Z direction), gluUnproject() returns near and far coordinats that don't actually translate to the given point, so my code it fails. My understanding of the linear algebra of opengl is too weak to understand what's going on in the reverse projection.