-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathtracer.cpp
57 lines (46 loc) · 1.52 KB
/
pathtracer.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
/* File: raytracer.cpp; Mode: C++; Tab-width: 3; Author: Simon Flannery; */
#include "pathtracer.h"
#include "scene.h"
#include "object.h"
#include "material.h"
#include "pdf.h"
color3f PathTracer::TracePath(const Ray& ray, size_t bounce) const
{
color3f color;
if (bounce > max_bounces)
{
return color;
}
Hit hit;
if (scene->GetGroup()->Intersect(ray, hit, epsilon) != false)
{
Material* material = hit.GetMaterial();
color3f light = material->Emitted(hit.GetIntersectionPoint());
vector3f scattered;
if (material->Scatter(ray, hit, scattered) != false)
{
if (material->IsSpecular(hit.GetIntersectionPoint()) != false)
{
Ray specular_ray = Ray(hit.GetIntersectionPoint(), scattered);
color = material->GetColor(hit.GetIntersectionPoint()) * TracePath(specular_ray, bounce + 1);
}
else
{
CosinePdf pdf(hit.GetNormal());
Ray scatter_ray = Ray(hit.GetIntersectionPoint(), pdf.Generate());
// float v = pdf.GetValue(scatter_ray.GetDirection());
// color = light + material->GetColor(hit.GetIntersectionPoint()) * material->ScatterPdf(hit, scatter_ray.GetDirection()) * TracePath(scatter_ray, bounce + 1) / v;
color = light + material->GetColor(hit.GetIntersectionPoint()) * TracePath(scatter_ray, bounce + 1);
}
}
else
{
color = light;
}
}
else
{
color = scene->GetBackground();
}
return color;
}