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