title: Weird shadows cube raytracing
body: <p>This is my third question in this raytracing thing, but there's been progress :P So, I'm implementing a C++ ray tracer for my object oriented programming class, and so far I've implemented monochromatic spheres and planes with support to reflection and specular shading. This is an example of a thing I've done:</p>
  
  <p><img src="https://i.stack.imgur.com/YU0dh.jpg" alt="Whee pretty and shiny"></p>
  
  <p>Now I'm trying to implement general polyhedrons. I'm using a modified version of <a href="http://geomalgorithms.com/a13-_intersect-4.html" rel="nofollow noreferrer">this algorithm</a> to compute intersections with an arbitrary polyhedron of <em>nFaces()</em> faces, each of which being contained by a plane defined by <em>Vec Polyhedron::point(int face)</em> and <em>Vec Polyhedron::normal(int face)</em>:</p>
  
  <pre><code>Vec Polyhedron::intersect(Vec o, Vec d)
  {
      int face = nFaces();
      Vec ni(0,0,0), pi(0,0,0);
      unit te = -1;
      unit tl = -1;
      unit t = 0;
      unit N, D;
      Vec v = d.normal();
      int facein, faceout;
      for(int i = 0; i &lt; face; i++)
      {
          ni = normal(i);
          pi = point(i);
          N = ((pi - o)*ni);
          D = v*ni;
          if(D == 0 &amp;&amp; N &lt; 0)
              return o;
          if(D != 0)
          {
              t = N/D;
              if(t &gt; 0)
              {
                  if(N &lt; 0)
                  {
                      if(t &gt; te){
                          te = t;
                          facein = i;
                      }
                  }else{
                      if((tl == -1) || (t &lt; tl)){
                          tl = t;
                          faceout = i;
                      }
                  }
                  if((tl != -1) &amp;&amp; (tl &lt; te))
                      return o;
              }
          }
      }
      if(tl != -1)
      {
          if(te != -1)
          {
              v = v*te + o;
              return (v + normal(facein)*0.000000000001);
          }
          v = v*tl + o;
          return (v + normal(faceout)*0.000000000001);
      }
      return o;
  }
  </code></pre>
  
  <p>So I modified the scene with the balls by removing the balls and adding a red cube (the only type of Polyhedron I implemented was a Cuboid) and ran it. This was the result:</p>
  
  <p><img src="https://i.stack.imgur.com/gHpEh.jpg" alt="enter image description here"></p>
  
  <p>And I have absolutely no idea why. Any clues?</p>