-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (78 loc) · 2.69 KB
/
main.cpp
File metadata and controls
95 lines (78 loc) · 2.69 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
#include <iostream>
#include "readXML.h"
#include "raytracer.h"
#include "ppm.h"
#include "gif-h-master/gif.h"
#include <chrono>
void createGif(Scene scene = getScene("example3.xml"));
void motionBlur(Scene scene = getScene("example7.xml"));
int main(){
while (true) {
std::cout << "\nPlease enter \"exit\" to end the program or enter the name of the scene file (with or without \".xml\") to generate an image:\n";
std::string filename;
std::cin >> filename;
filename = filename.substr(0, filename.find(".xml")); //remove .xml if it exists
if (filename == "exit") break;
if (filename == "gif"){
createGif();
continue;
}
if (filename == "motionBlur"){
motionBlur();
continue;
}
try {
auto start = std::chrono::high_resolution_clock::now();
Scene scene = getScene(filename + ".xml");
PPM image = getImage(scene, true);
image.create();
auto stop = std::chrono::high_resolution_clock::now();
double executionTime = round(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count()/10)/100;
std::cout << "--- " << scene.outputFile << ".ppm created " << "(" << executionTime << "s) ---\n";
} catch (std::runtime_error & e) {
std::cout << e.what() << "\n";
}
}
return 0;
}
void createGif(Scene scene){
const int& width = scene.camera.resolutionX;
const int& height = scene.camera.resolutionY;
auto fileName = "output/animation.gif";
int delay = 5;
GifWriter g;
GifBegin(&g, fileName, width, height, delay);
for(int frame = 0; frame < 15; frame++){
const std::vector<std::vector<Color>> pixels = getImage(scene).getPixels();
std::vector<uint8_t> nextFrame(width * height * 4);
int pixel = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
nextFrame[pixel++] = pixels[y][x].r > 1 ? 255 : pixels[y][x].r*255;
nextFrame[pixel++] = pixels[y][x].g > 1 ? 255 : pixels[y][x].g*255;
nextFrame[pixel++] = pixels[y][x].b > 1 ? 255 : pixels[y][x].b*255;
nextFrame[pixel++] = 255;
}
}
GifWriteFrame(&g, nextFrame.data(), width, height, delay);
for (auto it = scene.objects.begin(); it != scene.objects.end(); it++){
if (Sphere* sphere = dynamic_cast<Sphere*>(*it)){
sphere->radius2 += 0.5;
}
}
}
GifEnd(&g);
}
void motionBlur(Scene scene){
PPM image = getImage(scene);
for(int frame = 0; frame < 5; frame++){
for (auto it = scene.objects.begin(); it != scene.objects.end(); it++){
if (Sphere* sphere = dynamic_cast<Sphere*>(*it)){
sphere->center.x += 0.05;
}
}
image += getImage(scene);
}
image /= 6;
image.create("output/motionBlur");
}