Ray tracer reflections grainy

2019-08-06 02:10发布

I just implemented reflections in my ray tracer, here is the code that handles the reflections, however i have all my code uploaded to a github repository for better reading:

Color finalColor = closestObjectMaterial.GetColor() * AMBIENTLIGHT; // Add ambient light to the calculation

// Reflections
if(closestObjectMaterial.GetSpecular() > 0 && closestObjectMaterial.GetSpecular() <= 1)
{
    Vector scalar = closestObjectNormal * (closestObjectNormal.Dot(intersectingRayDir.Negative()));
    Vector resultantReflection = intersectingRayDir.Negative() + ((scalar + (intersectingRayDir)) * 2);
    Vector reflectionDirection = resultantReflection.Normalize();

    Ray reflectionRay(intersectionRayPos, resultantReflection);

    // determine what the ray intersects with first
    std::vector<FPType> reflectionIntersections;
    for(auto sceneObject : sceneObjects)
    {
        reflectionIntersections.push_back(sceneObject->GetIntersection(reflectionRay));
    }

    int closestObjectWithReflection = ClosestObjectIndex(reflectionIntersections);

    if(closestObjectWithReflection != -1)
    {
        // reflection ray missed everthing else
        if(reflectionIntersections[closestObjectWithReflection] > TOLERANCE)
        {
            // determine the position and direction at the point of intersection with the reflection ray
            // the ray only affects the color if it reflected off something
            Vector reflectionIntersectionPosition = intersectionRayPos + (resultantReflection * (reflectionIntersections[closestObjectWithReflection]));
            Vector reflectionIntersectionRayDirection = resultantReflection;
            Color reflectionIntersectionColor = GetColorAt(reflectionIntersectionPosition, reflectionIntersectionRayDirection, sceneObjects, closestObjectWithReflection, lightSources);
            finalColor += (reflectionIntersectionColor * closestObjectMaterial.GetReflection());
        }
    }
}

I'm getting these grainy artifacts on all reflections (this is a 16k resolution render zoomed in):

enter image description here

However it's even more obvious on lower resolutions like 1920x1080:

enter image description here

enter image description here

1条回答
做个烂人
2楼-- · 2019-08-06 02:36

I think the problem is that the reflection Ray hits itself. I didn't recompile the code to confirm this. You might try adding some offset to the start position of reflection ray.

Vector offset = resultantReflection * 0.001;
Ray reflectionRay(intersectionRayPos + offset, resultantReflection);
查看更多
登录 后发表回答