Ray-tracing the Cornell Box results in really inconsistent image So I have a vector of glm::vec3 containing the triangles for the classic Cornell Box called triangles. The Draw method casts a ray for each pixel on the screen and calls ClosestIntersection which returns true or false and updates a struct containing things like colour and the point of that intersection. Then I simply draw the pixels based on the colours (black for no intersection). 

The result is very inconsistent and I can't find the error:


void Draw()
{
    if (SDL_MUSTLOCK(screen))
        SDL_LockSurface(screen);

    // Loop through each pixel and cast ray
    for (int y = 0; y&lt;SCREEN_HEIGHT; ++y)
    {
        for (int x = 0; x&lt;SCREEN_WIDTH; ++x)
        {
            vec3 dir(x - SCREEN_WIDTH / 2, y - SCREEN_HEIGHT / 2, focalLength);
            Intersection isect;
            if (ClosestIntersection(cameraPos, dir, triangles, isect))
            {
                vec3 color = triangles[isect.triangleIndex].color;
                PutPixelSDL(screen, x, y, color);
            }
            else
            {
                PutPixelSDL(screen, x, y, vec3(1, 1, 1));
            }
        }
    }

    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);

    SDL_UpdateRect(screen, 0, 0, 0, 0);
}


This is the function called from Draw:

bool ClosestIntersection(vec3 start,
    vec3 dir,
    const vector&lt;Triangle&gt;&amp; triangles,
    Intersection&amp; closestIntersection)
{
    bool foundIntersection = false;

    // Loop through all the triangles and check for intersection with ray
    for (size_t i = 0; i &lt; triangles.size(); i++)
    {
        vec3 v0 = triangles[i].v0;
        vec3 v1 = triangles[i].v1;
        vec3 v2 = triangles[i].v2;
        vec3 e1 = v1 - v0;
        vec3 e2 = v2 - v0;
        vec3 b = start - v0;
        mat3 A(-dir, e1, e2);
        vec3 x = glm::inverse(A) * b;

        // Check that found scalars are within bounds
        if ((x.x &gt;= 0) &amp;&amp; (x.y &gt; 0) &amp;&amp; (x.z &gt; 0) &amp;&amp; (x.y + x.z &lt; 1))
        {
            if (foundIntersection)  // If there's a previvous intersection for this ray check that new one is closer to camera
            {
                if (glm::distance(start, x) &lt; closestIntersection.distance)
                {
                    closestIntersection.distance = glm::distance(start, x);
                    closestIntersection.triangleIndex = i;
                    closestIntersection.position = x;
                }
            }
            else
            {
                foundIntersection = true;
                closestIntersection.distance = glm::distance(start, x);
                closestIntersection.triangleIndex = i;
                closestIntersection.position = x;
            }
        }
    }
    return foundIntersection;
}
Two symptoms

There appear to be two problems with the image.


The background is showing through along the line between adjacent triangles.
The colour displayed is not always from the closest intersection.


Note that the background colour is white, rather than black, due to the line:

                PutPixelSDL(screen, x, y, vec3(1, 1, 1));


Why this background shows through at the triangle edges I do not know. However, the second problem is identified below, so I recommend you fix that first and then the first problem can be analysed in isolation without the additional distractions.



Cause of the problem with ClosestIntersection

The ClosestIntesection function returns true when an intersection is found, and updates an Intersection object called closestIntersection. However, the intersection stored here is not always the closest to the camera.

The reason for this is here:

                closestIntersection.distance = glm::distance(start, x);


This appears in two places so it will need to be fixed in both.

start is cameraPos, presumably in world coordinates. x appears to be in the local coordinates of the triangle being intersected (one of the triangle vertices being the origin). The distance between two points in different coordinate systems is meaningless, so that in different regions of a triangle it will appear to be behind or in front of another triangle for no immediately apparent reason.

If x is first converted to world coordinates then the distance will be correct and the intersection chosen will be the closest to the camera.