To give context to the question, I am reading Peter Shirley's Ray Tracing in one weekend. In his code for sphere/ray intersection, when the quadratic formula returns only 1 solution, he discards it, which strikes me as odd. Is there any graphics theory that support it or is it just his personal preference?

Here is the link to the code I am talking about on github. https://github.com/petershirley/raytracinginoneweekend/blob/master/sphere.h

  • 3
    (1) For a random ray, this happens with probability zero, so it doesn't really matter what you do here. (2) If the ray meets the sphere at exactly 1 point, it is tangent to the sphere, so the reflected ray will be in exactly the same direction, and it is as if there was no intersection. – Rahul Oct 7 at 7:19
up vote 7 down vote accepted

It is mainly personal preference, but it does have some logic behind it which would make it a better option. It should not have any big impact on your final image, though.

When the discriminant is 0, then that means that there is only one solution. For a sphere, when there is only one solution, that means that the ray is tangent to the sphere. When a line or ray is tangent to a sphere, that means that there is a 90° angle between the line/ray and the normal of the sphere at the intersection. When comparing that situation to ray-triangle and ray-plane intersections, they always ignore the hit when it has an angle of 90° with the normal. This is because the ray would then not really be able to hit it, and in real life you cannot see an infinitely thin plane or triangle that is perfectly parallel to your viewing direction.

Discarding hits when the angle is 90° could also help with shading. Depending on the shader, a 90° angle might always return black or start giving funky results, while when the angle is less than 90° it is just perfectly fine. Also as @Rahul has said, if you would calculate a reflected ray, it would end up in the same direction as your original ray and with most shaders that reflection ray would contribute to 100% of the ray's colour and not be coloured, so it would give the exact same colour as if you just discarded the ray.

The thing that makes this basically personal preference, is the fact that it would practically not happen that a ray would hit the sphere perfectly tangent, in an average render engine (as Rahul has also said). When looking at a sphere from a certain point, there would be a single circle with an infinitely small thickness that could create the occurrence of the discriminant being 0.

In short, it basically does not matter a single bit if you discard those hits or not.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.