title: Sphere Shading OpenGL
body: <p>I am trying to shade a sphere.I have no idea where to start from. I calculated the vertices, and connected them by using GL_TRIANGLE_FAN, and I also drew the normals to each vertex. The problem is that I have no idea how to even start doing some shading/lighting. I am using OpeGL 3+. Here is some of my code:</p>
  
  <p>Sphere's Vertices Calculations (I found online and implemented):</p>
  
  <pre><code>void CreateUnitSphere(int dtheta,int dphi) //dtheta, dphi angle
  {
  
      GLdouble x,y,z;
      GLdouble magnitude=0;
      int no_vertice=-1;
      int n;
      int k;
      int theta,phi;
      const double PI = 3.1415926535897;
      GLdouble DTOR = (PI/180);//degrees to radians
  
  
      //setting the color to white
      for (k=0; k&lt;10296*3; k+=1)
      {
          sphere_vertices[k].color[0] = 1.0f;
          sphere_vertices[k].color[1] = 1.0f;
          sphere_vertices[k].color[2] = 1.0f;
      }
  
  
     for (theta=-90;theta&lt;=90-dtheta;theta+=dtheta) {
        for (phi=0;phi&lt;=360-dphi;phi+=dphi) {
  
  
       x = cos(theta*DTOR) * cos(phi*DTOR);
       y = cos(theta*DTOR) * sin(phi*DTOR);
           z = sin(theta*DTOR);
  
      //calculating Vertex 1
      no_vertice+=1;
      sphere_vertices[no_vertice].position[0] = x;
      sphere_vertices[no_vertice].position[1] = y;
      sphere_vertices[no_vertice].position[2] = z;
  
           x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
           y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
           z = sin((theta+dtheta)*DTOR);
  
      //calculating Vertex 2
      no_vertice+=1;
      sphere_vertices[no_vertice].position[0] = x;
      sphere_vertices[no_vertice].position[1] = y;
      sphere_vertices[no_vertice].position[2] = z;
  
      x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
          y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
          z = sin((theta+dtheta)*DTOR);
  
      //calculating Vertex 3
      no_vertice+=1;
      sphere_vertices[no_vertice].position[0] = x;
      sphere_vertices[no_vertice].position[1] = y;
      sphere_vertices[no_vertice].position[2] = z;
  
          if (theta &gt; -90 &amp;&amp; theta &lt; 90) {
  
              x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
              y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
              z = sin(theta*DTOR);
  
      //calculating Vertex 4
      no_vertice+=1;
      sphere_vertices[no_vertice].position[0] = x;
      sphere_vertices[no_vertice].position[1] = y;
      sphere_vertices[no_vertice].position[2] = z;
  
           }
  
      }
  
     }
  
  no_vertice = -1;
  int no_index=10296; 
  
  //calculate normals and add them to the array of vertices
     for (no_vertice=0; no_vertice&lt;=10296; no_vertice+=1) {
  
      no_index+=1;
  
      //getting the sphere's vertices
      x=sphere_vertices[no_vertice].position[0];
      y=sphere_vertices[no_vertice].position[1];
      z=sphere_vertices[no_vertice].position[2];
  
      //normalising vector "norm(Vertex - Center)"
      magnitude = sqrt((x*x) + (y*y) + (z*z));
  
      //adding the new vector (the one divided by the magnitude
      sphere_vertices[no_index].position[0] = (x/magnitude)/0.8;
      sphere_vertices[no_index].position[1] = (y/magnitude)/0.8;
      sphere_vertices[no_index].position[2] = (z/magnitude)/0.8;
  
      ///adding the vertex's normal (line drawing issue)
      no_index+=1;
      sphere_vertices[no_index].position[0] = sphere_vertices[no_vertice].position[0];
      sphere_vertices[no_index].position[1] = sphere_vertices[no_vertice].position[1];
      sphere_vertices[no_index].position[2] = sphere_vertices[no_vertice].position[2];
  
      }
  
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/YRsNQ.jpg" alt="Here is my Sphere without the &quot;GL_TRIANGLE_FAN&quot;, JUST &quot;GL_LINE_STRIP&quot;"></p>
  
  <p>Here is my Sphere without the "GL_TRIANGLE_FAN", JUST "GL_LINE_STRIP"
  and this is how I use "glDrawArrays" :</p>
  
  <pre><code>glDrawArrays(GL_LINE_STRIP, 0, 10296);
  glDrawArrays(GL_LINES, 10297, 30888);
  </code></pre>
  
  <p>From 0-10296 are the Sphere's Vertices.
  From 10297-30888 are the Sphere's Normal Vertices.</p>
  
  <p>Here is my Vertex file:</p>
  
  <pre><code>precision highp float;
  
  in vec3 in_Position; //declare position
  in vec3 in_Color;
  
  // mvpmatrix is the result of multiplying the model, view, and projection matrices */
  uniform mat4 mvpmatrix;
  out vec3 ex_Color;
  
  void main(void) {
  
  // Multiply the mvp matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
  
  gl_Position = mvpmatrix * vec4(in_Position, 1.0);
  
  ex_Color = in_Color;
  
  }
  </code></pre>
  
  <p>and my Fragment file</p>
  
  <pre><code>#version 330
  precision highp float;
  
  in vec3 ex_Color;
  out vec4 gl_FragColor;
  
  void main(void) {
  
  gl_FragColor = vec4(ex_Color,1.0);
  
  }
  </code></pre>
  
  <p>Now I know that I need to pass the normals to the vertice and fragment shader, but how do I do that and how/where do I implement the light calculations, linear interpolation??
  Thanks</p>