-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (61 loc) · 2.11 KB
/
Copy pathmain.cpp
File metadata and controls
78 lines (61 loc) · 2.11 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
#include <iostream>
#include "include/core/Constants.h"
#include "include/core/methods.h"
#include "include/geometry/Sphere.h"
#include "include/core/Vec3.h"
#include <tuple>
#include <limits>
#include <vector>
#include "include/core/Canvas.h"
#include "include/scene/Scene.h"
#include <thread>
#include <chrono>
void partOfCanvas(int start_X, int end_X, Scene& scene, Canvas& canvas) {
Sphere* shadowSphere = nullptr; //keeps track of the current sphere that blocks a point p from reaching the light/light source
for (int x = start_X; x < end_X; ++x) {
for (int y = -CANVAS_HEIGHT / 2; y < CANVAS_HEIGHT / 2; ++y) {
Vec3 V = canvasToViewPort(x, y);
Vec3 D = rotationMatrix * V;
float posInf = std::numeric_limits<float>::infinity();
std::tuple<int, int, int> color = TraceRay(origin, D, 1.0, posInf, scene, 3, shadowSphere);
canvas.putPixel(x, y, color);
}
}
if (shadowSphere != nullptr) {
delete shadowSphere;
shadowSphere = nullptr;
}
}
int main() {
//setting up the scene
//
Scene scene;
Canvas canvas{}; //use curly brace to initialise Canvas object;
scene.setUpScene(); //get the information we need;
unsigned int num_threads = std::thread::hardware_concurrency();
if (num_threads == 0) {
num_threads = 2;
}
std::vector<std::thread> vectorThreads;
int chunk = (int)(CANVAS_WIDTH/ num_threads);
int start_X = -CANVAS_WIDTH / 2;
//measuring performance
//
auto start = std::chrono::high_resolution_clock::now();
//creating threads
for (unsigned int i = 1; i <= num_threads; ++i) {
vectorThreads.push_back( std::thread(partOfCanvas, start_X, start_X+chunk, std::ref(scene), std::ref(canvas)) );
start_X += chunk;
}
if (start_X < ((int)CANVAS_WIDTH / 2)) {
vectorThreads.push_back( std::thread(partOfCanvas, start_X, (int)CANVAS_WIDTH / 2, std::ref(scene), std::ref(canvas)) );
}
//joining threads
for (std::thread& item : vectorThreads) {
item.join();
}
auto end = std::chrono::high_resolution_clock::now();
canvas.writeToFile();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "The time it took: " << duration.count();
}