-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.cpp
135 lines (111 loc) · 3.5 KB
/
scene.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Created by fatih on 02.03.2017.
//
#include <lights.hpp>
#include <geometry.hpp>
#include <scene.hpp>
#include <physics/ray.hpp>
#include <vertex.hpp>
#include <queue>
#include <iostream>
thread_local boost::circular_buffer<const rtr::scene::octree_type*> q_s(1024);
bool rtr::scene::ray_cast_param(const physics::ray& ray, float min_param, float max_param) const
{
bool res = false;
std::queue<const octree_type*, decltype(q_s)> q (q_s);
using physics::intersect;
if (intersect(part.bounding_box(), ray))
q.push(&part);
while (!q.empty())
{
const octree_type* oc = q.front();
q.pop();
for (auto& c : oc->get_children())
{
if (intersect(part.bounding_box(), ray))
q.push(&c);
}
oc->for_shapes([&](auto shape)
{
auto p = shape->get_parameter(ray);
if (p)
{
auto& param = *p;
if (param.parameter < max_param && param.parameter > min_param)
{
res = true;
decltype(q) empty;
q.swap(empty);
}
}
});
}
return res;
}
template <class ShapeT>
using shape_ptr_data_tuple = std::tuple<const ShapeT*, decltype(std::declval<typename ShapeT::param_res_t>().data)>;
void rtr::scene::set_samplers(std::map<uint16_t, texturing::sampler2d*> smplrs)
{
this->samplers = std::move(smplrs);
}
boost::optional<rtr::physics::ray_hit> rtr::scene::ray_cast(const rtr::physics::ray &ray) const {
using shape_pointers = png::map_t<png::mapper<shape_ptr_data_tuple>, shape_list>;
using shape_variant = png::convert_t<boost::variant, shape_pointers>;
boost::optional<shape_variant> cur_hit;
float cur_param = std::numeric_limits<float>::infinity();
auto shape_handler = [&](auto* shape)
{
auto p = shape->get_parameter(ray);
if (p)
{
auto& param = *p;
if (param.parameter < cur_param)
{
cur_param = param.parameter;
cur_hit = std::make_tuple(shape, param.data);
}
}
};
traverse_octree(part, ray, shape_handler);
constexpr auto index = png::index_of_t<std::decay_t<geometry::sphere>, shape_list>();
auto& v = boost::fusion::at_c<index>(shapes);
std::for_each(v.begin(), v.end(), [&shape_handler](auto& s) {shape_handler(&s);});
if (!cur_hit)
{
return {};
}
boost::optional<rtr::physics::ray_hit> res;
boost::apply_visitor([&](auto hit)
{
auto& shape = std::get<0>(hit);
auto& data = std::get<1>(hit);
res = shape->intersect(ray, cur_param, data);
}, *cur_hit);
return res;
}
rtr::scene::scene(const glm::vec3& c, const glm::vec3& e, const std::unordered_map<long, rtr::material*> mats)
: part(c, e, nullptr), mats(std::move(mats))
{
}
void rtr::scene::finalize()
{
boost::fusion::for_each(shapes, [&](auto& vector)
{
std::for_each(vector.begin(), vector.end(), [&](auto& elem)
{
part.insert(elem);
});
});
}
void rtr::scene::insert(const rtr::lights::ambient_light& light)
{
ambient = std::make_unique<lights::ambient_light>(light);
}
void rtr::scene::insert(rtr::lights::ambient_light&& light)
{
ambient = std::make_unique<lights::ambient_light>(std::move(light));
}
void rtr::scene::resize(const glm::vec3 &pos, const glm::vec3 &extent) {
part.resize(pos, extent);
}
rtr::scene::~scene() = default;