Search code examples
c++shadowraytracinglight

Ray Tracing: Shadows from Multiple Lights


[Edit] As is typical in programming, I figured it out shortly after posting! see my answer if you're interested :)

I'm working on a ray tracer in C++ and wanted some help with something. I've got 2 lights in my scene, a point light and a directional light, and a bunch of spheres (along with a plane acting as the "floor").

If i run the ray tracer with either of the lights present (but the other light not present), it makes shadows as expected (see images below).

Directional light shadows image

Point light shadows image

The trouble is, when I run my ray tracer with both lights present, only the point light shadows show up, I can tell the light is "on" because the scene is brighter:

See my code below for detecting shadows:

bool Scene::shadowtrace( Ray &ray, double t )
{
    Object *obj = obj_list;
    Light *lt = light_list;
    Vector v1, v2; // hit -> light vector
    Hit   hit;

    Vertex intersect = (ray.position( t ));
    intersect.plus( ray.D, -0.001 ); // offset  intersection ever so slightly away from object, to avoid self-shadowing
    v1.set( 0.0, 0.0, 0.0 );
    v2.set( 0.0, 0.0, 0.0 ); // initialise

    while (lt != (Light *)0)
    {
        Ray shadowRay;
        shadowRay.P = (intersect);

        Vertex lightPos = Vertex( 0.0, 0.0, 0.0, 0.0 );
        lt->getPosition( lightPos ); // sets value of lightPos

        if (lightPos.x > T_LIMIT) // If set absurdly high, we're dealing with a directional light
        {
            lt->getDirection( v1 ); // sets v1 to light direction (reversed)
            v1.normalise( );
            shadowRay.D = v1; // set hit-to-light vector as shadowray direction

            while (obj != (Object *)0)
            {
                if (obj->intersect( shadowRay, &hit ) == true)
                {
                    if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number
                    {
                        return true; // ray hits an onject, and the object occurs before the light
                    }
                }
                obj = obj->next( );
            }
        }
        else    // otherwise, it's a point light :)
        {
            v1 = (lightPos.minus( intersect )); // find vector from intersection to light
            v2 = v1; // keep un-normalised version for preventing mis-shadowing from objects behind the light source
            v1.normalise( );
            shadowRay.D = v1; // set ray direction to hit-to-light vector

            while (obj != (Object *)0)
            {
                if (obj->intersect( shadowRay, &hit ) == true)
                {
                    if (!((hit.t * hit.t) > (v2.lengthSq( ))))  // Check hit.t against magnitude of (un-normalised) intersection-to-light vector
                        if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number
                        { // Used hit.t^2 to avoid having to SQRT the length. Is acceptable for comparisons
                            return true; // ray hits an onject, and the object occurs before the light
                        }

                }

                obj = obj->next( );
            }
        }

        lt = lt->next( );
    }

    return false;
}

If a shadow is detected, only ambient light is attributed to the point, otherwise ambient + diffuse is attributed (I haven't gotten round to adding specular yet).

Any suggestions would be great!


Solution

  • False alarm folks! I Figured it out!!

    before each object list loop I added:

    obj = obj_list;
    

    to reset to the first object, this solved the problem :)