Last active
November 16, 2023 01:41
-
-
Save dahmadjid/c60e5048124409b48977f1410d1eef9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
concept Hittable = requires (T& t, const Vec3<float>& position, const Ray& ray){ | |
{ t.position() } -> std::same_as<Vec3<float>>; | |
{ t.set_position(position) } -> std::same_as<void>; | |
{ t.hit(ray) } -> std::same_as<HitPayload>; | |
}; | |
template<Hittable...Ts> | |
class HittableList { | |
public: | |
template<same_as_any<Ts...> T> | |
auto add_object(T&& hittable_object) { | |
return std::get<std::vector<T>>(m_hittable_objects).emplace_back(hittable_object); | |
} | |
auto hit(const Ray& ray) { | |
std::vector<HitPayload> ret_vec; | |
ret_vec.reserve((std::get<std::vector<Ts>>(m_hittable_objects).size() + ...)); | |
std::apply([&](const std::vector<Ts>&... v) { | |
(std::for_each(v.begin(), v.end(), [&](auto object){ret_vec.emplace_back(object.hit(ray));}), ...); | |
}, m_hittable_objects); | |
return ret_vec; | |
} | |
private: | |
std::tuple<std::vector<Ts>...> m_hittable_objects; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment