title: What is the correct way to map a texture on a sphere?
body: <p>I'm having problems with the correct mapping of a sphere. I used a world-map to show where it goes wrong. North America appears on the front from top to bottom, while South America is on the other side upside down and continents like Asia are not even on the map.</p>
  <p><a href="https://i.stack.imgur.com/kSckD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kSckD.png" alt="Screenshot" /></a><br />
  <sub>(source: <a href="http://i.troll.ws/3aedbe4b.png" rel="nofollow noreferrer">troll.ws</a>)</sub></p>
  <p>The code below is the sphere-object</p>
  <pre><code>class Shape {
      
      public void drawSphere(double radius, int slices, int stacks) {
          gl.glEnable(GL_TEXTURE_2D);
          head.bind(gl); //Method that binds the world-map (for testing) texture.
          gl.glBegin(GL_QUADS);
          double stack = (2 * PI) / stacks;
          double slice = (2 * PI) / slices;
          for (double theta = 0; theta &lt; 2 * PI; theta += stack) {
              for (double phi = 0; phi &lt; 2 * PI; phi += slice) {
                  Vector p1 = getPoints(phi, theta, radius);
                  Vector p2 = getPoints(phi + slice, theta, radius);
                  Vector p3 = getPoints(phi + slice, theta + stack, radius);
                  Vector p4 = getPoints(phi, theta + stack, radius);
                  double s0 = theta / (2 * PI);
                  double s1 = (theta + stack) / (2 * PI);
                  double t0 = phi / (2 * PI);
                  double t1 = (phi + slice) / (2 * PI);
                                                        
                  vectorToNormal(norm(p1));
                  gl.glTexCoord2d(s0, t0);
                  vectorToVertex(p1);
                  
                  vectorToNormal(norm(p2));
                  gl.glTexCoord2d(s0, t1);
                  vectorToVertex(p2);
                  
                  vectorToNormal(norm(p3));
                  gl.glTexCoord2d(s1, t1 );
                  vectorToVertex(p3);
                  
                  vectorToNormal(norm(p4));
                  gl.glTexCoord2d(s1, t0);
                  vectorToVertex(p4);
              }
          }
          gl.glEnd();
          gl.glDisable(GL_TEXTURE_2D);
      }
      
      Vector getPoints(double phi, double theta, double radius) {
          double x = radius * cos(theta) * sin(phi);
          double y = radius * sin(theta) * sin(phi);
          double z = radius * cos(phi);
          return new Vector(x, y, z);
      }
  </code></pre>
  <p>How can I fix it? I tried swapping some coordinates and other things, but that made it even messier for me.</p>
  <p>Also, there seem to be some artifacts when on the spere when I bind the texture to it. Is that fixable?</p>