title: Implementing opengl sphere example code
body: <p>Trying to implement a sphere example code I <a href="https://stackoverflow.com/questions/5988686/how-do-i-create-a-3d-sphere-in-opengl-using-visual-c?lq=1">found here on stackoverflow</a> I have been running into problems. Note that I am not using the draw code, I am using the <code>void SolidSphere</code> function from this example to create my vertex data.</p>
  
  <p>This is my code with some minor changes to the example that I refered to:</p>
  
  <pre><code>std::vector&lt;GLfloat&gt; vertices;
  std::vector&lt;GLfloat&gt; normals;
  std::vector&lt;GLfloat&gt; texcoords;
  std::vector&lt;GLushort&gt; indices;
  
      Sphere(shared_ptr&lt;GL::Shader&gt; _shader, shared_ptr&lt;Material&gt; _material, float radius, unsigned int rings, unsigned int sectors)
  {
      this-&gt;attachShader(_shader);
      this-&gt;attachMaterial(_material);
  
      /** Implementation from:
      https://stackoverflow.com/questions/5988686/how-do-i-create-a-3d-sphere-in-opengl-using-visual-c?lq=1
      **/
  
      float const R = 1./(float)(rings-1);
      float const S = 1./(float)(sectors-1);
      int r, s;
  
      vertices.resize(rings * sectors * 3);
      //normals.resize(rings * sectors * 3);
      texcoords.resize(rings * sectors * 2);
      std::vector&lt;GLfloat&gt;::iterator v = vertices.begin();
      //std::vector&lt;GLfloat&gt;::iterator n = sphere_normals.begin();
      std::vector&lt;GLfloat&gt;::iterator t = texcoords.begin();
      for(r = 0; r &lt; rings; r++) {
          for(s = 0; s &lt; sectors; s++) {
              float const y = sin( -M_PI/2 + M_PI * r * R );
              float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );
              float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );
  
              *t++ = s*S;
              *t++ = r*R;
  
              *v++ = x * radius;
              *v++ = y * radius;
              *v++ = z * radius;
  
              //*n++ = x;
              //*n++ = y;
              //*n++ = z;
          }
      }
  
  
  
      indices.resize(rings * sectors * 4);
      std::vector&lt;GLushort&gt;::iterator i = indices.begin();
      for(r = 0; r &lt; rings; r++) {
          for(s = 0; s &lt; sectors; s++) {
              *i++ = r * sectors + s;
              *i++ = r * sectors + (s+1);
              *i++ = (r+1) * sectors + (s+1);
              *i++ = (r+1) * sectors + s;
          }
      }
  
  
             /************* THIS IS THE ADDED PART ********/
      for (unsigned int i = 0; i &lt; vertices.size(); i += 3) {
          vertexCoords.push_back(glm::vec4(vertices[i], vertices[i+1], vertices[i+2], 1));
      }
  
      for (unsigned int i = 0; i &lt; texcoords.size(); i += 2) {
          textureCoords.push_back(glm::vec2(texcoords[i], texcoords[i+1]));
      }
      indexElements = indices;
  
      cout &lt;&lt; "Vertex vector size: " &lt;&lt; vertexCoords.size() &lt;&lt; endl;
      cout &lt;&lt; "Texture vector size: " &lt;&lt; textureCoords.size() &lt;&lt; endl;
      cout &lt;&lt; "Element index vector size: " &lt;&lt; indexElements.size() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The only big change here is using <code>M_PI / 2</code> instead of <code>M_PI_2</code> and two loops that pushes the value into the format my code uses which is:</p>
  
  <pre><code>vector &lt;glm::vec4&gt; vertexCoords;
  vector &lt;glm::vec2&gt; textureCoords;
  vector &lt;GLushort&gt; indexElements;
  </code></pre>
  
  <p>Here is a picture of what I am rendering:</p>
  
  <p><img src="https://i.stack.imgur.com/7Xpr7.png" alt="enter image description here"></p>
  
  <p>so I am seeing this last line on the top of the sphere..</p>
  
  <p>Is it anything obvious I am doing wrong in this code that could cause this?</p>