RayTracing / raytracing.github.io

Main Web Site (Online Books)
https://raytracing.github.io/
Creative Commons Zero v1.0 Universal
8.83k stars 868 forks source link

Sphere normal calculation confusion #977

Closed dj2 closed 1 year ago

dj2 commented 2 years ago

When implementing the code in the book, at the point where the normal for the sphere is calculated I substituted a call to normalize the Vec3 instead of what the book has: rec.normal = (rec.p - center) / radius;. (This seemed like it made sense at the time as the radius and length would be the same).

This works, up until adding the inner glass sphere and then things go wrong. Eventually I realized that there is a subtle difference in that, with the glass sphere the inner sphere has a negative radius. This causes the normal to be in the opposite directions when dividing with radius vs calling normalize.

It maybe worth adding a note into the code where the normal for the sphere is first calculated that this is explicitly not using the normalize as we'll have negative radius spheres in the future and will cause the normals to go in the wrong direction if normalize is used.

TheRayTracer commented 2 years ago

This only seems an issue with section 10.5 - Modeling (sic) a Hollow Glass Sphere in Book 1. While a negative radius is a nice trick/hack to make hollow glass spheres, it's just that. There's nothing wrong with your method to normalise a sphere's surface normal and I also take this approach.

Perhaps you could explore adding a new material specifically for hollow glass spheres where the normal is flipped?

trevordblack commented 2 years ago

Modeling is the preferred spelling in the United States. Modelling is the preferred spelling elsewhere.

As the authors are American, we defer to American English (see color)

hollasch commented 1 year ago

Reviewing this, I believe that @dj2's comment is that normalize will not work with negative radius spheres because it would give you a vector into the object instead of away from the object ("inside" is the set of all points more than |radius| units away from the center). A sphere with negative radius has infinite volume, and the region outside a negative radius sphere is finite (the "bubble" of all points within |radius| of the center).

It just so happens that the optimization of dividing by the radius also yields correct normals, where just normalizing (P-C) would not.