Why in my rays of the sphere are there dark lines when illuminated by several light sources?

I have a simple raytracer that only works until the first intersection. The scene looks normal with two different light sources, but when both lights are in the scene, there are dark shadows where the illuminated area is from one end, even if it is in the middle of the illuminated area from the other light source (especially noticeable on the green ball). The transition from the “area illuminated by both light sources” to the “area illuminated by only one light source” seems a bit darker than the “area illuminated by one light source”.

Hosted by imgur.com

Code to which I add lighting effects:

// 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 do it right?

+3
4

, Mach.

, . "" , , , . , . -.

+7

. , , . , .

+3

; , .

0
source

Are you sure your lighting directions are normal?

It might be worth it to drop a statement there.

0
source

Source: https://habr.com/ru/post/1741539/


All Articles