RayTracing/raytracing.github.io

Book 3.12.5: Calling `hittable_list::random()` on empty `hittable_list` causes segmentation fault

Opened this issue · 0 comments

Summary

When using the final code from book 3 to render a scene lacking explicitly-declared light sources (eg. any scene from book 1), it is natural to camera::render() passing an empty hittable_list as the lights argument. However, this causes a segmentation fault, because the current implementation of hittable_list::random() assumes the list to contain at least one member:

class hittable_list : public hittable {
  public:
    ...

    vec3 random(const point3& origin) const override {
        auto int_size = int(objects.size());
        return objects[random_int(0, int_size-1)]->random(origin); // if int_size is 0, this index is negative
    }
}

Proposed fix

There are several fixes possible here. Perhaps the simplest fix is to make camera::render() take a hittable_list of lights, and then only construct a mixture PDF in ray_color if the list of lights is non-empty.

This method would obviously not fix crashes in the event that one of the elements of the lights hittable_list is itself an empty hittable_list (more generally that other empty hittable_lists appear further down the scene tree of lights), but this is admittedly a strange contingency to guard against.