-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytracer.cpp
111 lines (86 loc) · 2.55 KB
/
raytracer.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
//
// Martin Edwards Sept. 2021
//
//
/* TODO:
* bounding volumes
* median split algorithm
* //FIXME: aabox is broken
* use cosine attenuation for specular factor
*/
//bounding box tree
#include <iostream>
#include <vector>
#include <thread>
#include "Color.h"
#include "DataGrid.h"
#include "Helpers.h"
#include "Point.h"
#include "Ray.h"
#include "Objects/Object.h"
#include "Objects/Sphere.h"
#include "Camera.h"
#include "Material.h"
#include "Scene.h"
#include "Objects/Plane.h"
#include "Objects/Triangle.h"
#include "Lights/PointLight.h"
#include "Objects/AABox.h"
#include "Lights/BoxLight.h"
#include "SceneMaker.h"
#include <random>
//bool multiThreading = false;
bool multiThreading = true;
const int num_threads = (int) std::thread::hardware_concurrency() - 1;
void renderRow(Scene myScene, DataGrid myGrid, int j, int x_dim){
for (auto i = 0u; i < x_dim; i++) {
myGrid.set(i, j, myScene.getColorRecursiveMulti(i, j) * 255.0);
}
}
int main() {
//Welcome statement
std::cout << "Welcome to Martin's fabulous Ray tracer!" << std::endl;
//// threading stuff
std::vector<std::thread> threads;
SceneMaker sceneMaker;
// Scene myScene = *sceneMaker.scene1();
Scene myScene = *sceneMaker.scene2();
// Scene myScene = *sceneMaker.scene3();
// Scene myScene = *sceneMaker.scene4();
auto x_dim =myScene.myCamera->get_x_dim();
auto y_dim =myScene.myCamera->get_y_dim();
Progress my_prog(y_dim);
DataGrid myGrid(x_dim, y_dim);
if (multiThreading) {
// main loop for everything
for (auto j = 0u; j < y_dim; j++) {
while (threads.size() > num_threads - 1) {
// remove finished threads
for (int iter = 0; iter < num_threads; iter++) {
if (threads[iter].joinable()) {
threads[iter].join();
threads.erase(threads.begin() + iter);
break;
}
}
}
threads.emplace_back(renderRow, myScene, myGrid, j, x_dim);
my_prog.print_progress(j);
}
for (std::thread &t: threads) {
if (t.joinable()) {
t.join();
}
}
} else {
for (auto j = 0u; j < y_dim; j++) {
for (auto i = 0u; i < x_dim; i++) {
myGrid.set(i, j, myScene.getColorRecursiveMulti(i, j) * 255.0);
}
my_prog.print_progress(j);
}
}
my_prog.print_finish();
myGrid.save_image();
return 0;
}