1
我在光线追踪刚刚实施的思考,这里是处理反射的代码,但是我有我的所有代码上传到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的分辨率渲染放大):
但是它更明显较低分辨率1920×1080像:
我结束了使用这个,结果反射需要被归一化。 'Vector offset = reflectionDirection * 0.001; Ray reflectionRay(intersectionRayPos + offset,resultingReflection);' –
在你的评论中,你已经添加了 //添加了偏移来消除反射中的粒度效应 实际的问题是,你的光线从反射对象A因为它是距离射线开始位置最近的物体,所以被认为会自己击中。所以为了避免这种情况,我们将光线从物体A的表面稍微开始。 – codetiger