I know two ways how to do this:
- Use frame buffer, where you render all objects, each with different color (no lighing, no texturing, ...). Then render normal scene.
When you click with your mouse, you read 2D coordinates, look in frame buffer and find color of pixel, where you have clicked. Then you will find object according to this color.
This method is called pixel picking.
- Render your scene, read clicked coordinates. Now you have to transfer 2D point back to 3D - create ray from camera to clicked point. Then do ray tracing and find first object in path (ray object collision).
// EDIT:
I have found this article, it could be useful.
Or you can create 3D points - your screen coordinates (x, y) - devided by screen size (you have to get coords in interval <-1,1>), and z coordinate will be -1 (starting point) and 1 (ending point). These are NDC (normalized device coordinates). Then multiply them by inverse projection matrix and inverse modelview matrix. Results should be starting and ending points of ray. You can find something here - it will help you understand those transformations.
Maybe you can use gluUnProject for this purpose...