aboutsummaryrefslogtreecommitdiffstats
path: root/world.h
blob: 9723b78f9a9c48f5c14313d671dc4615a802b30a (plain)
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
#ifndef WORLD_H
#define WORLD_H

#include "sphere.h"

#include <limits>
#include <memory>
#include <optional>
#include <tuple>
#include <vector>

struct World
{
    std::vector<std::unique_ptr<Object>> objects;

    template<class T>
    void add(auto&&... args) {
        objects.emplace_back(new T(args...));
    }

    std::optional<std::pair<double, Object *>> hit(const ray& r) const {
        double closest = std::numeric_limits<double>::infinity();
        Object *sphere;

        for (const auto& o : objects) {
            if (auto t = o->hit(r, 0.001, closest); t) {
                closest = *t;
                sphere = o.get();
            }
        }

        if (closest != std::numeric_limits<double>::infinity())
            return std::pair {closest, sphere};
        else
            return {};
    }
};

#endif // WORLD_H