【发布时间】:2016-06-14 09:47:08
【问题描述】:
我刚刚在我的光线追踪器中实现了反射,这里是处理反射的代码,但是我已将所有代码上传到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 等较低分辨率下,这一点更加明显:
【问题讨论】:
标签: c++ graphics 3d rendering raytracing