title: OpenGL mapping texture to sphere
body: <p>I have OpenGL program that I want to texture sphere with bitmap of earth. I prepared mesh in Blender and exported it to OBJ file. Program loads appropriate mesh data (vertices, uv and normals) and bitmap properly- I have checked it texturing cube with bone bitmap.</p>
  
  <p>My program is texturing sphere, but incorrectly (or in the way I don't expect). Each triangle of this sphere includes deformed copy of this bitmap. I've checked bitmap and uv seems to be ok. I've tried many sizes of bitmap (powers of 2, multiples of 2 etc). </p>
  
  <p>Here's the texture:</p>
  
  <p><img src="https://i.stack.imgur.com/iqOyO.jpg" alt="Earth texture"></p>
  
  <p>Screenshot of my program (like It would ignore my UV coords):</p>
  
  <p><img src="https://i.stack.imgur.com/ZrpFp.jpg" alt="enter image description here"></p>
  
  <p>Mappings of UVs in Blender I've done in this way:</p>
  
  <p><img src="https://i.stack.imgur.com/dERQg.png" alt="enter image description here"></p>
  
  <p>Code setting texture after loading it (apart from code adding texture to VBO- I think it's ok):</p>
  
  <pre><code>  GLuint texID;
    glGenTextures(1,&amp;texID);
    glBindTexture(GL_TEXTURE_2D,texID);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_BGR,GL_UNSIGNED_BYTE,(GLvoid*)&amp;data[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  </code></pre>
  
  <p>Is there needed any extra code to map this texture properly?</p>
  
  <p>[Edit]
  Initializing textures (earlier presented code is in LoadTextureBMP_custom() function)</p>
  
  <pre><code>bool Program::InitTextures(string texturePath)
  {
    textureID = LoadTextureBMP_custom(texturePath);
    GLuint TBO_ID;
    glGenBuffers(1,&amp;TBO_ID);
    glBindBuffer(GL_ARRAY_BUFFER,TBO_ID);
    glBufferData(GL_ARRAY_BUFFER,uv.size()*sizeof(vec2),&amp;uv[0],GL_STATIC_DRAW);
    return true;
  }
  </code></pre>
  
  <p>My main loop:</p>
  
  <pre><code>bool Program::MainLoop()
  {
    bool done = false;
    mat4 projectionMatrix;
    mat4 viewMatrix;
    mat4 modelMatrix;
    mat4 MVP;
  
    Camera camera;
    shader.SetShader(true);
  
    while(!done)
    {
      if( (glfwGetKey(GLFW_KEY_ESC)))
        done = true;
      if(!glfwGetWindowParam(GLFW_OPENED))
        done = true;
  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
      // Tutaj przeksztalcenia macierzy
      camera.UpdateCamera();
      modelMatrix = mat4(1.0f);
      viewMatrix = camera.GetViewMatrix();
      projectionMatrix = camera.GetProjectionMatrix();
      MVP = projectionMatrix*viewMatrix*modelMatrix;
      // Koniec przeksztalcen
  
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D,textureID);
  
      shader.SetShaderParameters(MVP);
  
      SetOpenGLScene(width,height);
  
      glEnableVertexAttribArray(0); // Udostepnienie zmiennej Vertex Shadera =&gt; vertexPosition_modelspace
      glBindBuffer(GL_ARRAY_BUFFER,VBO_ID);
      glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
  
      glEnableVertexAttribArray(1);
      glBindBuffer(GL_ARRAY_BUFFER,TBO_ID);
      glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(void*)0);
  
      glDrawArrays(GL_TRIANGLES,0,vert.size());
  
      glDisableVertexAttribArray(0);
      glDisableVertexAttribArray(1);
  
      glfwSwapBuffers();
    }
    shader.SetShader(false);
  
    return true;
  }
  </code></pre>
  
  <p>VS:</p>
  
  <pre><code>#version 330
  
  layout(location = 0) in vec3 vertexPosition;
  layout(location = 1) in vec2 vertexUV;
  out vec2 UV;
  
  uniform mat4 MVP;
  
  void main()
  {
    vec4 v = vec4(vertexPosition,1.0f);
    gl_Position = MVP*v;
    UV = vertexUV;
  }
  </code></pre>
  
  <p>FS:</p>
  
  <pre><code>#version 330
  
  in vec2 UV;
  out vec4 color;
  
  uniform sampler2D texSampler; // Uchwyt tekstury
  
  void main()
  {
    color = texture(texSampler, UV);
  }
  </code></pre>