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.
39 lines
771 B
C
39 lines
771 B
C
5 months ago
|
#ifndef WORLD_H
|
||
|
#define WORLD_H
|
||
|
|
||
|
#include "sphere.h"
|
||
|
|
||
|
#include <limits>
|
||
|
#include <optional>
|
||
|
#include <tuple>
|
||
|
#include <vector>
|
||
|
|
||
|
struct World
|
||
|
{
|
||
|
std::vector<Sphere> objects;
|
||
|
|
||
|
void add(auto&&... args) {
|
||
|
objects.emplace_back(args...);
|
||
|
}
|
||
|
|
||
|
std::optional<std::pair<double, Sphere>> hit(const ray& r) const {
|
||
|
double closest = std::numeric_limits<double>::infinity();
|
||
|
Sphere sphere;
|
||
|
|
||
|
for (const auto& o : objects) {
|
||
|
if (auto t = o.hit(r, 0.001, closest); t) {
|
||
|
closest = *t;
|
||
|
sphere = o;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (closest != std::numeric_limits<double>::infinity())
|
||
|
return std::pair {closest, sphere};
|
||
|
else
|
||
|
return {};
|
||
|
}
|
||
|
};
|
||
|
|
||
|
#endif // WORLD_H
|
||
|
|