-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.cpp
120 lines (87 loc) · 3.9 KB
/
renderer.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
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
#include "renderer.h"
//#define G_EPS 1e-3
void Renderer::render()
{
float focal = m_height * m_camera->focal();
int it_done = 0;
int previous_percent = 0;
#pragma omp parallel for shared(it_done, previous_percent) num_threads(12) schedule(static, 2)
for (int i = 0; i < m_height; ++i)
{
for (int j = 0; j < m_width; ++j)
{
float3 color(0.0f);
for (int si = 0; si < m_spp; ++si)
{
float dx = m_sampler.get_sample(si, i, j, 0);
float dy = m_sampler.get_sample(si, i, j, 1);
float3 direction;
direction.x = (j + dx - 0.5f * m_width) / focal;
direction.y = (0.5f * m_height - i - dy) / focal;
direction.z = -1.0f;
direction.normalize();
direction = m_camera->orientation().dot(direction);
ray r(m_camera->position(), direction);
float3 path_color = sample_ray(r, 0, si, i, j);
color += path_color;
}
if (m_verbose)
#pragma omp critical
{
it_done += 1;
int percent_done = std::floor(10 * it_done / (m_height * m_width));
if (percent_done - previous_percent == 1 )
{
std::cout << 10 * percent_done << "% done" << std::endl;
previous_percent = percent_done;
}
}
color /= m_spp;
m_image.at(i * m_width + j) = color ^ (1.0f / 2.2f);
}
}
}
float3 Renderer::sample_ray(ray r, int sp, int sample_id, int i, int j)
{
float3 out_color(0.0f);
if (sp >= m_max_nb_bounces)
return out_color;
float t_max = 1e10f;
Hit hit = m_bvh.intersect(r, t_max);
if (!hit)
return out_color;
if (sp == 0)
out_color += m_mesh->face_material(hit.face_id).emission;
float3 p = r.origin + r.direction * hit.t;
float3 n = m_mesh->face_normal(hit.face_id, p);
float r1_light = m_sampler.get_sample(sample_id, i, j, 2 + 4 * sp + 0);
float r2_light = m_sampler.get_sample(sample_id, i, j, 2 + 4 * sp + 1);
float r1_path = m_sampler.get_sample(sample_id, i, j, 2 + 4 * sp + 2);
float r2_path = m_sampler.get_sample(sample_id, i, j, 2 + 4 * sp + 3);
int e_face_id = m_mesh->emissive_face_index(m_mesh->sample_emissive_face_id(r1_light));
float3 light_point = m_mesh->triangle(e_face_id).sample_point(r1_light, r2_light);
float3 pa = p + n * m_scene_epsilon;
float3 e_n = m_mesh->face_normal(e_face_id, light_point);
float3 pb = light_point + e_n * m_scene_epsilon;
float3 light_direction = pb - pa;
float light_t_max = light_direction.norm();
light_direction /= light_t_max;
ray sr(pa, light_direction);
float3 face_color = m_mesh->face_material(hit.face_id).color;
bool viz = !m_bvh.visibility(sr, light_t_max);
if (viz)
{
float3 light_color = m_mesh->face_material(e_face_id).emission;
float light_dp = std::max(light_direction.dot(n), 0.0f);
float light_pdf = 1.0f / ( m_mesh->nb_emissive_faces() * m_mesh->area(e_face_id));
float G = m_mesh->face_normal(e_face_id, pb).dot(light_direction * -1.0f) * light_dp / (light_t_max * light_t_max);
float3 light_contribution = face_color * light_dp * light_color * G / light_pdf;
out_color += min(light_contribution, max_sample_value);
}
float3 reflection_direction = sample_around_normal(n, r1_path, r2_path);
float reflection_dp = std::max(reflection_direction.dot(n), 0.0f);
float3 reflection_pdf = reflection_dp / pi;
float3 reflection_contribution = sample_ray(ray(pa, reflection_direction), sp + 1, sample_id, i, j) * face_color;
out_color += min(reflection_contribution, max_sample_value);
return out_color;
}