Shadow rays in Raytracing After spending a few days making very little headway with a simple Raytracing program that implements Phong illumination (with shadows and no attenuation), I'm convinced I've made a logic error that I'm overlooking.  

As I understand it, the pseudo-code should look like this:

ambient light
for each light source
    direction of light = light point - point on the object
    test for an intersection starting at the point on the object, until you hit something
    if (the intersection test returned a distance &gt; distance from point on object to light point)
    (do  diffuse and specular shading)


A one quick thing to mention about my implementation: the intersection test will return 9999999 if there is no intersection.

Some of the things I've tried are: inverting the direction of the shadow ray - no effect. Making the comparison intersection distance - light distance > (some small epsilon value) - no change.  Manually moving the light source to the opposite side - the specular reflection moved to a new spot, but otherwise, no change.   

Here's a picture of my most recent output, I'm trying to get a shadow in the bottom left corner of the light gray plane.  



Any help would be greatly appreciated.  

Here's the relevant part of my code:

    //iterate through all lights
    if (myLights.size() &gt; 0)
    {
        for (int i = 0; i &lt; myLights.size(); i++)
        {
            //calculate the direction of the light
            glm::vec3 ldir = (myLights[i].position - surfacePoint);
            //this the length of the distance between the light and the surface
            float ldist= sqrt(pow(ldir.x, 2.0f) + pow(ldir.y, 2.0f) + pow(ldir.z, 2.0f)); 
//check if the light can see the surface point
            float intersections = myObjGroup-&gt;testIntersections(surfacePoint, normalize(ldir));
            if (abs(intersections-ldist)  &gt; 0.0000001f)
            {
                //diffuse and specular shading have been cut for brevity
            }
        }
    }


edit: New image:
Your if condition makes me suspicious. You should include the diffuse and specular shading if the intersection test didn't hit an object; that is, if intersections &gt; ldist. So, your code should look as follows:

if (intersections &gt; ldist)
{
    colour += diffuse(...);
    colour += specular(...);
}


Your comparison with 0.0000001f suggests that you've also been having trouble with self-intersections (that is, with the ray intersecting the object being shaded). In a one-hit tracer, you can't fix that problem by simply discarding the intersection, because you still don't know whether the shadow ray would have intersected another object further along. To avoid self-intersections, you need to push the start point of the ray along its direction by epsilon:

glm::vec3 direction = normalize(ldir);
glm::vec3 start = surfacePoint + epsilon * direction;
float intersections = myObjGroup-&gt;testIntersections(start, direction);


One more thing: don't forget to test that the light is in front of the surface that you're illuminating. You can do this with the dot product of ldir and the surface normal: if the dot product is positive, the light is on the correct side; if it's negative, the light is behind the surface and thus shadowed before you even cast a ray.