-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.h
52 lines (43 loc) · 1.44 KB
/
scene.h
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
//
// Created by ilim on 2018/10/19.
//
#ifndef RENDERER_SCENE_H
#define RENDERER_SCENE_H
#include <optional>
#include <vector>
#include <memory>
#include "hit.h"
#include "ray.h"
#include "shape.h"
struct Scene {
std::vector<std::shared_ptr<ShapeBase>> shapes;
Vector3 sky_color;
int spp;
int lim_depth;
Scene(const std::vector<std::shared_ptr<ShapeBase>> shapes, const Vector3 &sky_color, int spp, int(lim_depth)) :
shapes(shapes), sky_color(sky_color), spp(spp), lim_depth(lim_depth) {}
std::optional<Hit> intersect(const Ray &ray) const {
std::optional<Hit> hit, min_hit;
for (const auto &shape : shapes) {
std::optional<Hit> hit = shape->intersect(ray);
if (!hit) {
continue;
}
if (!min_hit or hit->distance < min_hit->distance) {
min_hit = hit;
}
}
return min_hit;
}
Vector3 L(const Ray &ray, double roulette_p) {
auto hit = intersect(ray);
if (!hit || ray.depth >= lim_depth) {
return sky_color;
}
auto[next_ray, f, L_illuminance] = hit->hit_shape_ptr->reflect(hit->point, ray.direction, hit->normal);
next_ray.depth = ray.depth + 1;
Vector3 next_L = random_::rand() > roulette_p || f == Vector3(0) ? Vector3(0) : L(next_ray, roulette_p);
return L_illuminance + next_L * f / roulette_p;
}
};
#endif //RENDERER_SCENE_H