toytracer/hittable_list.h

63 lines
1.6 KiB
C
Raw Normal View History

2020-06-03 22:15:32 +00:00
#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H
#include <memory>
#include <vector>
#include "hittable.h"
2020-06-08 21:37:10 +00:00
#include "bvh.h"
2020-06-03 22:15:32 +00:00
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;
2020-06-08 21:37:10 +00:00
bool bounding_box(double t0, double t1, Aabb& output_box) const;
Bvh_node generate_bvh(double t0, double t1);
2020-06-03 22:15:32 +00:00
private:
std::vector<std::shared_ptr<Hittable>> objects;
2020-06-08 21:37:10 +00:00
2020-06-03 22:15:32 +00:00
};
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;
}
2020-06-08 21:37:10 +00:00
bool Hittable_list::bounding_box(double t0, double t1, Aabb& output_box) const {
if (objects.empty()) return false;
Aabb temp_box;
bool first_box = true;
for (const auto& object : objects) {
if (!object->bounding_box(t0, t1, temp_box)) return false;
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
first_box = false;
}
return true;
}
Bvh_node Hittable_list::generate_bvh(double t0, double t1) {
return Bvh_node(objects, 0, objects.size(), t0, t1);
}
2020-06-03 22:15:32 +00:00
#endif // HITTABLE_LIST_H