Repository navigation
Expand file tree
/
Copy pathScene.cpp
More file actions
119 lines (101 loc) · 3.43 KB
/
Copy pathScene.cpp
File metadata and controls
119 lines (101 loc) · 3.43 KB
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
#include "Scene.hpp"
void Scene::buildBVH() {
printf(" - Generating BVH...\n\n");
this->bvh = new BVHAccel(objects, 1, BVHAccel::SplitMethod::NAIVE);
}
Intersection Scene::intersect(const Ray &ray) const
{
return this->bvh->Intersect(ray);
}
void Scene::sampleLight(Intersection &pos, float &pdf) const
{
float emit_area_sum = 0;
for (uint32_t k = 0; k < objects.size(); ++k) {
if (objects[k]->hasEmit()){
emit_area_sum += objects[k]->getArea();
}
}
float p = get_random_float() * emit_area_sum;
emit_area_sum = 0;
for (uint32_t k = 0; k < objects.size(); ++k) {
if (objects[k]->hasEmit()){
emit_area_sum += objects[k]->getArea();
if (p <= emit_area_sum){
objects[k]->Sample(pos, pdf);
break;
}
}
}
}
bool Scene::trace(
const Ray &ray,
const std::vector<Object*> &objects,
float &tNear, uint32_t &index, Object **hitObject)
{
*hitObject = nullptr;
for (uint32_t k = 0; k < objects.size(); ++k) {
float tNearK = kInfinity;
uint32_t indexK;
Vector2f uvK;
if (objects[k]->intersect(ray, tNearK, indexK) && tNearK < tNear) {
*hitObject = objects[k];
tNear = tNearK;
index = indexK;
}
}
return (*hitObject != nullptr);
}
// Implementation of Path Tracing
Vector3f Scene::castRay(const Ray& ray, int depth) const
{
// TO DO Implement Path Tracing Algorithm here
Vector3f L_dir;
Vector3f L_indir;
// ray Intersection to object
Intersection obj_inter = intersect(ray);
if (!obj_inter.happened)
return L_dir;
// hit light
if (obj_inter.m->hasEmission())
return obj_inter.m->getEmission();
// hit object
Vector3f p = obj_inter.coords;
Material* m = obj_inter.m;
Vector3f N = obj_inter.normal.normalized();
Vector3f wo = ray.direction; // pixel to object vector
// has intersection,sample to light
float pdf_L = 1.0; //could not initialize
Intersection light_inter;
sampleLight(light_inter, pdf_L); // get light position and pdf of sample to light
Vector3f x = light_inter.coords;
Vector3f ws = (x - p).normalized(); //object to light
Vector3f NN = light_inter.normal.normalized();
Vector3f emit = light_inter.emit;
float d = (x - p).norm();
// ray from light to object again ,is visible?
Ray Obj2Light(p, ws);
float d2 = intersect(Obj2Light).distance;
// distance dicide visible,ATTENTION:float accuracy
if (d2 - d > -0.001) {
Vector3f eval = m->eval(wo, ws, N); // wo will not use
float cos_theta = dotProduct(N, ws);
float cos_theta_x = dotProduct(NN, -ws);//ws:from object to light,ws and NN angle > 180
L_dir = emit * eval * cos_theta * cos_theta_x / std::pow(d, 2) / pdf_L;
}
// L_indir
float P_RR = get_random_float();
if (P_RR < RussianRoulette) {
Vector3f wi = m->sample(wo, N).normalized();
Ray r(p, wi);
Intersection inter = intersect(r);
// has hit object emission?
if (inter.happened && !inter.m->hasEmission()) {
Vector3f eval = m->eval(wo, wi, N);
float pdf_O = m->pdf(wo, wi, N);
float cos_theta = dotProduct(wi, N);
L_indir = castRay(r, depth + 1) * eval * cos_theta / pdf_O / RussianRoulette;
}
}
//4->16min
return L_dir + L_indir;
}