raytracing.github.io icon indicating copy to clipboard operation
raytracing.github.io copied to clipboard

Book 3.12: Bug in final code? Black image when using lambert material for nearest object to camera in mixed density pdf approach

Open hollasch opened this issue 1 year ago • 1 comments

Discussed in https://github.com/RayTracing/raytracing.github.io/discussions/1240

Originally posted by 1vx-437312114 August 25, 2023 This is the result of the final code of book 3 (src/TheRestOfYourLife/main.cc ):

theRestOfYourLife_400

When i change the material of the glass sphere in the cornell_box() - function into a lambert diffuse material i get a nearly black image with basically only the light source visible when i render the image:

theRestOfYourLife

This is what it should look like (rendered for comparison in the brute force path tracer without the mixed density pdf approach):

cornell_box

I tried a few things e.g. replacing the sphere with a box or changing materials of the box inside the scene to lambert diffuse. My results so far are that whenever the object which is nearest to the camera is lambert diffuse the rendering breaks and i get the nearly black image from above as the result.

I came to this after i tried implementing the mixed density pdf approach from book 3 in my own path tracer where i tried to render the original cornell box (in which everything has a diffuse material) and i got the same bad results which got me trying to test the original code.

Here are the "changes" (just one line) in the cornell_box() - function of the main.cc file inside the folder src/TheRestOfYourLife/ for changing the glas sphere in the front to a blue colored diffuse sphere:

 hittable_list cornell_box() {
    ...
    shared_ptr<material> blue = make_shared<lambertian>(color(0.0, 0.0, 0.75));   // blue lambert diffuse
    //auto glass = make_shared<dielectric>(1.5);
    objects.add(make_shared<sphere>(point3(190,90,190), 90 , blue));  // changed from 'glass' to 'blue'

    return objects;
}

Has anyone any idea what is going wrong here or what i did wrong and how to fix this? Thanks in advance.

Here is the "modified" main.cc if anyone wants to test it:

main_cc.txt

hollasch avatar Sep 13 '23 21:09 hollasch

I used to get a similar result (only a nearly black image with basically only the light source visible), it is caused by calling wrong function hittable->pdf_value() rather then the expected function quad->pdf_value() in runtime. I fixed it by checking and modifying function parameter types carefully (using reference or pointer). You maybe should check whether the pdf_value() is override by the subclass of hittable and the pdf whether equal near "zero". Hope this helps.

LiuZengqiang avatar Sep 24 '23 02:09 LiuZengqiang