I am trying to get the following image to be projected to a sphere using mercator:
I have gotten this far using the formula from this : how map 2d grid points (x,y) onto sphere as 3d points (x,y,z)
My code is the following to generate the coordinates from (X,Y):
public void generateSphericalCoords(){
int R = 400; // Image Radius
int S = 400; // Sphere Radius
float longitude = (float)(this.x)/R;
float latitude = (float) (2*Math.atan(Math.exp((double)(this.y)/R)) - Math.PI/2);
sphericalX = (int) (S*Math.cos(latitude) * Math.cos(longitude)) + 300;
sphericalY = (int) (S*Math.cos(latitude) * Math.sin(longitude)) + 300;
sphericalZ = (int) (S*Math.sin(longitude));
//System.out.println(sphericalX + " " + sphericalY + " " + sphericalZ);
}
However, instead of getting a perfect sphere, I get this:
What am I doing wrong? Any help would be greatly appreciated.
EDIT:
I have gotten to the following formula:
float longitude = (float) ((float)(Math.PI*this.x)/R - Math.PI/2);
float latitude = (float) (Math.PI*2*Math.atan(Math.exp((float)(this.y)/R)));
sphericalX = (int) (S*Math.cos(latitude) * Math.cos(longitude)) + 300;
sphericalY = (int) (S*Math.cos(latitude) * Math.sin(longitude)) + 300;
sphericalZ = (int) (S*Math.sin(longitude));
However, I have gotten an odd ring along the outside edge, as shown: