title: Texture mapping in a ray tracing for sphere in C++
body: <p><img src="https://i.stack.imgur.com/aHLr4.png" alt=""></p>
  
  <p>I have set up a simple ray tracing in C++. I wanted to add the texture mapping to the sphere. It basically just map the texture from a PPM file to the sphere. Below are my code for the part.</p>
  
  <pre><code>//Call shaderay from trace ray function
  // index_of_winning_object = index of scene object array
  // point = intersection point
  Color shadeRay (int index_of_winning_object, Vect point, Ray ray){
  
  double final_index2;    
  
  //if no intersection, return the background color (double confirm)
  if (index_of_winning_object == -1) {
      return bkgcolor;
  }else{
  
      Vect c = scene_objects[index_of_winning_object].getSphereCenter();
      Vect p = point;
  
      //Normal to the intersection point and sphere
      Vect N = p.vectSub(c).normalize();
  
      //Calculating the texture coordinate for a sphere
      double temp = acos(N.getVectZ());
      double temp2 = atan2(N.getVectY(), N.getVectX());
      //u,v 
      double v = temp / 3.141592653589793;
  
      if (temp2 &lt; 0) {
          temp2 = temp2 + (2 * 3.141592653589793);
      }
  
      double u = temp2 / (2 * 3.141592653589793);
  
      // get_ppm_width = width of the sample texture ppm file like (picture.ppm)
      // get_ppm_height = height of the sample texture ppm file like (picture.ppm)
  
      int width = u * get_ppm_width;
      int height = v * get_ppm_height;        
  
  
      //calculating the pixel of the ppm file, I store the pixel in get_array in RGB struct
      // ___ ___ ___ ___ ___ ___ ___
      //  0   1   2   3   4   5   6
      //  7   8   9  10   11  12  13
      // ___ ___ ___ ___ ___ ___ ___
      // above is the example of the get_array. get_array is a one dimensional array of RGB struct
  
      int px = height + (width * get_ppm_width);
  
      // Get the color from the get_array 
      Color final (get_array[px].r, get_array[px].g, get_array[px].b );
  
      return final;
    }
  }
  </code></pre>
  
  <p>Can somebody please let me know what I have done wrong for the shaderay function? Thanks a lot
  Top left is the picture of the sphere I got. While the bottom the texture of world map.<img src="https://i.stack.imgur.com/pToYz.png" alt="enter image description here"></p>