Cache Staheli Cache Staheli - 24 days ago 6
Java Question

Correctly raytracing reflective surfaces

I'm having some trouble with raytracing reflections.

Here is what I am expecting to see:

Expected

But this is what I am actually seeing:
Actual

Now, my basic understanding of how to get the new direction of the
reflection ray is like this:

Reflection direction equation

Here is my code that emulates that:

public Color calculateIlluminationModel(Vector normal, boolean isInShadow, Scene scene, Ray ray, Vector intersectionPoint)
{
if (isInShadow)
{
return getColorInShadow(scene);
}
else
{
Vector originalDirection = ray.getDirection();

Vector reflectionVector = originalDirection
.subtract(normal
.multiply(2)
.multiply(originalDirection.dotProduct(normal)
)
)
.normalize();
Ray reflectionRay = Ray.translateRayByEpsilon(new Ray(intersectionPoint, reflectionVector));
return scene.getRayColor(reflectionRay);
}
}


But the sphere never appears to correctly reflect anything. Is there something wrong with the way I am calculating the reflection rays?

Note:
scene.getRayColor(reflectionRay);
is the ray tracing component to determine the color that the ray hits. I believe it isn't the problem, but if you think it's necessary to show that piece, let me know.

Answer

Your reflection math appears correct. The image appears filled with a lot of shadow color. Check to see what happens if you fudge isInShadow to be false, that should help narrow it down.

Also make sure your normals point the right direction and that your direction vectors are all normalized (you said they are, but if anybody else is having a similar issue, it's worth double checking).