Why do my raytraced spheres have dark lines when lit with multiple light sources?

Posted by Curyous on Stack Overflow See other posts from Stack Overflow or by Curyous
Published on 2010-04-16T23:27:09Z Indexed on 2010/04/16 23:33 UTC
Read the original article Hit count: 276

Filed under:
|

I have a simple raytracer that only works back up to the first intersection. The scene looks OK with two different light sources, but when both lights are in the scene, there are dark shadows where the lit area from one ends, even if in the middle of a lit area from the other light source (particularly noticeable on the green ball). The transition from the 'area lit by both light sources' to the 'area lit by just one light source' seems to be slightly darker than the 'area lit by just one light source'.

Hosted by imgur.com

The code where I'm adding the lighting effects is:

// trace lights
        for ( int l=0; l<primitives.count; l++) {

            Primitive* p = [primitives objectAtIndex:l];
            if (p.light) 
            {
                Sphere * lightSource = (Sphere *)p;

                // calculate diffuse shading
                Vector3 *light = [[Vector3 alloc] init];
                light.x = lightSource.centre.x - intersectionPoint.x;
                light.y = lightSource.centre.y - intersectionPoint.y;
                light.z = lightSource.centre.z - intersectionPoint.z;

                [light normalize];

                Vector3 * normal = [[primitiveThatWasHit getNormalAt:intersectionPoint] retain];
                if (primitiveThatWasHit.material.diffuse > 0)
                {
                    float illumination = DOT(normal, light);
                    if (illumination > 0)
                    {
                        float diff = illumination * primitiveThatWasHit.material.diffuse;
                        // add diffuse component to ray color
                        colour.red += diff * primitiveThatWasHit.material.colour.red * lightSource.material.colour.red;
                        colour.blue += diff * primitiveThatWasHit.material.colour.blue * lightSource.material.colour.blue;
                        colour.green += diff * primitiveThatWasHit.material.colour.green * lightSource.material.colour.green;
                    }
                }
                [normal release];
                [light release];
            }
        }

How can I make it look right?

© Stack Overflow or respective owner

Related posts about raytracing

Related posts about objective-c