# exact point on a rotating sphere

I have a sphere that represents the Earth textured with real pictures. It's rotating around the x axis, and when user click down it has to show me the exact place he clicked on. For example if he clicked on Singapore the system should be able to:

• understand that user clicked on the sphere (OK, I'll do it with unProject)
• understand where user clicked on the sphere (ray-sphere collision?) and take into account the rotation
• transform sphere-coordinate to some coordinate system good for some web-api service
• ask to api (OK, this is the simpler thing for me ;-)

• Did you mean "represents the EARTH"? – Bill Mar 7 '11 at 0:53

Will's answer is perfect until you want to do some displacement (which you want propably if simulating planet).

In my opinion the color picking would be the best solution. It is fast to implement and works with o(log(n)) complexity (+ time to render). Every triangle on the sphere will have unique color. Render planet with only colors, no lighting in same resolution as screen. Get clicked point and at the same position as on the screen, check color. Find object with the color. Objects can be simply ordered (color is number) so binary search with log(n) complexity can be used.

We used this simple and old fashion technique for our game Save Us! for geting areas on planet with no performance loss. So i can tell you, that it is suitable for realtime application.

Open simple approach is colour picking.

Create two versions of your world map texture that you texture your sphere with. The first is for the user to see, the second for the selection. On the second, each country is drawn with a unique colour.

When the user clicks, render the scene without lighting or other effects, and using this second colour-coded texture. Don't swap-buffers - don't show this version to the user. But then read the colour value using glReadPixels as in the article above.

1. gluUnproject the x,y of the click (flipping the y if necessary; screen coordinates for clicks are typically 0,0 is top-left of screen, whereas OpenGL 0,0 is bottom-left) for near (0.0) and far (1.0). This gives you a line segment which is the ray projecting from your mouse cursor through the scene. (gluUnproject is deprecated, so you might want to copy-n-paste it into your code from here.)