title: OpenGL Sphere vertices and UV coordinates
body: <p>I know there are many similar questions for this issue, such as <a href="https://stackoverflow.com/questions/5988686/creating-a-3d-sphere-in-opengl-using-visual-c/5989676#5989676">this one</a>, but I can't seem to figure out what is going wrong in my program.</p>
  
  <p>I am attempting to create a unit sphere using the naive longitude/latitude method, then I attempt to wrap a texture around the sphere using UV coordinates.</p>
  
  <p>I am seeing the classic vertical seam issue, but I'm also some strangeness at both poles.</p>
  
  <p>North Pole...
  <a href="https://i.stack.imgur.com/T1nDI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T1nDI.png" alt="North Pole"></a></p>
  
  <p>South Pole...
  <a href="https://i.stack.imgur.com/I6s9p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I6s9p.png" alt="enter image description here"></a></p>
  
  <p>Seam...
  <a href="https://i.stack.imgur.com/anBw8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/anBw8.png" alt="enter image description here"></a></p>
  
  <p>The images are from a sphere with 180 stacks and 360 slices.</p>
  
  <p>I create it as follows.</p>
  
  <p>First, here are a couple of convenience structures I'm using...</p>
  
  <pre><code>struct Point {
      float x;
      float y;
      float z;
      float u;
      float v;
  };
  
  struct Quad {
      Point lower_left;  // Lower left corner of quad
      Point lower_right; // Lower right corner of quad
      Point upper_left;  // Upper left corner of quad
      Point upper_right; // Upper right corner of quad
  };
  </code></pre>
  
  <p>I first specify a sphere which is '_stacks' high and '_slices' wide.</p>
  
  <pre><code>float* Sphere::generate_glTriangle_array(int&amp; num_elements) const
  {
      int elements_per_point  = 5; //xyzuv
      int points_per_triangle = 3;
      int triangles_per_mesh = _stacks * _slices * 2; // 2 triangles makes a quad
      num_elements = triangles_per_mesh * points_per_triangle * elements_per_point;
  
      float *buff = new float[num_elements];
      int i = 0;
  
      Quad q;
  
      for (int stack=0; stack&lt;_stacks; ++stack)
      {
          for (int slice=0; slice&lt;_slices; ++slice)
          {
              q = generate_sphere_quad(stack, slice);
              load_quad_into_array(q, buff, i);
          }
      }
  
      return buff;
  }
  
  Quad Sphere::generate_sphere_quad(int stack, int slice) const
  {
      Quad q;
  
      std::cout &lt;&lt; "Stack " &lt;&lt; stack &lt;&lt; ", Slice: " &lt;&lt; slice &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "   Lower left...";
      q.lower_left = generate_sphere_coord(stack, slice);
      std::cout &lt;&lt; "   Lower right...";
      q.lower_right = generate_sphere_coord(stack, slice+1);
      std::cout &lt;&lt; "   Upper left...";
      q.upper_left = generate_sphere_coord(stack+1, slice);
      std::cout &lt;&lt; "   Upper right...";
      q.upper_right = generate_sphere_coord(stack+1, slice+1);
      std::cout &lt;&lt; std::endl;
  
      return q;
  }
  
  Point Sphere::generate_sphere_coord(int stack, int slice) const
  {
      Point p;
  
      p.y = 2.0 * stack / _stacks - 1.0;
  
      float r = sqrt(1 - p.y * p.y);
      float angle = 2.0 * M_PI * slice / _slices;
  
      p.x = r * sin(angle);
      p.z = r * cos(angle);
  
      p.u = (0.5 + ( (atan2(p.z, p.x)) / (2 * M_PI) ));
      p.v = (0.5 + ( (asin(p.y)) / M_PI ));
  
      std::cout &lt;&lt; " Point: (x: " &lt;&lt; p.x &lt;&lt; ", y: " &lt;&lt; p.y &lt;&lt; ", z: " &lt;&lt; p.z &lt;&lt; ") [u: " &lt;&lt; p.u &lt;&lt; ", v: " &lt;&lt; p.v &lt;&lt; "]" &lt;&lt; std::endl;
  
      return p;
  }
  </code></pre>
  
  <p>I then load my array, specifying vertices of two CCW triangles for each Quad...</p>
  
  <pre><code>void Sphere::load_quad_into_array(const Quad&amp; q, float* buff, int&amp; buff_idx, bool counter_clockwise=true)
  {
      if (counter_clockwise)
      {
          // First triangle
          load_point_into_array(q.lower_left, buff, buff_idx);
          load_point_into_array(q.upper_right, buff, buff_idx);
          load_point_into_array(q.upper_left, buff, buff_idx);
  
          // Second triangle
          load_point_into_array(q.lower_left, buff, buff_idx);
          load_point_into_array(q.lower_right, buff, buff_idx);
          load_point_into_array(q.upper_right, buff, buff_idx);
      }
      else
      {
          // First triangle
          load_point_into_array(q.lower_left, buff, buff_idx);
          load_point_into_array(q.upper_left, buff, buff_idx);
          load_point_into_array(q.upper_right, buff, buff_idx);
  
          // Second triangle
          load_point_into_array(q.lower_left, buff, buff_idx);
          load_point_into_array(q.upper_right, buff, buff_idx);
          load_point_into_array(q.lower_right, buff, buff_idx);
      }
  }
  
  void Sphere::load_point_into_array(const Point&amp; p, float* buff, int&amp; buff_idx)
  {
      buff[buff_idx++] = p.x;
      buff[buff_idx++] = p.y;
      buff[buff_idx++] = p.z;
      buff[buff_idx++] = p.u;
      buff[buff_idx++] = p.v;
  }
  </code></pre>
  
  <p>My vertex and fragment shaders are simple...</p>
  
  <pre><code>// Vertex shader
  #version 450 core
  
  in vec3 vert;
  in vec2 texcoord;
  
  uniform mat4 matrix;
  
  out FS_INPUTS {
     vec2 i_texcoord;
  } tex_data;
  
  void main(void) {
     tex_data.i_texcoord = texcoord;
     gl_Position = matrix * vec4(vert, 1.0);
  }
  
  // Fragment shader
  #version 450 core
  
  in FS_INPUTS {
     vec2 i_texcoord;
  };
  
  layout (binding=1) uniform sampler2D tex_id;
  
  out vec4 color;
  
  void main(void) {
     color = texture(tex_id, texcoord);
  }
  </code></pre>
  
  <p>My draw command is:</p>
  
  <pre><code>glDrawArrays(GL_TRIANGLES, 0, num_elements/5);
  </code></pre>
  
  <p>Thanks!</p>