Screen-to-plane coords (what's under the mouse?)

I’ve made a “map” application.

Normally you’re looking at a plane that’s located at the NearPlaneClip distance.

My calls look like this:

glViewport(0, 0, W, H);
glFrustum(-W/2, W/2, -H/2, H/2, NearPlaneClip, NearPlaneClip*100); 

…where W and H means the size of the screen image in pixels.

Finding out what the mouse cursor points at, is easy in this mode. However, I have included a feature where the map goes 3d, by drawing the scene with the following modelview matrix:

glRotatef(-RotateAngle, 1, 0, 0);
glTranslatef(0, H/2, 0);

…which gives the nice effect of rotating the map on the X axis located at y = bottom of the screen.
By increasing the RotateAngle (range from 0 to 90) it will look more like flying than watching a map.

However, I still want to know what the mouse points at in the “flying” mode.

Currently the only buffer I have is RGB and Alpha (no stencil buffers, no depth buffer, no accumulation buffers etc), and I would like it to stay that way, so I can’t really use gluUnproject (since I need to read the depth buffer value for that to work), and to render everything twice seems like a huge waste of performance and memory.

Everything is located on one single plane, so I guess the math should be quite easy - I just can’t figure it out!

// David

Finally figured it out!

// David