-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTOMLLoader.hpp
191 lines (151 loc) · 5.88 KB
/
TOMLLoader.hpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef GPU_RAY_TRACING_TOMLLOADER_HPP_
#define GPU_RAY_TRACING_TOMLLOADER_HPP_
#include <chrono>
#include <iostream>
#include <toml.hpp>
#include <ctime>
#include <tuple>
#include "Camera.hpp"
#include "Random.hpp"
#include "Scene.hpp"
auto loadParams(const toml::value &scene_data)
-> std::tuple<int, int, int, int, color>;
auto loadCamera(const toml::value &scene_data) -> camera;
auto loadScene(const toml::value &scene_data) -> scene;
// TODO: move the below to a source file
#include <string>
#include <unordered_map>
#include <vector>
#include "Hittable.hpp"
#include "Material.hpp"
using std::string;
using std::vector;
inline auto loadParams(const toml::value &scene_data)
-> std::tuple<int, int, int, int, color> {
const auto raytracing_data = toml::find(scene_data, "raytracing");
int samples = toml::find<int>(raytracing_data, "samples");
int depth = toml::find<int>(raytracing_data, "depth");
int height = toml::find<int>(raytracing_data, "height");
const auto camera_data = toml::find(scene_data, "camera");
int width =
static_cast<int>(height * toml::find<num>(camera_data, "aspect_ratio"));
color background(toml::find<num>(camera_data, "background_color", 0),
toml::find<num>(camera_data, "background_color", 1),
toml::find<num>(camera_data, "background_color", 2));
return {samples, depth, width, height, background};
}
inline camera loadCamera(const toml::value &scene_data) {
const auto camera_data = toml::find(scene_data, "camera");
vector<num> lf = toml::find<vector<num>>(camera_data, "lookfrom");
point3 lookfrom(lf[0], lf[1], lf[2]);
vector<num> la = toml::find<vector<num>>(camera_data, "lookat");
point3 lookat(la[0], la[1], la[2]);
vector<num> up = toml::find<vector<num>>(camera_data, "up_vector");
vec3 up_vector(up[0], up[1], up[2]);
num aperture = toml::find<num>(camera_data, "aperture");
num fov = toml::find<num>(camera_data, "fov");
num aspect_ratio = toml::find<num>(camera_data, "aspect_ratio");
num dist_to_focus = glm::length(lookfrom - lookat);
return camera(lookfrom, lookat, up_vector, fov, aspect_ratio, aperture,
dist_to_focus);
}
inline auto loadScene(const toml::value &scene_data) -> scene {
// declare temporary unordered map along with world
std::unordered_map<string, material> material_map;
scene world;
// Getting material data
const auto &material_data = toml::find(scene_data, "materials").as_array();
// Storing material data in an unordered map
for (const auto &mat : material_data) {
string type = toml::find<string>(mat, "type");
color c(toml::find<num>(mat, "color", 0), toml::find<num>(mat, "color", 1),
toml::find<num>(mat, "color", 2));
auto id = toml::find<string>(mat, "id");
switch (type[0]) {
case 'l':
material_map.emplace(id, lambertian(c));
break;
case 'm':
material_map.emplace(id, metal(c, toml::find<num>(mat, "fuzz")));
break;
case 'd':
material_map.emplace(id, dielectric(c, toml::find<num>(mat, "ir")));
break;
case 'e':
material_map.emplace(id, emissive(c, toml::find<num>(mat, "intensity")));
break;
case 'p':
material_map.emplace(id, photoluminous(c, toml::find<num>(mat, "intensity")));
break;
}
}
// Converting unordered map into scene array
world.material_count = material_map.size();
world.materials = new material[world.material_count];
int i = 0;
for (const auto &[key, value] : material_map) {
world.materials[i] = value;
++i;
}
const auto &object_data = toml::find(scene_data, "objects").as_array();
world.hittables_size = 2 * object_data.size() - 1;
world.object_count = object_data.size();
world.hittables = new hittable[world.hittables_size];
i = object_data.size() - 1;
for (const auto &obj : object_data) {
// Referencing material_map to find correct index.
std::string key = toml::find<std::string>(obj, "material");
int mat_index =
std::distance(std::begin(material_map), material_map.find(key));
std::string geo = toml::find<std::string>(obj, "geometry");
world.hittables[i] = [&]() -> hittable {
switch (geo[0]) {
case 's': // Sphere
{
point3 p(toml::find<num>(obj, "position", 0),
toml::find<num>(obj, "position", 1),
toml::find<num>(obj, "position", 2));
return {sphere(p, toml::find<num>(obj, "radius"), mat_index)};
}
case 'p': // Plane
{
point3 p(toml::find<num>(obj, "position", 0),
toml::find<num>(obj, "position", 1),
toml::find<num>(obj, "position", 2));
vec3 n(toml::find<num>(obj, "normal", 0),
toml::find<num>(obj, "normal", 1),
toml::find<num>(obj, "normal", 2));
return {plane(p, n, mat_index)};
}
case 't': // Triangle
{
point3 p1(toml::find<num>(obj, "p1", 0), toml::find<num>(obj, "p1", 1),
toml::find<num>(obj, "p1", 2));
point3 p2(toml::find<num>(obj, "p2", 0), toml::find<num>(obj, "p2", 1),
toml::find<num>(obj, "p2", 2));
point3 p3(toml::find<num>(obj, "p3", 0), toml::find<num>(obj, "p3", 1),
toml::find<num>(obj, "p3", 2));
return {triangle(p1, p2, p3, mat_index)};
}
default:
return {};
}
}();
++i;
}
RandomStateCPU cpu_state;
RandomState *state = (RandomState *)&cpu_state;
int start = world.object_count - 1;
int end = world.hittables_size;
// Constructing Tree
hittable **hittable_ptrs = new hittable *[world.hittables_size];
for (int i = 0; i < world.hittables_size; ++i) {
hittable_ptrs[i] = world.hittables + i;
}
bounding_tree_node *tree = new bounding_tree_node_node(
hittable_ptrs, world.hittables_size, state, start, end);
convert_tree_to_array(tree, world.hittables);
delete tree;
return world;
}
#endif