You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
845 B
C

#ifndef WORLD_H
#define WORLD_H
#include "sphere.h"
#include <limits>
5 months ago
#include <memory>
#include <optional>
#include <tuple>
#include <vector>
struct World
{
5 months ago
std::vector<std::unique_ptr<Object>> objects;
5 months ago
template<class T>
void add(auto&&... args) {
5 months ago
objects.emplace_back(new T(args...));
}
5 months ago
std::optional<std::pair<double, Object *>> hit(const ray& r) const {
double closest = std::numeric_limits<double>::infinity();
5 months ago
Object *sphere;
for (const auto& o : objects) {
5 months ago
if (auto t = o->hit(r, 0.001, closest); t) {
closest = *t;
5 months ago
sphere = o.get();
}
}
if (closest != std::numeric_limits<double>::infinity())
return std::pair {closest, sphere};
else
return {};
}
};
#endif // WORLD_H