Skip to content

Book 3 / ch 12: Problem in sphere pdf value when origin is inside the sphere #470

Open
@Dalamar42

Description

@Dalamar42

In chapter 12 of book 3 where sphere::pdf_value and sphere::random are introduced the code only handles the case where the origin is outside the sphere.

If the origin is inside the sphere then the ratio of (radius^2 / ||centre - origin||^2) will be greater than 1 so the square root will return a NaN and kill the pixel.

I noticed this when I was rendering the book 1 cover scene. I don't know for sure, but I suspect it was caused by a lambertian sphere intersecting a dielectric and having a ray scattered off of the lambertian be sent towards the enclosing dielectric.

My solution to this in my code was to say any ray has equal probability to hit the enclosing sphere and so to generate a uniformly random direction vector and return a pdf value of (1 / 4π) for it.

There is one more problematic case where the origin is exactly on the surface of the sphere and the random direction picked by sphere::random is such that the second intersection with the sphere is closer to origin than the tmin passed to hit. This causes sphere::pdf_value to return 0.0 since hit doesn't return anything which causes a NaN due to a division by zero in colour. Same thing can happen if origin is very close to the surface of the sphere.

This case seems to happen extremely rarely though so I handled it by acting as if no scattering had occurred and having colour just return emitted in this case.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions