cpp-raytracer/hittable_list.hpp

46 lines
1 KiB
C++

#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H
#include "hittable.hpp"
#include <memory>
#include <vector>
using std::shared_ptr;
using std::make_shared;
struct hittable_list : hittable {
/* Attributes */
std::vector<shared_ptr<hittable>> objects;
/* Constructors */
hittable_list () {}
hittable_list(shared_ptr<hittable> h) { add(h); }
/* Methods */
void clear() { objects.clear(); }
void add (shared_ptr<hittable> h) { objects.push_back(h); }
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
};
bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& rec) const
{
hit_record temp_rec;
bool hit_anything = false;
double closest_so_far = t_max;
for (const auto& object : objects)
{
if (object->hit(r, t_min, closest_so_far, temp_rec))
{
hit_anything = true;
closest_so_far = temp_rec.t;
rec = temp_rec;
}
}
return hit_anything;
}
#endif