I just implemented reflections in my ray tracer, here is the code that processes reflections, however I have all my code loaded into the github repository for better reading:
Color finalColor = closestObjectMaterial.GetColor() * AMBIENTLIGHT;
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);
std::vector<FPType> reflectionIntersections;
for(auto sceneObject : sceneObjects)
{
reflectionIntersections.push_back(sceneObject->GetIntersection(reflectionRay));
}
int closestObjectWithReflection = ClosestObjectIndex(reflectionIntersections);
if(closestObjectWithReflection != -1)
{
if(reflectionIntersections[closestObjectWithReflection] > TOLERANCE)
{
Vector reflectionIntersectionPosition = intersectionRayPos + (resultantReflection * (reflectionIntersections[closestObjectWithReflection]));
Vector reflectionIntersectionRayDirection = resultantReflection;
Color reflectionIntersectionColor = GetColorAt(reflectionIntersectionPosition, reflectionIntersectionRayDirection, sceneObjects, closestObjectWithReflection, lightSources);
finalColor += (reflectionIntersectionColor * closestObjectMaterial.GetReflection());
}
}
}
I get these grainy artifacts in all reflections (this is an increase with a resolution of 16 thousand pixels):

However, this is even more obvious at lower resolutions, such as 1920x1080:


source
share