-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
193 lines (164 loc) · 7.54 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include "RayTracer.h"
#include "SceneParser.h"
#include "Image.h"
#include "Camera.h"
#include <string.h>
#include <map>
#include <thread>
//#include <omp.h>
using namespace std;
float clampedDepth ( float depthInput, float depthMin, float depthMax);
#include "bitmap_image.hpp"
#include "ArgParser.h"
int main( int argc, char* argv[] )
{
// Fill in your implementation here.
std::string version = "v0.1.0";
std::string help = "\n"
"Raytracer version: " + version + "\n"
"\n"
"Usage: raytracer [options]\n"
"\n"
"Options:\n"
"\t-help\t\t\toutput usage information\n"
"\t-version\t\toutput the version number\n"
"\t-input <file>\t\tinput file\n"
"\t-output <file>\t\toutput file\n"
"\t-normals <file>\t\tnormal visualization file\n"
"\t-size <width> <height>\tsize of rendered image\n"
"\t-depth <depth_min> <depth_max> <depth_file>\tdepth\n"
"\t-jitter <samples> \tantialiasing using jittering method\n"
"\t-bounces <bounces> \tnumber of ray bounces off objects\n"
"\t-shadows \tenable shadows\n"
"\t-shade_back \tenable Back-Face Shading\n"
"\n"
"Examples:\n"
"\traytracer -input scene1_01.txt -size 200 200 -output output1_01.tga -depth 9 10 depth1_01.tga\n"
"\traytracer -input scene1_02.txt -size 200 200 -output output1_02.tga -depth 8 12 depth1_02.tga\n"
"\traytracer -input scene1_03.txt -size 200 200 -output output1_03.tga -depth 8 12 depth1_03.tga\n"
"\traytracer -input scene1_04.txt -size 200 200 -output output1_04.tga -depth 12 17 depth1_04.tga\n"
"\traytracer -input scene1_05.txt -size 200 200 -output output1_05.tga -depth 14.5 19.5 depth1_05.tga\n"
"\traytracer -input scene1_06.txt -size 200 200 -output output1_06.tga -depth 3 7 depth1_06.tga\n"
"\traytracer -input scene1_07.txt -size 200 200 -output output1_07.tga -depth -2 2 depth1_07.tga\n"
"\n";
if (argc == 1 || (strcmp(argv[1], "-help") == 0)) {
// No arguments or `-help`
// Print help
std::cout << help << endl;
return 0;
}
else if (argc == 2 && (strcmp(argv[1], "-version") == 0)) {
// `-version`
// Print version
std::cout << version << endl;
return 0;
}
ArgParser args = ArgParser(argc, argv);
// First, parse the scene using SceneParser.
SceneParser scene = SceneParser(args.input_file);
Camera *camera = (Camera *) scene.getCamera();
Group *group = (Group *) scene.getGroup();
RayTracer raytracer = RayTracer(&scene,
args.bounces,
(bool) args.shadows,
(bool) args.shade_back);
char *outputFile = args.output_file;
char *depthFile = args.depth_file;
char *normalsFile = args.normals_file;
int jitter = args.jitter;
//std::cout << "# of Objects in Group: " << (int) group->getGroupSize() << endl;
// Then loop over each pixel in the image, shooting a ray
// through that pixel and finding its intersection with
// the scene. Write the color at the intersection to that
// pixel in your output image.
Image image( args.width, args.height );
Image depthImage ( args.width, args.height );
Image normalsImage ( args.width, args.height );
// unsigned int n = std::thread::hardware_concurrency();
// std::cout << n << " concurrent threads are supported.\n";
float tmin = camera->getTMin();
int x, y = 0;
//#pragma omp parallel for private(y) shared(image, depthImage, normalsImage, camera, raytracer)
for (x = 0; x < args.width; x++)
{
// std::cout<<"Threads: "<<omp_get_num_threads() <<"."<<endl;
for (y = 0; y < args.height; y++) {
// std::cout << "p = ("<< x << ", " << y << ")" << endl;
float t;
Vector3f pixelColor;
Vector3f normal;
if (jitter < 2) {
Vector2f point ( (x+0.5)/args.width, (y+0.5)/args.height );
Ray ray = camera->generateRay(point);
//std::cout << "(" << ray.getDirection()[0] << ", " << ray.getDirection()[1] << ", "<< ray.getDirection()[2] << ", " << ")" << endl;
Hit hit = Hit();
pixelColor = raytracer.traceRay(ray, tmin, 0, 1.0f, hit);
t = hit.getT();
normal = hit.getNormal();
} else {
// Antialiasing / Super-sampling
// Using Jittering method
int n = jitter; // To match the algorithm's notation
Vector3f c = Vector3f(0,0,0);
t = 0;
normal = Vector3f(0,0,0);
for (int p=0; p<n; p++) {
for (int q=0; q<n; q++) {
// Unfiform random number in the range 0 to 1
float e = ((double) rand() / (double) RAND_MAX);
// Hybrid strategy that randomly perturbs a regular grid
Vector2f point ( (x+((p+e)/n))/args.width, (y+((q+e)/n))/args.height );
Ray ray = camera->generateRay(point);
Hit hit = Hit();
// Accumulate colour, t, and normal values
c += raytracer.traceRay(ray, tmin, 0, 1.0f, hit);
t += hit.getT();
normal += hit.getNormal();
}
}
// Divide the accumulated sample values by the number of samples
float n2 = pow(n, 2); // # of samples
pixelColor = c / n2;
t = t / n2;
normal = normal / n2;
}
if (outputFile != NULL) {
// Ray Caster Image
image.SetPixel( x, y, pixelColor );
}
if (depthFile != NULL) {
// std::cout << "T before: " << t << endl;
t -= args.depth_min;
t *= 1/(args.depth_max - args.depth_min);
t = 1 - t;
// std::cout << "T after: " << t << endl;
Vector3f depthPixelColor (t, t, t);
depthImage.SetPixel(x, y, depthPixelColor);
}
if (normalsFile != NULL) {
normal = Vector3f (abs(normal.x()), abs(normal.y()), abs(normal.z()));
// std::cout << "Normal: (" << normal[0] << ", " << normal[1] << ", "<< normal[2] << ", " << ")" << endl;
normalsImage.SetPixel(x, y, normal);
}
}
}
// Save image to output file
if (outputFile != NULL) {
image.SaveImage(outputFile);
}
// Depth file
if (depthFile != NULL) {
depthImage.SaveImage(depthFile);
}
// Normals file
if (normalsFile != NULL) {
normalsImage.SaveImage(normalsFile);
}
return 0;
}