-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraytracer.cpp
More file actions
500 lines (438 loc) · 17.4 KB
/
Copy pathraytracer.cpp
File metadata and controls
500 lines (438 loc) · 17.4 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <sstream>
#include <limits>
#include "Vector3.h"
#include "Ray.h"
#include "Sphere.h"
#include "Triangle.h"
#include "Material.h"
#include "Lights.h"
#include "Hit.h"
#include "UV.h"
#define WIDTH 1920
#define HEIGHT 1080
#define PI 3.14159265
Color bkgcolor;
Point3 lightPos(5,5,0);
std::vector<Light> sceneLights;
Point3 eye;
std::vector<std::vector<Color>> image;
void ImageLoader(std::string filename) {
image.clear();
int max_color;
int width, height;
std::ifstream infile; // image variable used to read from a file
infile.open(filename); // open the file, and associate image with it
if(infile.fail()){ // true if filename doesn't exist
throw "File failed to open";
}
std::string magic_num;
infile >> magic_num >> width >> height >> max_color;
image.resize(height);
for (int i = 0; i < height; i++){
image[i].resize(width);
}
for (int y = height-1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
unsigned int red, green, blue;
infile >> red >> green >> blue;
if (!infile) {
std::cerr << "Error reading from file around (" << y << "," << x << ")" << std::endl;
return;
}
//std::cout << red << green << blue << std::endl;
image[y][x] = Color(red, green, blue);
}
}
}
void OutputColor(std::ofstream& output_stream, const Color& color){
int ir = static_cast<int>(255 * color.X());
int ig = static_cast<int>(255 * color.Y());
int ib = static_cast<int>(255 * color.Z());
output_stream << ir << ' ' << ig << ' ' << ib << '\n';
}
Color shade_ray(const Ray& r, const Hit hit, const Object* object, const std::vector<Object*>& objects){
Point3 surface = hit.p;
double t = hit.t;
Vector3 lighting(0,0,0);
double lightIntensity = .8;
//amibent
Color color(0,0,0);
double u = hit.u;
double v = hit.v;
if(image.size() > 0){
int i = static_cast<int>(round(u * (image[0].size()-1)));
int j = static_cast<int>(round(v * (image.size()-1)));
auto red = image[j][i].x;
auto green = image[j][i].y;
auto blue = image[j][i].z;
red /= 255;
green /= 255;
blue /= 255;
color = Color(red,green,blue);
}
else{
color = object->getMaterial().color;
}
Vector3 ambiantLight = object->getMaterial().ka * color;
for(auto light : sceneLights){
//diffuse
double diffuseK = object->getMaterial().kd;
Vector3 lightdir(0,0,0);
if(light.type == 1){
lightdir = (light.posOrDir - surface).normalized();
}
else{
lightdir = -light.posOrDir.normalized();
}
Vector3 normal = object->getNormal(t, r).normalized();
double diff = std::min(std::max(dot(normal, lightdir), 0.0), 1.0);
Vector3 diffuse = diffuseK * diff * color;
//specular
double specK = object->getMaterial().ks;
// Vector3 reflectDir = -lightdir - 2.0 * dot(normal, -lightdir) * normal;
// float spec = std::pow(std::max(dot(-r.getDirection(), reflectDir), 0.0), 32);
Vector3 halfwayDir = (lightdir + -r.getDirection()).normalized();
float spec = std::pow(std::max(dot(normal, halfwayDir), 0.0), object->getMaterial().n);
Vector3 specular = specK * spec * object->getMaterial().sMat;
//shadow
double shadow = 1;
Ray shadowRay(surface, lightdir);
for(Object* object : objects){
Hit hit = object->hit(shadowRay);
Vector3 rayToObject = hit.p - surface;
if(hit.t > 2 && (rayToObject.length() < (light.posOrDir - surface).length() || light.type == 1)){
shadow = 0;
}
}
lighting = lighting + shadow * lightIntensity * (diffuse + specular);
//lighting = lighting + lightIntensity * (diffuse + specular);
}
lighting = lighting + ambiantLight;
//clamp
lighting.x = std::min(1.0, lighting.x);
lighting.y = std::min(1.0, lighting.y);
lighting.z = std::min(1.0, lighting.z);
return lighting;
}
Color trace_ray(const Ray& r, const std::vector<Object*>& objects) {
double minT = std::numeric_limits<double>::max();
Object* closestObj;
Hit minHit(Point3(0,0,0));
for(Object* object : objects){
Hit hit = object->hit(r);
if(hit.t > 0.0 && hit.t < minT){
minT = hit.t;
minHit = hit;
closestObj = object;
}
}
if(minT < std::numeric_limits<double>::max()){
return shade_ray(r, minHit, closestObj, objects);
}
return bkgcolor;
}
std::vector<Point3> vertexArr;
std::vector<UV> textureUVArr;
std::vector<Vector3> normalArr;
int main(int argc, char *argv[]){
std::vector<Object*> objects;
int width;
int height;
double vfov;
Vector3 viewdir;
Vector3 updir;
Material* mtlInstance;
mtlInstance = NULL;
std::ifstream inputFile;
std::string line;
//If file is passsed into command line argument then opens the file and gets the width and height
int lineCount = 0;
if(argc > 1){
std::string inputFileName = argv[1];
std::ifstream inputFile;
inputFile.open(inputFileName);
if(inputFile.is_open()){
std::string line;
while(std::getline(inputFile, line)){
lineCount++;
if(line == "" || line == " "){
continue;
}
std::stringstream ss(line);
std::string word;
ss >> word;
if(word == "imsize"){
ss >> width;
ss >> height;
}
else if(word == "f"){
line = line.substr(2, line.length()-2);
const char* w = line.c_str();
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
if (sscanf(w, "%d/%d/%d %d/%d/%d %d/%d/%d", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]) == 9){
Point3 v0 = vertexArr[vertexIndex[0]-1];
Point3 v1 = vertexArr[vertexIndex[1]-1];
Point3 v2 = vertexArr[vertexIndex[2]-1];
UV uv0 = textureUVArr[uvIndex[0]-1];
UV uv1 = textureUVArr[uvIndex[1]-1];
UV uv2 = textureUVArr[uvIndex[2]-1];
Vector3 n0 = normalArr[normalIndex[0]-1];
Vector3 n1 = normalArr[normalIndex[1]-1];
Vector3 n2 = normalArr[normalIndex[2]-1];
if(mtlInstance){
Triangle* tri = new Triangle(v0, v1, v2, uv0, uv1, uv2, n0, n0, n0, *mtlInstance);
objects.push_back(tri);
}
else {
Triangle* tri = new Triangle(v0, v1, v2, uv0, uv1, uv2, n0, n0, n0);
objects.push_back(tri);
}
}
else if (sscanf(w, "%d//%d %d//%d %d//%d", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]) == 6){
Point3 v0 = vertexArr[vertexIndex[0]-1];
Point3 v1 = vertexArr[vertexIndex[1]-1];
Point3 v2 = vertexArr[vertexIndex[2]-1];
Vector3 n0 = normalArr[normalIndex[0]-1];
Vector3 n1 = normalArr[normalIndex[1]-1];
Vector3 n2 = normalArr[normalIndex[2]-1];
if(mtlInstance){
Triangle* tri = new Triangle(v0, v1, v2, n0, n0, n0, *mtlInstance);
objects.push_back(tri);
}
else {
Triangle* tri = new Triangle(v0, v1, v2, n0, n0, n0);
objects.push_back(tri);
}
}
else if (sscanf(w, "%d/%d %d/%d %d/%d", &vertexIndex[0], &uvIndex[0], &vertexIndex[1], &uvIndex[1], &vertexIndex[2], &uvIndex[2]) == 6){
Point3 v0 = vertexArr[vertexIndex[0]-1];
Point3 v1 = vertexArr[vertexIndex[1]-1];
Point3 v2 = vertexArr[vertexIndex[2]-1];
UV uv0 = textureUVArr[uvIndex[0]-1];
UV uv1 = textureUVArr[uvIndex[1]-1];
UV uv2 = textureUVArr[uvIndex[2]-1];
if(mtlInstance){
Triangle* tri = new Triangle(v0, v1, v2, uv0, uv1, uv2, *mtlInstance);
objects.push_back(tri);
}
else {
Triangle* tri = new Triangle(v0, v1, v2, uv0, uv1, uv2);
objects.push_back(tri);
}
}
else if (sscanf(w, "%d %d %d", &vertexIndex[0], &vertexIndex[1], &vertexIndex[2]) == 3){
Point3 v0 = vertexArr[vertexIndex[0]-1];
Point3 v1 = vertexArr[vertexIndex[1]-1];
Point3 v2 = vertexArr[vertexIndex[2]-1];
if(mtlInstance){
Triangle* tri = new Triangle(v0, v1, v2, *mtlInstance);
objects.push_back(tri);
}
else {
Triangle* tri = new Triangle(v0, v1, v2);
objects.push_back(tri);
}
}
else{
std::cout << "face format incorrect\n";
}
}
else if(word == "v"){
std::string x;
std::string y;
std::string z;
ss >> x;
ss >> y;
ss >> z;
Point3 vertex(std::stod(x),std::stod(y),std::stod(z));
vertexArr.push_back(vertex);
}
else if(word == "vn"){
std::string x;
std::string y;
std::string z;
ss >> x;
ss >> y;
ss >> z;
Vector3 noraml(std::stod(x),std::stod(y),std::stod(z));
normalArr.push_back(noraml);
}
else if(word == "vt"){
std::string x;
std::string y;
ss >> x;
ss >> y;
UV textureUV(std::stod(x),std::stod(y));
textureUVArr.push_back(textureUV);
}
else if(word == "texture"){
std::string imgName;
ss >> imgName;
ImageLoader(imgName);
}
else if(word == "eye"){
std::string x;
std::string y;
std::string z;
ss >> x;
ss >> y;
ss >> z;
eye = Point3(std::stod(x),std::stod(y),std::stod(z));
}
else if(word == "viewdir"){
std::string x;
std::string y;
std::string z;
ss >> x;
ss >> y;
ss >> z;
viewdir = Vector3(std::stod(x),std::stod(y),std::stod(z)).normalized();
}
else if(word == "updir"){
std::string x;
std::string y;
std::string z;
ss >> x;
ss >> y;
ss >> z;
updir = Vector3(std::stod(x),std::stod(y),std::stod(z)).normalized();
}
else if(word == "vfov"){
std::string deg;
ss >> deg;
vfov = std::stod(deg);
}
else if(word == "sphere"){
std::string x;
std::string y;
std::string z;
std::string rad;
ss >> x;
ss >> y;
ss >> z;
ss >> rad;
Point3 spherePos = Point3(std::stod(x),std::stod(y),std::stod(z));
if(mtlInstance){
Sphere* sphere = new Sphere(spherePos, std::stod(rad), *mtlInstance);
objects.push_back(sphere);
}
else{
Sphere* sphere = new Sphere(spherePos, std::stod(rad));
objects.push_back(sphere);
}
}
else if(word == "bkgcolor"){
std::string r;
std::string g;
std::string b;
ss >> r;
ss >> g;
ss >> b;
bkgcolor = Color(std::stod(r),std::stod(g),std::stod(b));
}
else if(word == "mtlcolor"){
std::string r;
std::string g;
std::string b;
std::string sr;
std::string sg;
std::string sb;
std::string ka;
std::string kd;
std::string ks;
std::string n;
ss >> r;
ss >> g;
ss >> b;
ss >> sr;
ss >> sg;
ss >> sb;
ss >> ka;
ss >> kd;
ss >> ks;
ss >> n;
Color mat = Color(std::stod(r),std::stod(g),std::stod(b));
Color sMat = Color(std::stod(sr),std::stod(sg),std::stod(sb));
double Ka = std::stod(ka);
double Kd = std::stod(kd);
double Ks = std::stod(ks);
double N = std::stod(n);
mtlInstance = new Material(mat, sMat, Ka, Kd, Ks, N);
}
else if(word == "light"){
std::string x;
std::string y;
std::string z;
std::string w;
std::string r;
std::string g;
std::string b;
ss >> x;
ss >> y;
ss >> z;
ss >> w;
ss >> r;
ss >> g;
ss >> b;
Vector3 pos(std::stod(x),std::stod(y),std::stod(z));
int type = std::stoi(w);
Color color = Color(std::stod(r),std::stod(g),std::stod(b));
Light light(pos, type, color);
sceneLights.push_back(light);
}
else if(word[0] == '#' || word[0] == 0){
continue;
}
else{
std::cout << word << " is not a valid keyword on line: " << lineCount << std::endl;
return 0;
}
}
}
inputFile.close();
}
else{
std::cout << "No input file provided" << std::endl;
return 0;
}
if(width <= 0 || height <= 0){
std::cout << "width or height not valid" << std::endl;
return 0;
}
//Calculate correct viewport width height and orientaion for the given vfov and viewdir/updir
double aspectRatio = double(width) / double(height);
double focalLength = 1.0;
double viewportHeight = 2.0 * tan((vfov/2.0) * PI / 180.0) * focalLength;
double viewportWidth = aspectRatio * viewportHeight;
Vector3 horizontal = viewportWidth*cross(viewdir, updir).normalized();
Vector3 vertical = viewportHeight*cross(horizontal.normalized(), viewdir).normalized();
Point3 lowerLeftCorner = eye - horizontal/2 - vertical/2 + viewdir*focalLength;
std::string outputFile = argv[1];
outputFile = outputFile.substr(0, outputFile.length()-4);
outputFile.append(".ppm");
//Creates an output stream.
std::ofstream output_stream(outputFile, std::ios::out | std::ios::binary);
//Writes header to the file.
output_stream << "P3\n" << width << std::endl << height << std::endl << 255 << std::endl;
for(uint32_t y = height-1; y > 0; y--){
std::cerr << "\rScanlines remaining: " << y << ' ' << std::flush;
for(uint32_t x = 0; x < width; x++){
auto u = double(x) / (width-1);
auto v = double(y) / (height-1);
Vector3 rayDir = lowerLeftCorner + u*horizontal + v*vertical - eye;
rayDir.normalize();
Ray r(eye, rayDir);
Color pixel = trace_ray(r, objects);
OutputColor(output_stream, pixel);
}
}
output_stream.close();
return 0;
}