-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittable_list.h
42 lines (30 loc) · 1.02 KB
/
hittable_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef RAYTRACER_HITTABLE_LIST_H
#define RAYTRACER_HITTABLE_LIST_H
#include "hit_record.h"
#include "hittable.h"
#include <memory>
#include <vector>
using std::make_shared;
using std::shared_ptr;
class HittableList : public Hittable {
public:
HittableList() = default;
explicit HittableList(const shared_ptr<Hittable> &object) { add(object); }
void clear() { objects.clear(); }
void add(const shared_ptr<Hittable> &object) { objects.push_back(object); }
[[nodiscard]] std::optional<HitRecord> hit(const Ray &r, double tMin, double tMax) const override;
public:
std::vector<shared_ptr<Hittable>> objects;
};
std::optional<HitRecord> HittableList::hit(const Ray &r, double tMin, double tMax) const {
std::optional<HitRecord> result;
auto nearestHitDist = tMax;
for (const auto &object: objects) {
if (auto rec = object->hit(r, tMin, nearestHitDist)) {
nearestHitDist = rec->t;
result = rec;
}
}
return result;
}
#endif//RAYTRACER_HITTABLE_LIST_H