toytracer/hittable_list.h
2020-06-04 00:15:32 +02:00

40 lines
955 B
C++

#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H
#include <memory>
#include <vector>
#include "hittable.h"
class Hittable_list : public Hittable {
public:
Hittable_list() {}
Hittable_list(std::shared_ptr<Hittable> object) { add(object); }
void clear() { objects.clear(); }
void add(std::shared_ptr<Hittable> object) { objects.push_back(object); }
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const;
private:
std::vector<std::shared_ptr<Hittable>> objects;
};
bool Hittable_list::hit(const Ray &r, double tmin, double tmax, hit_record &rec) const {
hit_record temp_rec;
bool hit_anything = false;
auto closest_so_far = tmax;
for (const auto& object : objects) {
if (object->hit(r, tmin, closest_so_far, temp_rec)) {
hit_anything = true;
closest_so_far = temp_rec.t;
rec = temp_rec;
}
}
return hit_anything;
}
#endif // HITTABLE_LIST_H