1
\$\begingroup\$

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 ;-)

some advice?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Did you mean "represents the EARTH"? \$\endgroup\$
    – Bill
    Mar 7, 2011 at 0:53

3 Answers 3

0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Your approach looks good. Some links for each step, assuming OpenGL:

  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.)

  2. find line-sphere intersection code e.g. from this excellent page.

  3. inverse the rotation of the nearest intersection point to account for the sphere's rotation e.g. adapt this.

\$\endgroup\$
3
  • \$\begingroup\$ thanks! but with line-sphere intersection give me 2 points, and thet're in world coordinate. how to convert them into texture coordinate? \$\endgroup\$
    – nkint
    Mar 7, 2011 at 11:22
  • \$\begingroup\$ The point nearest the screen is the interesting one \$\endgroup\$
    – Will
    Mar 7, 2011 at 12:02
  • \$\begingroup\$ great resource bytheway! \$\endgroup\$
    – nkint
    Mar 11, 2011 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .