Mapping or converting window coordinates to OpenGL object coordinates is indeed a very nice thing. Say you want to drag an object in your scene with the mouse. Simply handle all necessary mouse events and then convert the current mouse position in the corresponding coordinates in OpenGL space. Sounds easy and .. yes it really is!
The function that makes this very, very easy is gluUnProject(..) declared in GL\glu.h. Here's the definition:
The three floats winx, winy and winz represent the window coordinates that should be converted. modelMatrix, projMatrix and viewport are the corresponding OpenGL state values that can be retrieved with glGetDouble(..) and glGetInteger(..). They are needed to calculate the final object coordinates that will be stored in the pointers objx, objy and objz.
This function works best in othogonal projections only. With perspective projection the results aren't very satisfactory. Also remember to reset the current modelview matrix with glLoadIdentity() before using the function. Besides this function there is a very similar function called gluProject(..) that does the inverse of gluUnProject: it converts object coordinates to window coordinates. It works exactly the same only that the winx etc. and objx parameters and swapped.
Here is an example of how to use gluUnProject(..). Say you have an object that will be translated based on the array double position. Secondly you want this object moved when the mouse moves too. Here's the code (Mfc mouse handler as example):