-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (142 loc) · 6.24 KB
/
Copy pathmain.cpp
File metadata and controls
178 lines (142 loc) · 6.24 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
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
#include <iostream>
#include <fstream>
#include <chrono>
#include <string>
#include <memory>
#include <thread>
#include <cstdlib>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "externals/stb_image_write.h"
#include "camera.h"
#include "utilities/vec3.h"
#include "utilities/random.h"
#include "utilities/ray.h"
#include "hittables/hittable.h"
#include "hittables/bvh_node.h"
#include "textures/texture.h"
#include "materials/material.h"
#include "scenes/scene.h"
constexpr float MAX_DIST = 1000.f;
unsigned int max_bounces = 3, max_samples = 20;
unsigned int width = 300, height = 300;
vec3 compute_color(const ray& r, const vec3& background, const hittable& world, int bounces) {
hit_record rec;
if (bounces <= 0)
return vec3(0, 0, 0);
if (!world.hit(r, 0.001, MAX_DIST, rec))
return background;
ray scattered;
vec3 attenuation;
vec3 emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.point);
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
return emitted;
return emitted + attenuation * compute_color(scattered, background, world, bounces - 1);
}
void shoot_ray(unsigned char* result, const size_t& thread_no, const size_t& samples_per_thread, const camera& cam, const bvh_node& world, const vec3& background) {
srand(static_cast<unsigned int>(time(nullptr) * (thread_no + 1)));
float u, v;
ray r;
vec3 color;
size_t index = 0;
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
color = vec3();
for (size_t sample_i = 0; sample_i < samples_per_thread; sample_i++) {
u = float(x + random_double()) / float(width);
v = float(height - (y + random_double())) / float(height);
r = cam.get_ray(u, v);
color += compute_color(r, background, world, max_bounces);
}
color /= static_cast<float>(samples_per_thread);
color.output(result, &index);
}
}
}
void show_help() {
std::cout << "At one point this should be helpful.\nIn the meantime, the README will suffice." << std::endl;
}
int main(int argc, char* argv[]) {
srand(static_cast<unsigned int>(time(nullptr)));
constexpr int channels = 3;
std::string filename = "output";
random_scene cur_scene;
double fov = cur_scene.fov(), aperture = cur_scene.aperture(), focus_dist = cur_scene.focus_dist();
vec3 cam_pos = cur_scene.cam(), look_at = cur_scene.lookat();
vec3 background = cur_scene.background();
unsigned int max_threads = 0;
for (size_t i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-help")) {
show_help();
goto program_end;
}
else if (strcmp(argv[i], "-f") == 0)
filename = argv[i + 1];
else if (strcmp(argv[i], "-t") == 0)
max_threads = atoi(argv[i + 1]);
else if (strcmp(argv[i], "-w") == 0)
width = atoi(argv[i + 1]);
else if (strcmp(argv[i], "-h") == 0)
height = atoi(argv[i + 1]);
else if (strcmp(argv[i], "-s") == 0)
max_samples = atoi(argv[i + 1]);
else if (strcmp(argv[i], "-b") == 0)
max_bounces = atoi(argv[i + 1]);
else if (strcmp(argv[i], "-fov") == 0)
fov = std::stof(argv[i + 1]);
else if (strcmp(argv[i], "-cam") == 0)
cam_pos = vec3(std::stof(argv[i + 1]), std::stof(argv[i + 2]), std::stof(argv[i + 3]));
else if (strcmp(argv[i], "-look") == 0)
look_at = vec3(std::stof(argv[i + 1]), std::stof(argv[i + 2]), std::stof(argv[i + 3]));
else if (strcmp(argv[i], "-a") == 0)
aperture = std::stof(argv[i + 1]);
else if (strcmp(argv[i], "-focus") == 0)
focus_dist = std::stof(argv[i + 1]);
}
{
float aspect_ratio = width / static_cast<float>(height);
if (!focus_dist)
focus_dist = (cam_pos - look_at).length();
camera cam(cam_pos, look_at, vec3(0., 1., 0.), fov, aspect_ratio, aperture, focus_dist, 0, 1);
unsigned int allocated_size = width * height * channels;
if (!max_threads)
max_threads = std::thread::hardware_concurrency();
if (max_samples < max_threads)
max_threads = max_samples;
std::vector<std::thread> threads_queue;
threads_queue.reserve(max_threads);
unsigned char* results = new unsigned char[max_threads * allocated_size];
bvh_node world = cur_scene.descr();
unsigned int samples_per_thread = max_samples / max_threads;
std::cout << "Outputting to \"" << filename << ".png\" (" << width << "x" << height << ") at " << samples_per_thread * max_threads << " sppx.\n"
<< max_bounces << " bounce(s) max.\n"
<< max_threads << " working thread(s) with " << samples_per_thread << " sample(s) per thread." << std::endl;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
for (size_t i = 0; i < max_threads; i++) {
threads_queue.emplace_back(shoot_ray, &results[i * allocated_size], std::ref(i), std::ref(samples_per_thread), std::ref(cam), std::ref(world), std::ref(background));
}
// waiting for ray gen to finish on all threads
for (auto& t : threads_queue) {
t.join();
}
// sum computation of each threads !!FIXME!!: Use mutexes instead
unsigned char* data = new unsigned char[allocated_size];
if (!data) {
std::cerr << "Failure to allocate on heap. Aborting." << std::endl;
return EXIT_FAILURE;
}
int index = 0;
for (size_t i = 0; i < allocated_size; i++) {
unsigned int temp = 0;
for (size_t j = 0; j < max_threads; j++)
temp += results[j * allocated_size + i];
data[index++] = temp / max_threads;
}
delete[] results;
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "\nElapsed time: " << std::chrono::duration_cast<std::chrono::seconds> (end - begin).count() << "s." << std::endl;
stbi_write_png(filename.append(".png").c_str(), width, height, channels, data, 0);
delete[] data;
}
program_end:
return EXIT_SUCCESS;
}