光线追踪器反射有颗粒感

Ray tracer reflections grainy

我刚刚在我的光线追踪器中实现了反射,这里是处理反射的代码,但是我已将所有代码上传到 github repository 以便更好地阅读:

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());
        }
    }
}

我在所有反射上都得到了这些颗粒状的伪影(这是放大的 16k 分辨率渲染):

然而,在 1920x1080 等较低分辨率下更明显:

我认为问题是反射射线击中了自己。我没有重新编译代码来确认这一点。您可以尝试在反射光线的起始位置添加一些偏移量。

Vector offset = resultantReflection * 0.001;
Ray reflectionRay(intersectionRayPos + offset, resultantReflection);