Search code examples
pythongraphicsraytracing

Visual bug in the raytracer?


scene

I do not know if this is a bug or normal behavior, but I am confused by an elongated object in the reflection of a red sphere, what is it?

My scene:

s = Scene(
    [
        Light(
            type_=LightType.ambient, 
            intensity=vec3(0.2, 0.2, 0.2)
        ),
        Light(
            type_=LightType.point, 
            intensity=vec3(0.6, 0.6, 0.6), 
            position=vec3(2, 1, 0)
        ),
        Light(
            type_=LightType.directional,
            intensity=vec3(0.2, 0.2, 0.2), 
            direction=-vec3(1, 4, 4)
        ),
    ],
    [
        Triangle(vec3(-1.5, 0.5, 5.2), vec3(1.5, 0.5, 5.2), vec3(-1.5, 2, 4), vec3(0, 0, 0), 500, 1),
        Triangle(vec3(1.5, 0.5, 5.2), vec3(1.5, 2, 4), vec3(-1.5, 2, 4), vec3(0, 0, 0), 500, 1),
        Sphere(
            color=u8vec3(255, 0, 0), 
            radius=1, 
            center=vec3(0, -1, 3),
            specular=500,
            reflective = 0.2
        ),
        Sphere(
            color=u8vec3(0, 0, 255), 
            radius=1, 
            center=vec3(2, 0, 4),
            specular=500,
            reflective=0.3
        ),
        Sphere(
            color=u8vec3(0, 255, 0), 
            radius=1, 
            center=vec3(-2, 0, 4),
            specular=10,
            reflective=0.4 
        ),
        Sphere(
            color=u8vec3(255, 255, 0), 
            radius=5000, 
            center=vec3(0, -5001, 0),
            specular=1000,
            reflective=0.5
        )
    ]
)

full code is here: https://github.com/linux-admin0001/raytracer


Solution

  • Fixed version

    I'm fixed this bug.

    My mistake was that the ray intersected with the object without taking into account the direction of the ray, let's say the ray was directed forward (0, 0, 1), but it also intersected the object that was behind, I corrected this for triangles, for spheres I did not observe this

    if dot(D-O, object_.normal) > 0: continue
    

    As you can see, in the final image I got rid of many distortions that seemed to me to be serviceable