Ray tracing in One weeked: issue with metal reflection

Problem

I am following the book Ray tracing in one weekend , and I got stuck after the implementation of the material Metal. I checked my code many times, and it looks exactly like the code implemented by Peter Shirley. The problem is that, at this point, my code would need to generate this image:

Instead, my implementation results in this:

There is no reflection on the left and right sphere, even if the code looks fine. I am not posting the code here since many classes are involved, and I implemented it following strictly the book. Did someone have a similar problem? I can post relevant parts of code that can help.

Possible solution

The function of the reflection is not the problem, since it is very simple and it is exactly like the one proposed in the book. Also, when I set the sphere below (the floor) as Metal instead of Lambertian, it shows the reflections correctly:

This suggests that the problem is not the reflection function, nor the material.

• Try posting the relevant section where you calculate reflection. Are you using Monte-Carlo methods to simulate GI as well? The grey sphere is getting a greenish tint from the floor. Seems like color bleeding effect due to GI but simple raytracers don't do that. It could be that the reflection function is working partially. Feb 16 '19 at 13:40
• You'll have to post the relevant parts of your code. Shirley's code is available on github, and it works correctly if you compile it, thus even if you say that the code is the same, it most certainly is not. Feb 16 '19 at 15:09
• Note that it's not just the metal reflection that's wrong in your image, it's obvious that the middle (lambertian) sphere is also wrong. Feb 16 '19 at 17:34
• It seems like the normals on your spheres are wrong. Feb 17 '19 at 1:31

1 Answer

The problem was in the way I was calculating the function bool Sphere::hit(). The author eliminates some redundant 2's that cancel each other without explicitly showing it, and if you don't pay attention to this detail the code the hit function will not work as it is supposed to. In case someone else is stuck, that might be the problem.