title: Opengl: earth texturing
body: <p>i'm trying to use a texture to draw the Earth over a sphere. My sphere is composed by by quads, each quad is composed by two triangles:</p>
  
  <pre><code>void Sphere(float radius, int n_lat, int n_lon)
  {
      glEnable(GL_LIGHTING);
  glEnable(GL_TEXTURE_2D); 
  
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  
      TextureManager::Inst()-&gt;BindTexture(1);
  
      glBegin(GL_TRIANGLES);
  for (float i = 0; i &lt; n_lat; i += 1.f)
      for (float j = 0; j &lt; n_lon; j += 1.f)
      {
          Vector3 p;
          GLfloat u;
          GLfloat v;
          // -- first triangle
          p = SphereCoord(i, j, n_lat, n_lon);
                  glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  
          p = SphereCoord(i + 1, j, n_lat, n_lon);
          glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  
  
          p = SphereCoord(i + 1, j + 1, n_lat, n_lon);
          glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  
  
  
          // -- second triangle
          p = SphereCoord(i, j, n_lat, n_lon);
                  glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  
  
  
          p = SphereCoord(i + 1, j + 1, n_lat, n_lon);
          glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  
  
  
          p = SphereCoord(i, j + 1, n_lat, n_lon);
          glNormal3(p);
  
          u=atan2f(p.y,p.x)/2*PI+0.5;
          v=0.5-asinf(p.z)/PI;
          glTexCoord2f(u,v);
  
          glVertex3(p * radius);
  }
  glEnd();
  }
  </code></pre>
  
  <p>With this code don't obtain a correct texture mapping. Does anyone can give me some tips?</p>
  
  <p>It looks like the texture is in someway repeated on the longitude axis:
  <img src="https://i.stack.imgur.com/SSdfy.png" alt="Image of the earth"></p>