View Full Version : OpenGL ES Sphere latitude/longitude

09-29-2010, 07:25 PM
Hello everyone,

I am trying to figure out the best way to make an interactive 3d sphere in OpengGL ES 1.1. I have the program loading in a 3D mesh sphere that has vertex/normal/texture coordinates and mapping an texture onto the surface.

Now my question is how can I figure out the latitude and longitude of any given point on the sphere?

The ultimate goal is to use lat/long to map to xy coordinates of the texture. Basically touch the sphere and see what part of the mapped texture was touched.

I would greatly appreciate any help with this. Thanks!

09-29-2010, 11:21 PM
A simple way is to render the sphere GL_NEAREST mapped with a 256*256 texture having a red gradient in the S direction, and blue gradient in th T direction. Then readpixels the pixel under mouse cursor, from the red and blue component of the texture find the S and T texture coordinates. This will not work if you need more than 256 values per coordinate.

Edit: this may be overkill for a simple sphere, as a basic ray-sphere intersection would be more accurate and probably faster. But it will work for any textured model.

09-30-2010, 11:12 AM
Thanks for the suggestion with glReadPixels. I have a 1024x512 texture that is getting mapped onto the sphere, it has to be that size because it will be broken up into 32 128x128px tiles. So I need to find a way to look up which tile the mouse clicked. These tiles are a bunch of images loaded from a website, I have no way of knowing what they are.

I am trying to render a 1024x512 grid of with different colors for each tile and draw that into an FBO. Is there a way to have glReadPixles read from the FBO rather than the texture mapped to the sphere?

When I do:

glBindFramebufferOES(GL_FRAMEBUFFER_OES, gridFrameBuffer);

glReadPixels(loc.x, loc.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColor[0]);

The pixel colors are from the sphere texture and not from gridFrameBuffer. Does readpixles not operate on the currently bound framebuffer?

carsten neumann
09-30-2010, 02:58 PM
It does, but you need to tell it which color buffer/attachment to read from (an FBO can have more than one of those), with something like: glReadBuffer(GL_COLOR_ATTACHMENT0)

09-30-2010, 04:33 PM
Unfortunately glReadBuffer is not available in OGL ES... is there another way to do this?

09-30-2010, 09:12 PM
I believe there is another way to do it. I assume the only reason you are trying to use a FBO is so that you can render your texture mapped sphere and render a colored tile version of the same sphere in the same rendering pass, right?

Rather than all that complication, you could render your sphere in two passes, but to the default framebuffer both times. No FBO required, so you could use glReadPixels().

The idea is this:
First pass, render the sphere with your uniquely colored tiles. Do not swap the back buffer with the front buffer (you must use a double buffered framebuffer) so this rendering pass will never be displayed to the user.

Then call glReadPixels() where the cursor is located, reading from the back buffer. You can identify the tile the cursor is over by inspecting the color of the pixel returned. Do whatever you need to whatever is represented by the location where this tile is located.

Clear the back buffer. Render the sphere with the texture maps intended to be displayed.

Swap the back buffer with the front buffer.

There you have it. All the user ever sees is the texture mapped sphere, and all your program ever sees (via glReadPixels()) is the colored tiles rendered version.