Well, I made this tutorial with all my knowledge about picking in OpenGL, but i think it's weird stuff, so it was difficult to explain it. If you find this is bad written, tell me and i'll try to rewrite it.
And I'm sorry about glPushName and glPopName, but actually I can't understand how they works, so I didn't explained how they works, I hope that someone can explain it.
This is a great tutorial. Just to be sure, I'm getting 3-6 hits whenever I click on a glutSolidCube, I assume this is once per face, is this correct?
Well, the code does something but I have no idea how to verify if it is correct. I am new to this. However, there are the followinig issues: 1) What does minimum/maximum z mean? Where is it referenced from? 2) What is the unit of z? The world-unit supposed to be meter. 'mericans will probably use yard. This is important so one can create the models/objects in world-units and so they will be portable between applications. (They can be sold/bought). In any case, the unit is a float. So why is the result an integer? The opengl documentation doesn't say it. (Like the other open source stuff, the opengl documentation is horrific) I would appreciate if someone could clear this up. Cheers, Bill
About converting a user's click coordinates to world-spatial coordinates: I worked this algorithm out, and it seems to work for a perspective projection matrix:
Given the place where the user clicked and the location of the camera's eye, map the click so that the lower-left is <1, 1> and the upper-right is <-1, -1>. Then create a vector v=<xmapped, ymapped, 1, 1>. Multiply the x and y components of v by the inverse of the projection matrix (which must have no transforms on it), then multiply all of v by the modelview matrix. Then you have a ray (<eyepoint> + t*<vx, vy, vz>) going from the camera right down the pixel you clicked. (Note, v is not a unit vector, but for finding intersections it should be OK).
Again, I don't have a mathematical proof but it works for e.g. finding where exactly on an arbitrary plane the user clicked in a strategy game.
Happy coding, Erik