Compensating for distortion when projecting a 2D texture onto a sphere I want to map an image texture on to a sphere but I can't work out how to compensate for distortion. The sphere mesh is as generated by jMonkeyEngine's Sphere object with unit radius and poles on the Z axis. The points in the mesh are approximately equidistant on the surface. The texture coordinates it generated caused bad stretching at the poles, none of the polar distortion options in Gimp corrected it, and I couldn't work out what transformation/mapping it was using. So I decided to replace the texture coordinates with my own.
I thought up a simple projection scheme, considering the sphere as two hemispheres and only using a circular portion of the texture image. Each pole is at the centre of the texture and the equator maps on to the circumference of the circle. If we pretend that the texture coordinates are in the range [-1,1] (they're actually [0,1] but that can be ignored for now and remapped after the "difficult" part) all we have to do is copy the x and y from the position coordinates. However, that causes stretching of the texture near the equator. I know why it's being distorted; it's because for large values of x or y near the equator a small variation of that x or y maps to a much bigger distance on the surface. Despite knowing that I still can't devise a formula to cancel out the distortion. Can anyone help?
 A: Since one of your comments indicates that stereographic projection would be all right as well, here is a formula for that. I'll assume $(x,y,z)$ is a point on the unit sphere, i.e. $x^2+y^2+z^2=1$. Then the stereographic projection of the $z>0$ hemisphere onto the unit disk using $(0,0,-1)$ as center of projection would be
\begin{align*}
u &= \frac x{1+z} &
v &= \frac y{1+z}
\end{align*}
Conversely, from the unit disc onto the upper hemisphere is
\begin{align*}
x &= \frac{2u}{1+u^2+v^2} &
y &= \frac{2v}{1+u^2+v^2} &
z &= \frac{1-u^2-v^2}{1+u^2+v^2}
\end{align*}
All of this works without any trigonometric functions. For the other hemisphere (as the Wikipedia page uses it), you simply swap the sign wherever $z$ is mentioned.
If you want to, you can compare my stereographic projection (green) against the trigonomentric map from your solution (blue):

Apparently your transformation causes less overall length deformation, but changes angles and causes the pixels near the rim of the unit circle to become stretched towards the pole.
A: I kept on thinking about it and drawing diagrams etc and eventually came up with an idea that seems to at least reduce the stretching significantly. I know I haven't done a good job of describing the problem, and probably wouldn't do a good job of describing my solution either, so posting my code is probably a better way to describe it. For a point on the surface of the sphere at (x, y, z):
// Latitude is easily derived from z
double lat = Math.abs(Math.asin(z));
// Express this as distance from pole along surface
double dist = 1 - lat / Math.PI * 2;
// Where this projects on to x axis (for y = 0):
double xProj = Math.cos(lat);
// Ratio of dist to xProj gives a multiplier to compensate
// for texture getting stretched near equator
float ratio = (xProj == 0) ? 1 : (float) (dist / xProj);
newUV[n * 2] = (x * ratio + 1) / 2;
newUV[n * 2 + 1] = (y * ratio + 1) / 2;

