-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1265 lines (1155 loc) · 45.7 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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stack>
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <string>
#include "Transform.h"
#include <vector>
//#include <GL/glew.h>
//#include <GL/glut.h>
using namespace std ;
float probMirror = 34.0f/100.0f;
float probRand = 34.0f/100.0f;
float probDirect = 31.0f/100.0f;
float probEmit = 1.0f/100.0f;
vec3 center;
vec3 eye; // The (regularly updated) vector coordinates of the eye location
vec3 up; // The (regularly updated) vector coordinates of the up location
const float ip_corners[4][3] = { {-1, 1, -1}, {1, 1, -1}, {1, -1, -1}, {-1, -1, -1}};
int width, height; // assigned from size later
const int maxwidth = 500;
const int maxheight = 500;
int depth = 5; // maximum recursive depth
float depth_of_field = 1;
//enum shape {sphere, triangle} ; // geometry
int size[2] ; // size of the image
//vec3 lights [];
int numlights = 0;
float attenuation[3] = {1, 0, 0};
string filename; // if a filename is specified, this will be it
float camera[10]; // 0-2: lookfrom, 3-5: lookat, 6-8: up 9: fov
vec3 vertarray[1000]; // array storing every single vertex, numbered 0 through 999
int facearray[2000][3]; // array storing faces (triangles, up to 2000), which consist of 3 verticies each
int maxverts; // just a convenience variable
int maxvertnorms; // max num of verticies w/ normals
float vertexnormalarray[1000][6]; // x, y, z, nx, ny, nz
int vertexcounter = 0;
int trianglecounter = 0;
const int maxobjects = 50;
vec3 g_ambient;
vec3 g_diffuse;
vec3 g_specular;
vec3 g_emission;
float g_shininess ;
float refraction;
vec3 current_translate;
vec4 current_rotate;
vec3 current_scale;
const int directional = 0;
const int point = 1;
float shadowoffset = 0.2;
struct Color {
int r, g, b;
};
class Light {
public:
int type; // 0 directional, 1 point
vec3 pos; // Either the position for a point light, or direction for a directional
//Color color;
vec3 intensity;
// attenuation too!
float radius;
}lights[50];
int lightcounter = 0;
class Ray {
public:
vec4 pos;
vec4 dir;
bool is_shadowray;
float t_min, t_max; // these might be necessary for intersection calculations
void RayThruPixel(int,int); // and a camera too
float current_index; // initially 1
void RandRayThruPixel(int,int);
};
void Ray::RayThruPixel(int i, int j) {
// i,j are position of pixel, i -> height, w -> width
// for vec4's, w term: 0 indicates vector (direction), 1 indicates point
is_shadowray = 0;
current_index = 1;
vec3 a = eye - center;
vec3 b = up;
vec3 w = glm::normalize(a);
vec3 u = glm::normalize(glm::cross(b, w));
vec3 v = glm::cross(w, u);
// Handling proper aspect ratio
float aspect = ((float) width / (float) height);
float fovy = camera[9];
float fovx = fovy;
// Lecture implementation, radians conversion
fovy = fovy *(pi/180);
fovx = fovx *(pi/180) ;
//For depth of field, we randomly nudge i & j by a random amount, and redefine the focal plane
float alpha = tan(aspect*fovx/2)*((float) (j - (width/2)) / (width/2));
float beta = tan(fovy/2)*( (float) ( (height/2) - i) / (height/2));
pos = vec4(eye,1); // now they are set as vec4's
//vec3 temp = glm::normalize(alpha*u + beta*v - w);
if (depth_of_field >= 0) {
vec3 rnd(( ((float) (rand() % 10)) / 50), ( ((float) (rand() % 10)) / 50), 0);
vec3 neye = eye + rnd; // new eye in 3D
vec4 neweye = pos + vec4(rnd, 0); // new eye in 4D
vec3 temp = alpha*u + beta*v - w;
dir = glm::normalize(vec4(temp,0));
float D = 1; // eye to image plane (always 1)
float d = sqrt(pow(temp[0], 2) + pow(temp[1], 2) + pow(temp[2], 2)); // dist of eye to pixel
// image plane is 1 away from eye
vec4 p = pos + (d/ (D / (D + 4.5)) )*dir; // the intersection point of the ray with the focal point
vec4 newdir = glm::normalize(p - neweye);
pos = neweye;
dir = newdir;
}
else {
vec3 temp = alpha*u + beta*v - w;
dir = glm::normalize(vec4(temp,0));
}
}
void Ray::RandRayThruPixel(int i, int j) {
is_shadowray = 0;
current_index = 1;
float randPhi = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
float randTheta = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
float randPsi = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
mat3 rotation(cos(randTheta)*cos(randPsi),
-cos(randTheta)*sin(randPsi),
sin(randTheta),
cos(randPhi)*sin(randPsi) + sin(randPhi)*sin(randTheta)*cos(randPsi),
cos(randPhi)*cos(randPsi) - sin(randPhi)*sin(randTheta)*sin(randPsi),
-sin(randPhi)*cos(randTheta),
sin(randPhi)*sin(randPsi) - cos(randPhi)*sin(randTheta)*cos(randPsi),
sin(randPhi)*cos(randPsi) + cos(randPhi)*sin(randTheta)*sin(randPsi),
cos(randPhi)*cos(randTheta)
);
vec3 a = eye - center;
vec3 b = up;
vec3 w = glm::normalize(a);
vec3 u = glm::normalize(glm::cross(b, w));
vec3 v = glm::cross(w, u);
// Handling proper aspect ratio
float aspect = ((float) width / (float) height);
float fovy = camera[9];
float fovx = fovy;
// Lecture implementation, radians conversion
fovy = fovy *(pi/180);
fovx = fovx *(pi/180) ;
//For depth of field, we randomly nudge i & j by a random amount, and redefine the focal plane
float alpha = tan(aspect*fovx/2)*((float) (j - (width/2)) / (width/2));
float beta = tan(fovy/2)*( (float) ( (height/2) - i) / (height/2));
pos = vec4(eye,1); // now they are set as vec4's
//vec3 temp = glm::normalize(alpha*u + beta*v - w);
if (depth_of_field >= 0) {
vec3 rnd(( ((float) (rand() % 10)) / 50), ( ((float) (rand() % 10)) / 50), 0);
vec3 neye = eye + rnd; // new eye in 3D
vec4 neweye = pos + vec4(rnd, 0); // new eye in 4D
vec3 temp = alpha*u + beta*v - w;
dir = glm::normalize(vec4(temp,0));
float D = 1; // eye to image plane (always 1)
float d = sqrt(pow(temp[0], 2) + pow(temp[1], 2) + pow(temp[2], 2)); // dist of eye to pixel
// image plane is 1 away from eye
vec4 p = pos + (d/ (D / (D + 4.5)) )*dir; // the intersection point of the ray with the focal point
vec4 newdir = glm::normalize(p - neweye);
pos = neweye;
dir = newdir;
}
else {
vec3 temp = alpha*u + beta*v - w;
dir = glm::normalize(vec4(temp,0));
}
dir = vec4(glm::normalize(rotation * vec3(dir.x, dir.y, dir.z)), 0);
}
// Shape is a basic abstract class, holds intersect method
class Shape {
public:
Color color; // RGB color, from scale of 0 to 1
float intersect(Ray);
vec3 ambient ; // Lighting properties, as vec3's
vec3 diffuse ;
vec3 specular ;
vec3 emission ;
float shininess ; // simple float
mat4 matrix ; // Transformation matrix, mat4
vec4 normal; // to get around messy transformation issues
float refractive_index; // n, index of refraction
};
class Sphere: public Shape {
public:
float args[4]; //float x, y, z, radius;
float intersect(Ray);
}spheres[100];
int spherecounter = 0; // Tracks how many spheres in scene
float Sphere::intersect(Ray r) {
// actual implementation goes here
float ret = 999999;
vec4 pos = r.pos;
vec4 dir = r.dir;
//if (r.is_shadowray == true) {
// r.t_min = 0.001; // arbitrary?
//}
r.t_min = 0;
// In the case of a miss, r.t_min is 0, and returned t is 999999 (6 9's)
vec4 cen = vec4(args[0], args[1], args[2],1); //center of the sphere
float A = glm::dot(dir,dir);
float B = 2*glm::dot(dir, pos - cen);
float C = glm::dot(pos-cen, pos-cen) - args[3]*args[3];
float det = B*B - 4*A*C;
if (det >= 0) {
float root1 = (-B + sqrt(det))/2*A;
float root2 = (-B - sqrt(det))/2*A;
float min;
if (root1 < root2) {
min = root1;
} else {
min = root2;
}
if (det = 0) {
ret = root1;
} else if (root1 >= 0 && root2 >= 0) {
ret = min;
} else if (root1 >=0 && root2 < 0) {
ret = root1;
} else if (root1 < 0 && root2 >= 0) {
ret = root2;
}
}
//if (ret >= r.t_min) {
return ret;
//}
//else {
// return 999999;
//}
}
class Triangle: public Shape {
public:
vec3 vertexarray[3]; // 3 verticies of a triangle
float intersect(Ray);
}triangles[500];
float Triangle::intersect(Ray r) {
// actual implementation goes here
float ret = 999999;
vec4 pos = r.pos;
vec4 dir = r.dir;
r.t_min = 0;
vec3 vert_a = vertexarray[0];
vec3 vert_b = vertexarray[1];
vec3 vert_c = vertexarray[2];
normal = normal = glm::normalize(vec4(glm::normalize(glm::cross(vert_c - vert_a,vert_b - vert_a)),0));
float a = vert_a[0] - vert_b[0]; // Xa - Xb
float b = vert_a[1] - vert_b[1]; // Ya - Yb
float c = vert_a[2] - vert_b[2]; // Za - Zb
float d = vert_a[0] - vert_c[0];
float e = vert_a[1] - vert_c[1];
float f = vert_a[2] - vert_c[2];
float g = dir[0];
float h = dir[1];
float i = dir[2];
float j = vert_a[0] - pos[0];
float k = vert_a[1] - pos[1];
float l = vert_a[2] - pos[2];
float beta;
float gamma;
float t;
float M = a*(e*i-h*f)+b*(g*f-d*i)+c*(d*h-e*g);
beta = (j*(e*i-h*f)+k*(g*f-d*i)+l*(d*h-e*g))/M;
gamma = (i*(a*k-j*b)+h*(j*c-a*l)+g*(b*l-k*c))/M;
t = -(f*(a*k-j*b)+e*(j*c-a*l)+d*(b*l-k*c))/M;
vec4 n = vec4(glm::normalize(glm::cross(vertexarray[2]-vertexarray[0],vertexarray[1]-vertexarray[0])),0);
if (glm::dot(dir,n) != 0 && (t >= r.t_min) && (gamma >= 0) && (gamma <= 1) && (beta >= 0) && (beta <= 1 - gamma)) {
ret = t;
}
return ret;
}
class Image { // stores the color values and has method writeIamge?
public:
Color ** colors;
void writeImage();
void initialize();
};
void Image::initialize() { // magic memory allocation bullshit
int size_x = height;
int size_y = width;
colors = (Color**) malloc (size_x * sizeof(Color *));
for (int i = 0; i < size_x; i++) {
colors[i] = (Color*) malloc(size_y * sizeof(Color));
}
//colors = (Color*) malloc (10000);
}
void Image::writeImage() {
ofstream myfile; //("output.ppm"); //say we had an output file
myfile.open ("output.ppm");
if (myfile.is_open()) {
myfile << "P3 \n";
myfile << width << " " << height << " \n";
myfile << "255 \n";
//for (int i = height - 1; i >= 0; i--) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) { // order is changed since ppm starts from top left corner
myfile << colors[i][j].r << " " << colors[i][j].g << " " << colors[i][j].b << " ";
//myfile << 0 << " " << 0 << " " << 0 << " ";
}
myfile << "\n";
}
myfile.close();
}
}
// **************************** Raytracer stuff
class Intersection {
public:
Shape s;
Ray r;
vec4 normal;
Intersection (Ray);
bool flag;
vec4 point; // The intersection point, for use in transformations
};
Intersection::Intersection(Ray ray) {
flag = false;
float min = 999999;
//r.t_min = min;
for (int i = 0; i < spherecounter; i++) {
Sphere sphere = spheres[i];
Ray sray;
sray.pos = ray.pos*glm::transpose(glm::inverse(sphere.matrix));
vec4 tempdir = ray.dir*glm::transpose(glm::inverse(sphere.matrix));
sray.dir = glm::normalize(tempdir);
float t = sphere.Sphere::intersect(sray); //we now have the transformed intersection "t"
vec4 newpoint = sray.pos + t*sray.dir;
float temp = t; // temp holds the object-space t value
t = t/sqrt(glm::dot(tempdir, tempdir)); // returns a proper t in world coordinates. It seems to make everything really slow...
//float t = sphere.Sphere::intersect(ray);
if (t < min && temp < 999999) { // the min has to be world coordinate min, so here the problem comes in
min = t; // now are we returning the correct t?
point = newpoint*glm::transpose(sphere.matrix); // intersection point p turns into Mp
s = spheres[i];
flag = true;
r = sray;
r.current_index = ray.current_index;
r.dir = sray.dir*glm::transpose(sphere.matrix);
r.t_min = min;
normal = glm::normalize(glm::normalize( (sray.pos + sray.dir*temp) - vec4(sphere.args[0], sphere.args[1], sphere.args[2],1))*glm::inverse(sphere.matrix)); // Normals are hit with inverse transpose
//normal = glm::normalize(((ray.pos + ray.dir*t) - vec4(sphere.args[0], sphere.args[1], sphere.args[2],1)));
}
}
for (int i = 0; i < trianglecounter; i++) {
Triangle triangle = triangles[i];
Ray tray;
tray.pos = ray.pos*glm::transpose(glm::inverse(triangle.matrix));
vec4 tempdir = ray.dir*glm::transpose(glm::inverse(triangle.matrix));
tray.dir = glm::normalize(tempdir);
float t = triangle.Triangle::intersect(tray);
float temp = t;
vec4 newpoint = tray.pos + t*tray.dir;
t = t/sqrt(glm::dot(tempdir, tempdir));
//float t = triangle.Triangle::intersect(ray);
if (t < min && temp < 999999) {
min = t;
point = newpoint*glm::transpose(triangle.matrix); // intersection point p turns into Mp
s = triangles[i];
flag = true;
//r = ray;
r = tray;
r.current_index = ray.current_index;
r.dir = tray.dir*glm::transpose(triangle.matrix);
r.t_min = min;
normal = -glm::normalize(triangle.normal*glm::inverse(triangle.matrix));
}
}
}
struct Colorf {
float r, g, b;
};
class Shading {
public:
Colorf shadecolor;
void DirectShade(Intersection, int); // also must take in all lights later
void InDirectRandShade(Intersection, int);
void MirrorShade(Intersection, int);
void EmitShade(Intersection, int);
};
void Shading::DirectShade(Intersection hit, int d) {
if (d < depth && (hit.flag == true)) { //recursive depth
Shape shape = hit.s;
vec3 veccolor = vec3(shape.ambient[0] + shape.emission[0], shape.ambient[1] + shape.emission[1],
shape.ambient[2] + shape.emission[2]);
vector<Ray> shadowRays;
for (int i = 0; i < lightcounter; i++) {
Ray shadow; // generate a shadow ray
shadow.is_shadowray = true;
shadow.current_index = 1;
//shadow.pos = (hit.r.pos + hit.r.dir*hit.r.t_min + hit.normal*0.001);
shadow.pos = (hit.point + hit.normal*0.001); // now, replace calculating point with hit.point
vec4 lightposition;
vec4 lightdirection;
vec4 lightside;
vec4 lightup;
float area;
if (lights[i].type == 1) { // point light
vec4 lightpositiontest = vec4(lights[i].pos,1);
vec4 lightdirectiontest = glm::normalize(lightpositiontest - shadow.pos);
lightside = glm::normalize(vec4(glm::cross(vec3(lightdirectiontest), up),0)); //might not work for lights directly above or below a point
lightup = glm::normalize(vec4(glm::cross(vec3(lightside),vec3(lightdirectiontest)),0)); //fixing lightup will fix this as well
//lightposition = lightpositiontest + unifRand()*lightup*shadowoffset + unifRand()*lightside*shadowoffset; // Randomized shadows
lightposition = lightpositiontest;
lightdirection = glm::normalize(lightposition - shadow.pos);
area = 1.0f;
} else if (lights[i].type == 0) { //directional
lightposition = vec4(lights[i].pos,1); // the 3d position in space
lightdirection = glm::normalize(vec4(vec3(lightposition),0));
area = 1.0f;
} else if (lights[i].type == 2) { //area
vec4 lightpositiontest = vec4(lights[i].pos,1);
vec4 lightdirectiontest = glm::normalize(lightpositiontest - shadow.pos);
lightside = glm::normalize(vec4(glm::cross(vec3(lightdirectiontest), up),0)); //might not work for lights directly above or below a point
lightup = glm::normalize(vec4(glm::cross(vec3(lightside),vec3(lightdirectiontest)),0)); //fixing lightup will fix this as well
//lightposition = lightpositiontest + unifRand()*lightup*shadowoffset + unifRand()*lightside*shadowoffset; // Randomized shadows
lightposition = lightpositiontest;
lightdirection = glm::normalize(lightposition - shadow.pos);
area = 4.0f*pi*pow(lights[i].radius,2.0f);
}
vec4 eyedirn = glm::normalize(vec4(eye,1) - shadow.pos);
vec4 half = glm::normalize(lightdirection + eyedirn);
shadow.dir = lightdirection;
Intersection intersect(shadow);
float visibility;
if (lights[i].type == 1 && intersect.flag == true) {
visibility = (float)(sqrt(glm::dot(lightposition-shadow.pos, lightposition-shadow.pos))-sqrt(glm::dot(lightposition-intersect.point, lightposition-intersect.point)))/sqrt(glm::dot(lightposition-shadow.pos, lightposition-shadow.pos));
} else if (lights[i].type == 0 && intersect.flag == true) {
visibility = (float)0.0f;
} else if (lights[i].type == 2) {
float numshadows = 10;
visibility = (float)((float)1.0f/(float)(numshadows+(float)1.0f))*(sqrt(glm::dot(lightposition-shadow.pos, lightposition-shadow.pos))-sqrt(glm::dot(lightposition-intersect.point, lightposition-intersect.point)))/sqrt(glm::dot(lightposition-shadow.pos, lightposition-shadow.pos));
for (int s = 0; s < numshadows; s++) {
float randPhi = (float) ((float) rand() / RAND_MAX) * 180.0f-90.0f;
float randTheta = (float) ((float) rand() / RAND_MAX) * 180.0f-90.0f;
float randPsi = (float) ((float) rand() / RAND_MAX) * 180.0f-90.0f;
mat3 rotation(cos(randTheta)*cos(randPsi),
-cos(randTheta)*sin(randPsi),
sin(randTheta),
cos(randPhi)*sin(randPsi) + sin(randPhi)*sin(randTheta)*cos(randPsi),
cos(randPhi)*cos(randPsi) - sin(randPhi)*sin(randTheta)*sin(randPsi),
-sin(randPhi)*cos(randTheta),
sin(randPhi)*sin(randPsi) - cos(randPhi)*sin(randTheta)*cos(randPsi),
sin(randPhi)*cos(randPsi) + cos(randPhi)*sin(randTheta)*sin(randPsi),
cos(randPhi)*cos(randTheta)
);
vec4 tempdir = -shadow.dir;
vec4 newdir = glm::normalize(vec4(rotation*vec3(tempdir[0],tempdir[1],tempdir[2]),0));
vec4 templightpos = lightposition + newdir*lights[i].radius;
Ray newshadow;
newshadow.is_shadowray = true;
newshadow.current_index = 1;
newshadow.pos = (hit.point + hit.normal*0.001);
newshadow.dir = glm::normalize(templightpos - newshadow.pos);
Intersection newintersect(newshadow);
if (newintersect.flag == true) {
visibility += (float)((float)1.0f/(float)(numshadows+(float)1.0f))*(float)(sqrt(glm::dot(templightpos-newshadow.pos, templightpos-newshadow.pos))-sqrt(glm::dot(templightpos-newintersect.point, templightpos-newintersect.point)))/sqrt(glm::dot(templightpos-newshadow.pos, templightpos-newshadow.pos));
} else {
visibility += (float)((float)1.0f/(float)(numshadows+(float)1.0f))*(float)1.0f;
}
}
} else {
visibility = (float)1.0f;
}
//visibility = 0.5f;
veccolor[0] = (veccolor[0] + visibility*area*lights[i].intensity[0]*(shape.diffuse[0]*max((float) glm::dot(hit.normal,lightdirection), (float) 0) + shape.specular[0]*pow((float) max((float) glm::dot(hit.normal,half), (float) 0), shape.shininess) ));
veccolor[1] = (veccolor[1] + visibility*area*lights[i].intensity[1]*(shape.diffuse[1]*max((float) glm::dot(hit.normal,lightdirection), (float) 0) + shape.specular[1]*pow((float) max((float) glm::dot(hit.normal,half), (float) 0), shape.shininess) ));
veccolor[2] = (veccolor[2] + visibility*area*lights[i].intensity[2]*(shape.diffuse[2]*max((float) glm::dot(hit.normal,lightdirection), (float) 0) + shape.specular[2]*pow((float) max((float) glm::dot(hit.normal,half), (float) 0), shape.shininess) ));
}
// Reflection rays, sent after all other rays are cast. Does this make sense?
Ray rray;
rray.dir = glm::normalize(hit.r.dir - (2*glm::dot(hit.r.dir,hit.normal))*hit.normal);
rray.pos = hit.point + rray.dir*0.001;
if (veccolor[0] > 1) {
veccolor[0] = 1;
}
if (veccolor[0] < 0) {
veccolor[0] = 0;
}
if (veccolor[1] > 1) {
veccolor[1] = 1;
}
if (veccolor[1] < 0) {
veccolor[1] = 0;
}
if (veccolor[2] > 1) {
veccolor[2] = 1;
}
if (veccolor[2] < 0) {
veccolor[2] = 0;
}
shadecolor.r = (1/(1-probDirect)) * veccolor[0];
shadecolor.g = (1/(1-probDirect)) * veccolor[1];
shadecolor.b = (1/(1-probDirect)) * veccolor[2];
}
else {
shadecolor.r = 0;
shadecolor.g = 0;
shadecolor.b = 0;
}
}
void Shading::InDirectRandShade(Intersection hit, int d) {
if (d < depth && (hit.flag == true)) { //recursive depth
Shape shape = hit.s;
vec3 veccolor = vec3(shape.ambient[0] + shape.emission[0], shape.ambient[1] + shape.emission[1],
shape.ambient[2] + shape.emission[2]);
// Reflection rays, sent after all other rays are cast. Does this make sense?
Ray randRay;
float randPhi = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
float randTheta = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
float randPsi = (float) ((float) rand() / RAND_MAX) * 180.0f - 90.0f;
mat3 rotation(cos(randTheta)*cos(randPsi),
-cos(randTheta)*sin(randPsi),
sin(randTheta),
cos(randPhi)*sin(randPsi) + sin(randPhi)*sin(randTheta)*cos(randPsi),
cos(randPhi)*cos(randPsi) - sin(randPhi)*sin(randTheta)*sin(randPsi),
-sin(randPhi)*cos(randTheta),
sin(randPhi)*sin(randPsi) - cos(randPhi)*sin(randTheta)*cos(randPsi),
sin(randPhi)*cos(randPsi) + cos(randPhi)*sin(randTheta)*sin(randPsi),
cos(randPhi)*cos(randTheta)
);
vec3 newRraydir = vec3(-hit.normal.x, -hit.normal.y, hit.normal.z);
vec3 rotRraydir = glm::normalize(rotation * newRraydir);
randRay.dir = glm::normalize(vec4(rotRraydir, 1));
randRay.pos = hit.point + randRay.dir*0.001;
Intersection randreflect(randRay);
Shading randReflectShade;
Shading thisShade;
thisShade.DirectShade(hit, d+1);
float aveg = (thisShade.shadecolor.r + thisShade.shadecolor.g + thisShade.shadecolor.b)/3.0f;
float randChoice = ((float) rand() / (float) RAND_MAX);
if (aveg < randChoice) {
float randN = 1.0f;
while (randN > (probDirect + probEmit)) {
randN = ((float) rand() / (float) RAND_MAX);
}
if (randN < probDirect) {
randReflectShade.DirectShade(randreflect, d+1);
} else {
randReflectShade.EmitShade(randreflect, d+1);
}
/* WE EMIT */
} else {
float randN = 1.0f;
while (randN > (probRand + probMirror)) {
randN = ((float) rand() / (float) RAND_MAX);
}
if (randN < probRand) {
randReflectShade.InDirectRandShade(randreflect, d+1);
} else {
randReflectShade.MirrorShade(randreflect, d+1);
}
}
/*float randN = ((float) rand() / (float) RAND_MAX);
if (randN < probDirect) {
randReflectShade.DirectShade(randreflect,d+1);
}
if ((randN >= probDirect) && (randN < (probDirect+probRand))) {
randReflectShade.InDirectRandShade(randreflect, d+1);
}
if ((randN >= (probDirect+probRand)) && (randN < (probDirect+probRand+probMirror))) {
randReflectShade.MirrorShade(randreflect, d+1);
}
if ((randN >= (probDirect+probRand+probMirror)) && (randN < (probDirect+probRand+probMirror+probEmit))) {
randReflectShade.EmitShade(randreflect, d+1);
//randReflectShade.DirectShade(randreflect, d+1);
}*/
//vec3 cosineWeight = 1.0f;
float dotVec = glm::dot(randRay.dir, hit.normal);
float randRayMag = sqrt(glm::dot(randRay.dir, randRay.dir));
float hitMag = sqrt(glm::dot(hit.normal, hit.normal));
float cosTerm = dotVec / ((randRayMag) * (hitMag));
cosTerm = 1.0f;
//vec4 temp = glm::normalize(randRay.dir * hit.normal);
veccolor[0] = shape.specular[0]* cosTerm *((float) randReflectShade.shadecolor.r);
veccolor[1] = shape.specular[1]* cosTerm *((float) randReflectShade.shadecolor.g);
veccolor[2] = shape.specular[2]* cosTerm *((float) randReflectShade.shadecolor.b);
if (veccolor[0] > 1) {
veccolor[0] = 1;
}
if (veccolor[0] < 0) {
veccolor[0] = 0;
}
if (veccolor[1] > 1) {
veccolor[1] = 1;
}
if (veccolor[1] < 0) {
veccolor[1] = 0;
}
if (veccolor[2] > 1) {
veccolor[2] = 1;
}
if (veccolor[2] < 0) {
veccolor[2] = 0;
}
shadecolor.r = (1/(1-probRand)) * veccolor[0];
shadecolor.g = (1/(1-probRand)) * veccolor[1];
shadecolor.b = (1/(1-probRand)) * veccolor[2];
}
else {
shadecolor.r = 0;
shadecolor.g = 0;
shadecolor.b = 0;
}
}
void Shading::MirrorShade(Intersection hit, int d) {
if (d < depth && (hit.flag == true)) { //recursive depth
Shape shape = hit.s;
vec3 veccolor = vec3(shape.ambient[0] + shape.emission[0], shape.ambient[1] + shape.emission[1],
shape.ambient[2] + shape.emission[2]);
// Reflection rays, sent after all other rays are cast. Does this make sense?
Ray rray;
rray.dir = glm::normalize(hit.r.dir - (2*glm::dot(hit.r.dir,hit.normal))*hit.normal);
rray.pos = hit.point + rray.dir*0.001;
Intersection reflect(rray);
Shading reflectshade;
float randN = ((float) rand() / (float) RAND_MAX);
if (randN < probDirect) {
reflectshade.DirectShade(reflect,d+1);
}
if ((randN >= probDirect) && (randN < (probDirect+probRand))) {
reflectshade.InDirectRandShade(reflect, d+1);
}
if ((randN >= (probDirect+probRand)) && (randN < (probDirect+probRand+probMirror))) {
reflectshade.MirrorShade(reflect, d+1);
}
if ((randN >= (probDirect+probRand+probMirror)) && (randN < (probDirect+probRand+probMirror+probEmit))) {
reflectshade.EmitShade(reflect, d+1);
//reflectshade.DirectShade(reflect, d+1);
}
//reflectshade.DirectShade(reflect, d+1);
veccolor[0] = shape.specular[0]*((float) reflectshade.shadecolor.r);
veccolor[1] = shape.specular[1]*((float) reflectshade.shadecolor.g);
veccolor[2] = shape.specular[2]*((float) reflectshade.shadecolor.b);
if (veccolor[0] > 1) {
veccolor[0] = 1;
}
if (veccolor[0] < 0) {
veccolor[0] = 0;
}
if (veccolor[1] > 1) {
veccolor[1] = 1;
}
if (veccolor[1] < 0) {
veccolor[1] = 0;
}
if (veccolor[2] > 1) {
veccolor[2] = 1;
}
if (veccolor[2] < 0) {
veccolor[2] = 0;
}
shadecolor.r = (1/(1-probMirror)) * veccolor[0];
shadecolor.g = (1/(1-probMirror)) * veccolor[1];
shadecolor.b = (1/(1-probMirror)) * veccolor[2];
}
else {
shadecolor.r = 0;
shadecolor.g = 0;
shadecolor.b = 0;
}
}
void Shading::EmitShade(Intersection hit, int d) {
if (d < depth && (hit.flag == true)) { //recursive depth
Shape shape = hit.s;
vec3 veccolor = vec3(shape.ambient[0] + shape.emission[0], shape.ambient[1] + shape.emission[1],
shape.ambient[2] + shape.emission[2]);
shadecolor.r = (1/(1-probEmit)) * veccolor[0];
shadecolor.g = (1/(1-probEmit)) * veccolor[1];
shadecolor.b = (1/(1-probEmit)) * veccolor[2];
}
else {
shadecolor.r = 0;
shadecolor.g = 0;
shadecolor.b = 0;
}
}
void Parser (const char * filename) {
stack <mat4> transfstack ;
transfstack.push(mat4(1.0)) ; //sets initial value to identity
string str, ret = "" ;
ifstream in ;
in.open(filename) ;
if (in.is_open()) {
getline (in, str) ;
int n = 0;
while (in) {
if ((str.find_first_not_of("\t\r\n") != string::npos) && (str[0] != '#')) {
string cmd;
stringstream s(str);
s >> cmd;
if (cmd == "directional" && lightcounter < 50) { // change later
lights[lightcounter].type = 0; // signifies directional
for (int i = 0; i < 6; i++) { // xyz rgb
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
float x;
s >> x;
if (i < 3) {
lights[lightcounter].pos[i] = x;
} else {
lights[lightcounter].intensity[i-3] = x;
}
lights[lightcounter].radius = 0.001;
}
}
lightcounter++;
}
else if (cmd == "point" && lightcounter < 50) { // change later
//Light l = lights[lightcounter];
lights[lightcounter].type = 1; // signifies directional
for (int i = 0; i < 6; i++) { // xyz rgb
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
float x;
s >> x;
if (i < 3) {
lights[lightcounter].pos[i] = x;
}
else {
lights[lightcounter].intensity[i-3] = x;
}
lights[lightcounter].radius = 0.001;
}
}
lightcounter++;
}
else if (cmd == "area" && lightcounter < 50) { // change later
//Light l = lights[lightcounter];
lights[lightcounter].type = 2; // signifies area
for (int i = 0; i < 7; i++) { // xyz rgb
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
float x;
s >> x;
if (i < 3) {
lights[lightcounter].pos[i] = x;
}
else if (i < 6) {
lights[lightcounter].intensity[i-3] = x;
}
else {
lights[lightcounter].radius = x;
}
}
}
lightcounter++;
}
else if (cmd == "size") { // refers to size of image, width and height
for (int i = 0; i < 2; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> size[i];
}
}
}
else if (cmd == "attenuation") { // refers to size of image, width and height
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> attenuation[i];
}
}
}
else if (cmd == "maxdepth") {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> depth;
}
}
else if (cmd == "camera") {
for (int i = 0; i < 10; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> camera[i] ;
}
}
}
else if (cmd == "maxverts") {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> maxverts;
}
}
else if (cmd == "maxvertnorms") {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> maxvertnorms;
}
}
else if (cmd == "vertex") {
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> vertarray[vertexcounter][i] ;
}
}
vertexcounter++;
}
else if (cmd == "tri") {
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
int x;
s >> x;
triangles[trianglecounter].vertexarray[i] = vertarray[x]; //conversion from 1 based indexing
}
}
triangles[trianglecounter].ambient = g_ambient;
triangles[trianglecounter].diffuse = g_diffuse;
triangles[trianglecounter].emission = g_emission;
triangles[trianglecounter].specular = g_specular;
triangles[trianglecounter].shininess = g_shininess;
triangles[trianglecounter].matrix = transfstack.top();
triangles[trianglecounter].refractive_index = refraction;
trianglecounter++;
}
else if (cmd == "sphere") { // temporarily make sphere red
for (int i = 0; i < 4; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> spheres[spherecounter].args[i] ;
}
}
spheres[spherecounter].ambient = g_ambient;
spheres[spherecounter].diffuse = g_diffuse;
spheres[spherecounter].emission = g_emission;
spheres[spherecounter].specular = g_specular;
spheres[spherecounter].shininess = g_shininess;
spheres[spherecounter].matrix = transfstack.top();
spheres[spherecounter].refractive_index = refraction;
spherecounter++;
}
// Lighting
else if (cmd == "ambient") { // change later?
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> g_ambient[i];
}
}
}
else if (cmd == "diffuse") { // change later?
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> g_diffuse[i] ;
}
}
}
else if (cmd == "specular") { // change later?
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> g_specular[i] ;
}
}
}
else if (cmd == "emission") { // change later?
for (int i = 0; i < 3; i++) {
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> g_emission[i] ;
}
}
}
else if (cmd == "shininess") { // change later?
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> g_shininess;
}
}
else if (cmd == "refraction") { // change later?
if (s.str().empty()) {
cerr << "Not enough arguments to 'size'\n";
throw 2;
} else {
s >> refraction;
}
}
else if (cmd == "pushTransform") {
//flag = true ;