title: Improper reflection in recursive ray tracer
body: <p>I'm implementing a recursive ray tracer with reflection. The ray tracer is currently reflecting areas that are in shadow, and I don't know why. The shadow aspect of the ray tracer works as expected when the reflective code is commented out, so I don't think that's the issue.</p>
  
  <pre><code>Vec Camera::shade(Vec accumulator,
                    Ray ray,
                    vector&lt;Surface*&gt;surfaces,
                    vector&lt;Light*&gt;lights,
                    int recursion_depth) {
  
  if (recursion_depth == 0) return Vec(0,0,0);
  
  double closestIntersection = numeric_limits&lt;double&gt;::max();
  Surface* cs;
  for(unsigned int i=0; i &lt; surfaces.size(); i++){
      Surface* s = surfaces[i];
  
      double intersection = s-&gt;intersection(ray);
  
      if (intersection &gt; EPSILON &amp;&amp; intersection &lt; closestIntersection) {
          closestIntersection = intersection;
          cs = s;
      }
  }
  if (closestIntersection &lt; numeric_limits&lt;double&gt;::max()) {
  
      Point intersectionPoint = ray.origin + ray.dir*closestIntersection;
      Vec intersectionNormal = cs-&gt;calculateIntersectionNormal(intersectionPoint);
  
      Material materialToUse = cs-&gt;material;
  
      for (unsigned int j=0; j&lt;lights.size(); j++) {
  
          Light* light = lights[j];
  
          Vec dirToLight = (light-&gt;origin - intersectionPoint).norm();
          Vec dirToCamera = (this-&gt;eye - intersectionPoint).norm();
  
  
          bool visible = true;
          for (unsigned int k=0; k&lt;surfaces.size(); k++) {
              Surface* s = surfaces[k];
  
              double t = s-&gt;intersection(Ray(intersectionPoint, dirToLight));
  
              if (t &gt; EPSILON &amp;&amp; t &lt; closestIntersection) {
                  visible = false;
                  break;
              }
          }
  
          if (visible) {
              accumulator = accumulator + this-&gt;color(dirToLight, intersectionNormal,
                                               intersectionPoint, dirToCamera, light, materialToUse);
          }
  
      }
  
      //Reflective ray
      //Vec r = d − 2(d · n)n
      if (materialToUse.isReflective()) {
          Vec d = ray.dir;
          Vec r_v = d-intersectionNormal*2*intersectionNormal.dot(d);
          Ray r(intersectionPoint+intersectionNormal*EPSILON, r_v);
          //km is the ideal specular component of the material, and mult is component-wise multiplication
          return this-&gt;shade(accumulator, r, surfaces, lights, recursion_depth--).mult(materialToUse.km);
      }
      else
          return accumulator;
  }
  else
      return accumulator;
  }
  
  Vec Camera::color(Vec dirToLight,
                    Vec intersectionNormal,
                    Point intersectionPoint,
                    Vec dirToCamera,
                    Light* light,
                    Material material) {
  
  //kd I max(0, n · l) + ks I max(0, n · h)p
  
  Vec I(light-&gt;r, light-&gt;g, light-&gt;b);
  double dist = (intersectionPoint-light-&gt;origin).magnitude();
  I = I/(dist*dist);
  
  Vec h = (dirToLight + dirToCamera)/((dirToLight + dirToCamera).magnitude());
  
  Vec kd = material.kd;
  Vec ks = material.ks;
  
  Vec diffuse = kd*I*fmax(0.0, intersectionNormal.dot(dirToLight));
  
  Vec specular = ks*I*pow(fmax(0.0, intersectionNormal.dot(h)), material.r);
  
  return diffuse+specular;
  
  }
  </code></pre>
  
  <p>I've provided my output and the expected output. The lighting looks a bit different b/c mine was originally an .exr file and the other is a .png, but I've drawn arrows in my output where the surface should be reflecting shadows, but it's not.</p>
  
  <p><a href="https://i.stack.imgur.com/4YAws.png" rel="nofollow"><img src="https://i.stack.imgur.com/4YAws.png" alt="My output"></a></p>
  
  <p><a href="https://i.stack.imgur.com/AvR0Y.png" rel="nofollow"><img src="https://i.stack.imgur.com/AvR0Y.png" alt="Expected output"></a></p>