-
Notifications
You must be signed in to change notification settings - Fork 0
/
hit_tree.hpp
316 lines (266 loc) · 9.34 KB
/
hit_tree.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef TREE_H
#define TREE_H
#include "color.hpp"
#include "ray.hpp"
#include "light.hpp"
#include "vec3.hpp"
#include <memory>
#include <vector>
#include<random>
using namespace std;
const double infinity = numeric_limits<double>::infinity();
// Seed the random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> distribution(-1.0f, 1.0f);
struct hit_node
{
hit_record rec;
ray r;
vector<hit_node*> reflection_ray;
vector<hit_node*> transmission_ray;
hit_node* parent = nullptr;
hit_node(hit_record rec, ray r);
hit_node() {}
hit_node* get_reflection_ray(int depth, int refl_samples, int refr_samples, hittable& world, double roughness);
hit_node* get_transmission_ray(int depth, int refl_samples, int refr_samples, hittable& world, double roughness);
};
class hit_tree
{
public:
hit_tree(ray r, hit_record rec, hittable& world, vector<shared_ptr<light> > lights, color bgColor, int refl_samples, int refr_samples) : world(world), lights(lights)
{
const int DEPTH = 3;
root.r = r;
root.rec = rec;
this->world = world;
this->lights = lights;
this->bgColor = bgColor;
this->refl_samples = refl_samples;
this->refr_samples = refr_samples;
construct_tree(DEPTH, root);
}
~hit_tree() {}
color evaluate();
color node_solve(hit_node* node);
private:
hit_node root;
hittable& world;
vector<shared_ptr<light> > lights;
color bgColor;
int refl_samples;
int refr_samples;
hit_node* construct_tree(int depth, hit_node &root);
};
color hit_tree::evaluate()
{
//DFS evaluation of color at each hit record, combined to final pixel color
color sample_color = node_solve(&root);
return sample_color;
}
color hit_tree::node_solve(hit_node* node)
{
color out = color(0,0,0);
color refl_color = color(0,0,0);
color trans_color = color(0,0,0);
//evaluate reflection component
if (node->reflection_ray.size() != 0)
{
for (auto refl_node : node->reflection_ray)
{
if (refl_node != nullptr)
{
refl_color += node_solve(refl_node);
}
}
refl_color = refl_color / node->reflection_ray.size();
}
else
{
refl_color = bgColor;
}
refl_color = refl_color * node->rec.material->refl;
//evaluate transmission component
if (node->transmission_ray.size() != 0)
{
for (auto trans_node : node->transmission_ray)
{
if (trans_node != nullptr)
{
trans_color = node_solve(trans_node);
}
}
trans_color = trans_color;// / node->transmission_ray.size();
}
else
{
trans_color = bgColor;
}
trans_color = trans_color * node->rec.material->Kt;
//diffuse spec component
for (const auto& light : lights)
{
point3 p = node->rec.p;
if (light->isArea)
{
for (int i = 0; i < pow(light->samples, 2); i++)
{
//get the light's direction
vec3 light_direction = unit_vector(light->get_light_direction(node->rec.p));
ray shadow_ray(p, light_direction);
hit_record shadow_hit;
//cout << light->samples << endl;
if(!world.hit(shadow_ray, 0.001, infinity, shadow_hit))
{
color diffuse_lobe = node->rec.material->compute_diffuse(node->rec.normal, node->r.direction(), light_direction, light->col, node->rec.uv);
color spec_lobe = node->rec.material->compute_spec(node->rec.normal, node->r.direction(), light_direction, light->col);
out += (light->intensity(p) * (diffuse_lobe + spec_lobe)) / pow(light->samples, 2);
}
}
}
else
{
//get the light's direction
vec3 light_direction = unit_vector(light->get_light_direction(node->rec.p));
ray shadow_ray(p, light_direction);
hit_record shadow_hit;
if(!world.hit(shadow_ray, 0.001, infinity, shadow_hit))
{
color diffuse_lobe = node->rec.material->compute_diffuse(node->rec.normal, node->r.direction(), light_direction, light->col, node->rec.uv);
color spec_lobe = node->rec.material->compute_spec(node->rec.normal, node->r.direction(), light_direction, light->col);
out += light->intensity(p) * (diffuse_lobe + spec_lobe);
}
}
}
color ambient_term = node->rec.material->compute_ambient(node->rec.uv);
return clamp(out + refl_color + ambient_term + trans_color);
}
hit_node* hit_tree::construct_tree(int depth, hit_node& node)
{
//reflections
if(node.rec.material->is_reflective())
{
for (int i = 0; i < refl_samples; i++)
{
node.reflection_ray.push_back(node.get_reflection_ray(depth, refl_samples, refr_samples, world, node.rec.material->roughness));
}
}
//refraction
if (node.rec.material->is_transmissive())
{
for (int i = 0; i < refl_samples; i++)
{
node.transmission_ray.push_back(node.get_transmission_ray(depth, refl_samples, refr_samples, world, node.rec.material->refr_rough));
}
}
//hit_node* refraction_node = node.get
//node.refraction_ray = construct_tree(depth-1, reflection_node);
}
hit_node* hit_node::get_transmission_ray(int depth, int refl_samples, int refr_samples, hittable& world, double rough)
{
if (depth <= 0 || !this->rec.material->is_transmissive())
{
return nullptr;
}
double n2 = this->rec.material->IOR;
double n1 = 1;
if (this-> parent != nullptr && this->parent->rec.material->is_transmissive())
{
n1 = this->parent->rec.material->IOR;
n2 = 1;
}
double nR = n1/n2;
vec3 N = this->rec.normal;
vec3 I = -this->r.direction();
vec3 T_dir = (nR * (dot(N, I)) - sqrt(1-pow(nR, 2) * (1 - pow(dot(N,I),2)))) * N - nR * I;
//refraction roughness
vec3 jitter(distribution(gen), distribution(gen), distribution(gen));
T_dir += jitter * rough;
ray T = ray(this->rec.p, T_dir);
hit_record rec;
if (world.hit(T, 0.001, infinity, rec))
{
hit_node* trans_node = new hit_node();
trans_node->parent = this;
trans_node->r = T;
trans_node->rec = rec;
//refraction rays
for (int i = 0; i < refr_samples; i++)
{
hit_node* transmission = trans_node->get_transmission_ray(depth-1, refl_samples, refr_samples, world, trans_node->rec.material->refr_rough);
if (transmission != nullptr)
{
trans_node->transmission_ray.push_back(transmission);
}
}
//secondary reflections
if (rec.material->is_reflective())
{
for (int i = 0; i < refl_samples; i++)
{
hit_node* reflection = trans_node->get_reflection_ray(depth-1, refl_samples, refr_samples, world, trans_node->rec.material->roughness);
if (reflection != nullptr)
{
trans_node->reflection_ray.push_back(reflection);
}
}
}
return trans_node;
}
else
{
return nullptr;
}
}
hit_node* hit_node::get_reflection_ray(int depth, int refl_samples, int refr_samples, hittable& world, double rough)
{
;
//std::uniform_real_distribution<float> pos_dist(0.0f, 1.0f);
//base case, depth reached, none-reflective
if (depth <= 0 || !this->rec.material->is_reflective())
{
return nullptr;
}
vec3 jitter(distribution(gen), distribution(gen), distribution(gen));
//jitter = vec3(0,0,0);
vec3 d = unit_vector(this->r.direction() + jitter * rough);
vec3 n = this->rec.normal;
vec3 r = d - 2 * n * (dot(d, n));
//r = this->rec.p - point3(0.1,0,0.4);
ray reflect_ray(this->rec.p, r);
hit_record rec;
if (world.hit(reflect_ray, 0.001, infinity, rec))
{
hit_node* refl_node = new hit_node();
refl_node->parent = this;
refl_node->r = reflect_ray;
refl_node->rec = rec;
//reflection rays
for (int i = 0; i < refl_samples; i++)
{
hit_node* reflection = refl_node->get_reflection_ray(depth-1, refl_samples, refr_samples, world, refl_node->rec.material->roughness);
if (reflection != nullptr)
{
refl_node->reflection_ray.push_back(reflection);
}
}
//secondary transmission
if (rec.material->is_transmissive())
{
for (int i = 0; i < refr_samples; i++)
{
hit_node* transmission = refl_node->get_transmission_ray(depth-1, refl_samples, refr_samples, world, refl_node->rec.material->refr_rough);
if (transmission != nullptr)
{
refl_node->transmission_ray.push_back(transmission);
}
}
}
return refl_node;
}
else
{
return nullptr;
}
}
#endif