我刚刚在我的光线跟踪器中实现了反射,这里是处理反射的代码,但是我将所有代码上传到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:
)上它更为明显答案 0 :(得分:1)
我认为问题在于Ray反射自身。我没有重新编译代码来确认这一点。您可以尝试在反射光线的起始位置添加一些偏移。
Vector offset = resultantReflection * 0.001;
Ray reflectionRay(intersectionRayPos + offset, resultantReflection);