-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayTracer.cpp
76 lines (54 loc) · 1.71 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
#include <iostream>
#include <fstream>
#include <string>
#include <optional>
#include <cstdio>
#include <chrono>
#include "Scene/Scene.hpp"
#include "Scene/Ray.hpp"
#include "Scene/Sphere.hpp"
#pragma warning(disable:4996)
void save_imageP6(int Width, int Height, const char* fname, unsigned char* pixels) {
FILE* fp;
const int maxVal = 255;
//Reflect the image vertically
uint32_t rowLength = Width * 3;
uint8_t* temp = new uint8_t[rowLength];
for (uint64_t i = 0; i < Height / 2; ++i)
{
uint8_t* row1 = pixels + i * rowLength;
uint8_t* row2 = pixels + (Height - i - 1) * rowLength;
memcpy(temp, row1, rowLength);
memcpy(row1, row2, rowLength);
memcpy(row2, temp, rowLength);
}
delete[] temp;
printf("Saving image %s: %d x %d\n", fname, Width, Height);
fp = fopen(fname, "wb");
if (!fp) {
printf("Unable to open file '%s'\n", fname);
return;
}
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", Width, Height);
fprintf(fp, "%d\n", maxVal);
for (int j = 0; j < Height; j++) {
fwrite(&pixels[j * Width * 3], 3, Width, fp);
}
fclose(fp);
}
int main(int argc, char** argv)
{
using namespace std::chrono;
typedef high_resolution_clock Clock;
std::string filename(argv[1]);
std::cout << "Start...\n";
auto t1 = Clock::now();
Scene scene(filename);
Image image = scene.Render();
auto t2 = Clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "Done in " << time_span.count() << " seconds\n";
save_imageP6(image.x, image.y, image.name.c_str(), reinterpret_cast<unsigned char*>(image.pixels));
return 0;
}