diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2024-05-16 08:03:42 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2024-05-16 08:03:42 -0400 |
commit | fdc2d91285cd9548a3c95f1bdfb75c70ccfffff6 (patch) | |
tree | 65e460dccf3336228d8b0803e334c9ab2f4bf582 /world.h | |
parent | 549fe1388c9dd641c698d4a2ea0a1e6f754df534 (diff) |
abstract objects
Diffstat (limited to 'world.h')
-rw-r--r-- | world.h | 14 |
1 files changed, 8 insertions, 6 deletions
@@ -4,26 +4,28 @@ #include "sphere.h" #include <limits> +#include <memory> #include <optional> #include <tuple> #include <vector> struct World { - std::vector<Sphere> objects; + std::vector<std::unique_ptr<Object>> objects; + template<class T> void add(auto&&... args) { - objects.emplace_back(args...); + objects.emplace_back(new T(args...)); } - std::optional<std::pair<double, Sphere>> hit(const ray& r) const { + std::optional<std::pair<double, Object *>> hit(const ray& r) const { double closest = std::numeric_limits<double>::infinity(); - Sphere sphere; + Object *sphere; for (const auto& o : objects) { - if (auto t = o.hit(r, 0.001, closest); t) { + if (auto t = o->hit(r, 0.001, closest); t) { closest = *t; - sphere = o; + sphere = o.get(); } } |