cpp-raytracer/hittable_list.hpp
2021-08-29 16:17:13 +02:00

52 lines
1.1 KiB
C++

#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H
#include "hittable.hpp"
#include "sphere.hpp"
#include <memory>
#include <vector>
using std::shared_ptr;
using std::make_shared;
template <typename T = sphere>
struct hittable_list {
/* Attributes */
std::vector<T> objects;
/* Constructors */
hittable_list () {}
hittable_list(T object) { add(object); }
/* Methods */
void clear() { objects.clear(); }
void add (T h) { objects.push_back(h); }
bool hit(const ray& r, float t_min, float t_max, hit_record& rec);
};
template <typename T>
bool hittable_list<T>::hit(const ray& r, float t_min, float t_max, hit_record& rec)
{
rmt_ScopedCPUSample(HittableList_Hit, RMTSF_Aggregate);
hit_record temp_rec;
bool hit_anything = false;
float closest_so_far = t_max;
uint32_t s = objects.size();
for (uint32_t i = 0; i < s; ++i)
{
T *object = &objects[i];
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