Skip to content
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.12.5: Calling hittable_list::random() on empty hittable_list causes segmentation fault #1669

Open
dariotrinchero opened this issue Jan 10, 2025 · 0 comments

Comments

@dariotrinchero
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant