RayTracing / raytracing.github.io

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

Importance sampling #74

Open aromanro opened 5 years ago

aromanro commented 5 years ago

Hi,

Thank you for the books, they were very helpful! I signalled an issue on the blog, but you might not visit it often, so I'll put it here, too.

I implemented a ray tracer with help from your books and blog. I think I found an issue with the importance sampling. I'll try to describe it here in words.

The idea is that there is a list of objects that are 'important' (like the light in your book). Now, the same object also exists in the scene. It might happen that both the origin point and the generated target point to be on the same object, which for a rectangle for example means along the surface. But the pdf value for such a vector is zero, which means a division by zero. In my code I just checked for such a case and tried sampling again if that was the case, in a loop until success.

petershirley commented 5 years ago

Thst is a good point! Good catch thank you.

Not many people have made it that far— I’d enjoy seeing your pictures.

Pete

On Fri, Oct 19, 2018 at 7:55 AM Adrian Roman notifications@github.com wrote:

Hi,

Thank you for the books, they were very helpful! I signalled an issue on the blog, but you might not visit it often, so I'll put it here, too.

I implemented a ray tracer with help from your books and blog. I think I found an issue with the importance sampling. I'll try to describe it here in words.

The idea is that there is a list of objects that are 'important' (like the light in your book). Now, the same object also exists in the scene. It might happen that both the origin point and the generated target point to be on the same object, which for a rectangle for example means along the surface. But the pdf value for such a vector is zero, which means a division by zero. In my code I just checked for such a case and tried sampling again if that was the case, in a loop until success.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/petershirley/raytracingtherestofyourlife/issues/6, or mute the thread https://github.com/notifications/unsubscribe-auth/ACq6ipEeywl9Jdf6MXNWB7NiG7xBg57Sks5umdmygaJpZM4XwaB_ .

aromanro commented 5 years ago

I'll let you know when I'll put the code on GitHub. I still have some things to implement, right now I'm up to loading models from obj files, but no colours or textures yet are applied (from the obj file, of course I can set colours and so on on the composite object by coding). Here is one of the images I uploaded on facebook, it's the typical 'monkey' made transparent in a Cornell box: https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FComputePhysics%2Fphotos%2Fa.583365435413199%2F583608115388931%2F%3Ftype%3D3

pavlus commented 4 years ago

while not always it will be exactly zero, sometimes it may be some very small number, weighting sample up to thoundes of times more than any other, for me its usually means that there will be lots of overly bright pixels, and I could only get clear images once clamped scattering_pdf / pdf_val to 0.0..1.0, otherwise it doesn't converge.

Upd: found unrelated issue in my code that was leading to lots of bright spots.

aromanro commented 4 years ago

Depends on how you implemented it, I guess.

I don't recall exactly the details, but for the case when the both the origin and the target were on the same rectangle, the Hit call probably in my case returned false and in consequence the pdfValue call returned 0.

I think there might exist some issues even if you have the Hit test return true for such a case (but why? it's a ray that's parallel to the surface, it should not 'hit' the surface), because then you have the scalar product between a vector that is parallel with the surface and the normal to the surface (to get the cosine). That one will be zero and as such, you get a division by zero, this time in the pdfValue.

See from line 64 in my code, here: https://github.com/aromanro/RayTracer/blob/master/RayTracer/Scene.h pdfValue for a rectangle is here, at line 87: https://github.com/aromanro/RayTracer/blob/master/RayTracer/Rectangle.h

Taylor-Reid commented 1 year ago

@pavlus Can you add any details about your "Upd: found unrelated issue in my code that was leading to lots of bright spots.", I'm having a similar problem bright pixels and non-convergence, and hope that your fix might help me too.