-
Notifications
You must be signed in to change notification settings - Fork 904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Book 3 / ch 12: Problem in sphere pdf value when origin is inside the sphere #470
Comments
Generally, in book 1, the The general solution to dealing with
|
Good catch, though, most people don't get this far into the book |
Thomas, can you elaborate on this?
I'm not seeing how a zero return from |
To be exact, I made a mistake when I originally wrote the issue. I think in this case you will get This needs verifying, but I think what can happen is the following.
To avoid this you need the guarantee that for every geometry, any vector returned by that geometry's I run some tests in my code for all other geometries included in the books and I believe this condition holds for them. It's only spheres where I found this issue. |
I'm not sure this is the correct fix. What's wrong with infinite components? If instead of zero you got a small value, then you can end up with components of, say, a million. The result is the same — light intensity is clamped to 1.0 for each component, and then mapped to 255 on output. |
I agree. For this issue clamping to [0.0, 1.0] is sufficient to solve the problem and the occurrence of these cases is infrequent enough that it doesn't have a visible impact on the image. I have done the same fix on my implementation. The issue I reported above has to do more with rays originating inside the sphere which is a different problem and which in my code produced a bad image even with clamping. I added the last two paragraphs of the original issue description as something I noticed on the same code, but they're not directly related with the issue. I don't think I made that very clear. Sorry about that. |
Sounds good. I agree that stamping out NaNs is worthwhile. |
In chapter 12 of book 3 where
sphere::pdf_value
andsphere::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 thetmin
passed tohit
. This causessphere::pdf_value
to return 0.0 since hit doesn't return anything which causes aNaN
due to a division by zero incolour
. 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 returnemitted
in this case.The text was updated successfully, but these errors were encountered: