-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWritePixels.cpp
105 lines (84 loc) · 2.33 KB
/
WritePixels.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
#include <iostream>
#include <fstream>
#include <math.h>
#include "RayTracer.h"
#include <fstream>
using std::ofstream;
using std::ifstream;
// store width and height of the render
int width = 512;
int height = 512;
int fov = 55;
int depth = 12;
int superSampling = 4;
// Our ray tracer
RayTracer * rt;
int main(const int argc, const char *argv[]){
string fileName = "default";
string test = argv[1];
if (argc > 2) // Custom image size
{
width = atoi(argv[2]);
height = atoi(argv[3]);
}
cout<< "\nResolution: \nWidth: "<< width << "\theight: " << height<< endl;
if (argc > 4) // Custom FOV
{
fov = atoi(argv[4]);
}
if (argc > 5) // Custom depth
{
depth = atoi(argv[5]);
}
if (argc > 6) // Custom superSampling
{
superSampling = atoi(argv[6]);
}
cout<< "\nFov: " << fov << endl;
if (test == "--default")
{
fileName = "default.ppm";
rt = new RayTracer(Scene::initTestScene(width, fov),depth,superSampling);
}
if (test == "--yours")
{
fileName = "yours.ppm";
rt = new RayTracer(Scene::initTestScene2(width, fov),depth,superSampling);
}
cout<< "\nFile saved as: "<< fileName << endl;
cout<< "\nDepth level: "<< depth << endl;
cout<< "\nSuperSampling level: "<< superSampling << endl;
// Test scene with max depth of 4 and sampling of 1
//rt = new RayTracer(Scene::initTestScene(width),12,4);
float pixels[width][height][4];
for(int ctr = 0; ctr < height*width; ctr++){
int i = ctr % width;
int j = ctr / width;
Color rad = rt->calculate(i,j); // Calculate Ray Trace
pixels[i][j][0] = rad.r; //Red
pixels[i][j][1] = rad.g; //Green
pixels[i][j][2] = rad.b; //Blue
pixels[i][j][3] = 1.0; //Alpha
}
// once we are done calculating, we will write to file.
ofstream testimage;
testimage.open(fileName,ios::binary | ios::out);
testimage << "P3\n" << width << " " << height << endl << width-1 << endl;
for(int j = height-1; j >=0 ; j-- ) {
for(int i = 0; i< width; i++){
for(int k = 0; k < 3; k++)
{
// normalize color value to 0-255.
// This assumes that the color values are in the
// range [0,1].
//char c = int(pixels[i][j][k]*255);
testimage << int(pixels[i][j][k]*(width-1)) << " ";
//testimage << c;
}
testimage << "\t"; // write tab
}
testimage << "\n"; // Write new line
}
testimage.close();
return 0;
}