-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayTrace.cpp
1168 lines (1009 loc) · 30.4 KB
/
RayTrace.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 <fstream>
#include <sstream>
#include <vector>
#include <iostream>
#include <math.h>
#include "linAlg.h"
#include "Ray.h"
#include "PPM.h"
#include "Pixel.h"
#include <list>
#include "Sphere.h"
#include "Light.h"
#include <algorithm>
#include <math.h>
#include <fstream>
#include "Triangle.h"
#include "Vertex.h"
#include "Intersect.h"
#include "Shaders.h"
#include <list>
/*
void loadScene(std::string file) {
//store variables and set stuff at the end
int width, height;
std::string fname = "output.bmp";
//Camera values
std::vector<float> lookAt(3);
std::vector<float> lookFrom(3);
std::vector<float> up(3);
float fov;
// store values for shape objects
// initialize in loop below
std::vector<Sphere::Sphere> spheres;
std::vector<Vertex::Vertex> vertices;
std::vector<Triangle::Triangle> tris;
// store lights
std::vector<Light::Light> lights;
std::ifstream inpfile(file.c_str());
if(!inpfile.is_open()) {
std::cout << "Unable to open file" << std::endl;
} else {
std::string line;
//MatrixStack mst;
while(inpfile.good()) {
std::vector<std::string> splitline;
std::string buf;
std::getline(inpfile,line);
std::stringstream ss(line);
while (ss >> buf) {
splitline.push_back(buf);
}
//Ignore blank lines
if(splitline.size() == 0) {
continue;
}
//Ignore comments
if(splitline[0][0] == '#') {
continue;
}
//Valid commands:
//size width height
// must be first command of file, controls image size
else if(!splitline[0].compare("size")) {
width = atoi(splitline[1].c_str());
height = atoi(splitline[2].c_str());
}
//maxdepth depth
// max # of bounces for ray (default 5)
else if(!splitline[0].compare("maxdepth")) {
// maxdepth: atoi(splitline[1].c_str())
}
//output filename
// output file to write image to
else if(!splitline[0].compare("output")) {
fname = splitline[1];
}
//camera lookfromx lookfromy lookfromz lookatx lookaty lookatz upx upy upz fov
// specifies the camera in the standard way, as in homework 2.
else if(!splitline[0].compare("camera")) {
// lookfrom:
lookFrom[0] = atof(splitline[1].c_str());
lookFrom[1] = atof(splitline[2].c_str());
lookFrom[2] = atof(splitline[3].c_str());
// lookat:
lookAt[0] = atof(splitline[4].c_str());
lookAt[1] = atof(splitline[5].c_str());
lookAt[2] = atof(splitline[6].c_str());
// up:
up[0] = atof(splitline[7].c_str());
up[1] = atof(splitline[8].c_str());
up[2] = atof(splitline[9].c_str());
// FOV
fov = atof(splitline[10].c_str());
}
//sphere x y z radius
// Defines a sphere with a given position and radius.
else if(!splitline[0].compare("sphere")) {
x: atof(splitline[1].c_str())
y: atof(splitline[1].c_str())
z: atof(splitline[1].c_str())
r: atof(splitline[4].c_str())
// Create new sphere:
spheres.push_back(Sphere::Sphere(r, x, y, z));
// Store 4 numbers
// Store current property values
// Store current top of matrix stack
}
//maxverts number
// Defines a maximum number of vertices for later triangle specifications.
// It must be set before vertices are defined.
else if(!splitline[0].compare("maxverts")) {
// Care if you want
// Here, either declare array size
// Or you can just use a STL std::vector, in which case you can ignore this
}
//maxvertnorms number
// Defines a maximum number of vertices with normals for later specifications.
// It must be set before vertices with normals are defined.
else if(!splitline[0].compare("maxvertnorms")) {
// Care if you want
}
//vertex x y z
// Defines a vertex at the given location.
// The vertex is put into a pile, starting to be numbered at 0.
else if(!splitline[0].compare("vertex")) {
x = atof(splitline[1].c_str()),
y = atof(splitline[2].c_str()),
z = atof(splitline[3].c_str()));
// Create a new vertex with these 3 values, store in some array
vertices.emplace_back(x, y, z);
}
//vertexnormal x y z nx ny nz
// Similar to the above, but define a surface normal with each vertex.
// The vertex and vertexnormal set of vertices are completely independent
// (as are maxverts and maxvertnorms).
else if(!splitline[0].compare("vertexnormal")) {
// x: atof(splitline[1].c_str()),
// y: atof(splitline[2].c_str()),
// z: atof(splitline[3].c_str()));
// nx: atof(splitline[4].c_str()),
// ny: atof(splitline[5].c_str()),
// nz: atof(splitline[6].c_str()));
// Create a new vertex+normal with these 6 values, store in some array
}
//tri v1 v2 v3
// Create a triangle out of the vertices involved (which have previously been specified with
// the vertex command). The vertices are assumed to be specified in counter-clockwise order. Your code
// should internally compute a face normal for this triangle.
else if(!splitline[0].compare("tri")) {
v1 = vertices.at(atoi(splitline[1].c_str());
v2 = vertices.at(atoi(splitline[2].c_str()));
v3 = vertices.at(atoi(splitline[3].c_str()));
// Create new triangle:
tris.emplace_back(v1, v2, v3);
// Store pointer to array of vertices
// Store 3 integers to index into array
// Store current property values
// Store current top of matrix stack
}
//trinormal v1 v2 v3
// Same as above but for vertices specified with normals.
// In this case, each vertex has an associated normal,
// and when doing shading, you should interpolate the normals
// for intermediate points on the triangle.
else if(!splitline[0].compare("trinormal")) {
// v1: atof(splitline[1].c_str())
// v2: atof(splitline[2].c_str())
// v3: atof(splitline[3].c_str())
// Create new triangle:
// Store pointer to array of vertices (Different array than above)
// Store 3 integers to index into array
// Store current property values
// Store current top of matrix stack
}
//translate x y z
// A translation 3-std::vector
else if(!splitline[0].compare("translate")) {
// x: atof(splitline[1].c_str())
// y: atof(splitline[2].c_str())
// z: atof(splitline[3].c_str())
// Update top of matrix stack
}
//rotate x y z angle
// Rotate by angle (in degrees) about the given axis as in OpenGL.
else if(!splitline[0].compare("rotate")) {
// x: atof(splitline[1].c_str())
// y: atof(splitline[2].c_str())
// z: atof(splitline[3].c_str())
// angle: atof(splitline[4].c_str())
// Update top of matrix stack
}
//scale x y z
// Scale by the corresponding amount in each axis (a non-uniform scaling).
else if(!splitline[0].compare("scale")) {
// x: atof(splitline[1].c_str())
// y: atof(splitline[2].c_str())
// z: atof(splitline[3].c_str())
// Update top of matrix stack
}
//pushTransform
// Push the current modeling transform on the stack as in OpenGL.
// You might want to do pushTransform immediately after setting
// the camera to preserve the “identity” transformation.
else if(!splitline[0].compare("pushTransform")) {
//mst.push();
}
//popTransform
// Pop the current transform from the stack as in OpenGL.
// The sequence of popTransform and pushTransform can be used if
// desired before every primitive to reset the transformation
// (assuming the initial camera transformation is on the stack as
// discussed above).
else if(!splitline[0].compare("popTransform")) {
//mst.pop();
}
//directional x y z r g b
// The direction to the light source, and the color, as in OpenGL.
else if(!splitline[0].compare("directional")) {
// x: atof(splitline[1].c_str()),
// y: atof(splitline[2].c_str()),
// z: atof(splitline[3].c_str()));
// r: atof(splitline[4].c_str()),
// g: atof(splitline[5].c_str()),
// b: atof(splitline[6].c_str()));
// add light to scene...
}
//point x y z r g b
// The location of a point source and the color, as in OpenGL.
else if(!splitline[0].compare("point")) {
// x: atof(splitline[1].c_str()),
// y: atof(splitline[2].c_str()),
// z: atof(splitline[3].c_str()));
// r: atof(splitline[4].c_str()),
// g: atof(splitline[5].c_str()),
// b: atof(splitline[6].c_str()));
// add light to scene...
}
//attenuation const linear quadratic
// Sets the constant, linear and quadratic attenuations
// (default 1,0,0) as in OpenGL.
else if(!splitline[0].compare("attenuation")) {
// const: atof(splitline[1].c_str())
// linear: atof(splitline[2].c_str())
// quadratic: atof(splitline[3].c_str())
}
//ambient r g b
// The global ambient color to be added for each object
// (default is .2,.2,.2)
else if(!splitline[0].compare("ambient")) {
// r: atof(splitline[1].c_str())
// g: atof(splitline[2].c_str())
// b: atof(splitline[3].c_str())
}
//diffuse r g b
// specifies the diffuse color of the surface.
else if(!splitline[0].compare("diffuse")) {
// r: atof(splitline[1].c_str())
// g: atof(splitline[2].c_str())
// b: atof(splitline[3].c_str())
// Update current properties
}
//specular r g b
// specifies the specular color of the surface.
else if(!splitline[0].compare("specular")) {
// r: atof(splitline[1].c_str())
// g: atof(splitline[2].c_str())
// b: atof(splitline[3].c_str())
// Update current properties
}
//shininess s
// specifies the shininess of the surface.
else if(!splitline[0].compare("shininess")) {
// shininess: atof(splitline[1].c_str())
// Update current properties
}
//emission r g b
// gives the emissive color of the surface.
else if(!splitline[0].compare("emission")) {
// r: atof(splitline[1].c_str())
// g: atof(splitline[2].c_str())
// b: atof(splitline[3].c_str())
// Update current properties
} else {
std::cerr << "Unknown command: " << splitline[0] << std::endl;
}
}
inpfile.close();
}
//more variables
std::vector<float> camDir = vSub(lookAt, lookFrom);
std::vector<float> camBasisW = vScale(-1.0, camDir);
std::vector<float> camBasisU = vCross(camBasisW, up);
std::vector<float> camBasisV = vCross(camBasisU, camBasisW);
float distance = 1.0;
float planeHeight = tan(fov/2.0);
float planeWidth = 0.0;
float vertSpace = (2.0 * planeWidth)/width;
float horizSpace = (2.0 * planeHeight)/height;
Ray::Ray ray = new Ray();
Intersect::Intersect itsct = new Intersect::Intersect();
std::vector<float> intersection(3);
PPM::PPM output = new PPM(height, width);
// Ray-trace loop
for (float w=0; w<width; w++) {
for (float h=0; h<height; h++) {
// pixel positions
float u = (-1.0 * planeWidth) + horizSpace * (w+0.5);
float v = (-1.0 * planeHeight) + vertSpace * (h+0.5);
//generate ray from h, w, camera, etc...
ray.setEye(lookFrom);
ray.setPoint(lookAt);
ray.initCamDir();
//calculate intersections for each object
pixel *p = new pixel(0, 0, 0);
//for (int i = 0; i < numSpheres; i++) {
for(std::vector<Sphere::Sphere>::iterator sphereIt = spheres.begin(); sphereIt != myvector.end(); ++it) {
// assume numSpheres is an int >= 0
// assume spheres is an array of Sphere objects
itsct = spheres.at(sphereIt).intersect(ray);
if (itsct.hit) {
intersection = ray.project(itsct.point);
vector<float> cameraVector = ray.getDir();//get the vector
vector<float> surfaceNormal = HELP! calculate surface Normal of shape
float scale = 2 * vDot(cameraVector, surfaceNormal)
vector<float> n = scale(scale, surfaceNormal)
vector<float> reflect = cameraVector - n;
vector<float> reflectedLocation
//shade and store values in image output
// for every light
// calculate shading for object point
// add values to pixel vector
//if depth > 0
//calculate shadows and reflections for that ray and surface point
// shade appropriately
// add values to pixel vector
//(*p).add(//returned shade values//);
}
//save pixel to image
output.addPixel((*p).copy());
//reset pixel
(*p).reset();
}
}
//save output file
output.save(file);
}
*/
//===============================================
// Lights
//===============================================
Light::Light(int t, float x1, float y1, float z1, float r1, float g1, float b1)
{ type = t; x = x1;
y = y1;
z = z1;
r = r1;
g = g1;
b = b1;
}
int Light::getType(){
return type;}
float Light::getX(){
return x;
}
float Light::getY(){
return y;
}
float Light::getZ(){
return x;
}
float Light::getR(){
return r;
}
float Light::getG(){
return g;
}
float Light::getB(){
return b;
}
void Light::print() {
std::cout<<"lgt [" << x << ", " << y << ", " << z << ", " << r << ", " << g << ", " << b <<"]\n";
}
//===============================================
// Linear Algebra Functions
//===============================================
float vDot(std::vector<float> a, std::vector<float> b) {
return a.at(0)*b.at(0) + a.at(1)*b.at(1) + a.at(1)*b.at(1);
}
std::vector<float> vSub(std::vector<float> a, std::vector<float> b) {
std::vector<float> retVec(3);
retVec[0] = a.at(0)-b.at(0);
retVec[1] = a.at(1)-b.at(1);
retVec[2] = a.at(2)-b.at(2);
return retVec;
}
std::vector<float> vAdd(std::vector<float> a, std::vector<float> b) {
std::vector<float> retVec(3);
retVec[0] = a.at(0)+b.at(0);
retVec[1] = a.at(1)+b.at(1);
retVec[2] = a.at(2)+b.at(2);
return retVec;
}
std::vector<float> vScale(float scalar, std::vector<float> a) {
std::vector<float> retVec(3);
retVec[0] = a.at(0)*scalar;
retVec[1] = a.at(1)*scalar;
retVec[2] = a.at(2)*scalar;
return retVec;
}
// component-wise multiplication of a 3D vector
std::vector<float> vMult(std::vector<float> a, std::vector<float> b) {
std::vector<float> retVec(3);
retVec[0] = a.at(0)*b.at(0);
retVec[1] = a.at(1)*b.at(1);
retVec[2] = a.at(2)*b.at(2);
return retVec;
}
std::vector<float> vCross(std::vector<float> a, std::vector<float> b) {
std::vector<float> retVec(3);
retVec[0] = a.at(1)*b.at(2)-a.at(2)*b.at(1);
retVec[1] = a.at(2)*b.at(0)-a.at(0)*b.at(2);
retVec[2] = a.at(0)*b.at(1)-a.at(1)*b.at(0);
return retVec;
}
void vPrint(std::vector<float> a) {
std::cout<<"vec ["<<a.at(0)<<", "<<a.at(1)<<", "<<a.at(2)<<"]\n";
}
float magnitude(std::vector<float> a) {
return a.at(0)*a.at(0)+a.at(1)*a.at(1)+a.at(2)*a.at(2);
}
std::vector<float> normalize(std::vector<float> a) {
std::vector<float> retVec(3);
float mag = magnitude(a);
retVec[0] = a.at(0)/mag;
retVec[1] = a.at(1)/mag;
retVec[2] = a.at(2)/mag;
return retVec;
}
//===============================================
// Pixel
//===============================================
Pixel::Pixel() {
r = 0;
g = 0;
b = 0;
}
Pixel::Pixel(int x, int y, int z) {
r = fmin(x, 255);
g = fmin(y, 255);
b = fmin(z, 255);
}
Pixel::Pixel(float x, float y, float z) {
r = fmin(255, round(255*x));
g = fmin(255, round(255*y));
b = fmin(255, round(255*z));
}
std::string Pixel::toStr() {
std::stringstream s;
s<<r<<" "<<g<<" "<<b;
return s.str();
}
Pixel Pixel::copy() {
Pixel *p = new Pixel();
p->setR(r);
p->setG(g);
p->setB(b);
return *p;
}
void Pixel::reset() {
r = 0;
g = 0;
b = 0;
}
void Pixel::add(Pixel p) {
this->setR(r+p.getR());
this->setG(g+p.getG());
this->setB(b+p.getB());
}
int Pixel::getR() {
return r;
}
int Pixel::getG() {
return g;
}
int Pixel::getB() {
return b;
}
void Pixel::setR(int red) {
r = fmin(red, 255);
}
void Pixel::setG(int green) {
g = fmin(green, 255);
}
void Pixel::setB(int blue) {
b = fmin(blue, 255);
}
void Pixel::print() {
std::cout<<"pix ["<<r<<", "<<g<<", "<<b<<"]\n";
}
//===============================================
// Pixel
//===============================================
PPM::PPM(int w, int h, int m){
height = h;
width = w;
maxVal = m;
std::stringstream s;
s<<"P3\n"<<width<<" "<<height<<"\n"<<maxVal<<"\n";
header = s.str();
pixels = new Pixel::Pixel[height * width];
pixelCount = 0;
}
int PPM::getPxCount() const{
return wPos;
}
int PPM::getW() const {
return width;
}
int PPM::getH() const {
return height;
}
void PPM::addPixel(Pixel p){
pixels[pixelCount] = p;
pixelCount++;
}
Pixel PPM::getPixel(int x, int y) {
return pixels[x*y+x];
}
void PPM::save(std::string name) {
std::ofstream newFile;
newFile.open("output.ppm");
newFile<<header;
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
newFile<<" "<<w<<" "<< pixels[(h*w) + w].toStr();
}
newFile<<"\n";
}
newFile.close();
}
//===============================================
// Ray
//===============================================
Ray::Ray() {}
Ray::Ray(std::vector<float> e, std::vector<float> p) {
eye = e;
point = p;
camDir = vSub(p, e);
}
void Ray::setEye(std::vector<float> e){
eye = e;
}
void Ray::setPoint(std::vector<float> p) {
point = p;
}
void Ray::initCamDir() {
camDir[0] = point[0] - eye[0];
camDir[1] = point[1] - eye[1];
camDir[2] = point[2] - eye[2];
}
std::vector<float> Ray::getEye() {
return eye;
}
std::vector<float> Ray::getPoint() {
return point;
}
std::vector<float> Ray::getDir(){
return camDir;
}
std::vector<float> Ray::project(float t) {
std::vector<float> k(3);
k[0] = t*camDir[0];
k[1] = t*camDir[1];
k[2] = t*camDir[2];
std::vector<float> a(3);
a[0] = eye[0] + k[0];
a[1] = eye[1] + k[1];
a[2] = eye[2] + k[2];
return a;
}
//===============================================
// Sphere
//===============================================
Sphere::Sphere(float r, float x, float y, float z) {
radius = r;
position = std::vector<float>(3);
position[0] = x;
position[1] = y;
position[2] = z;
}
Sphere::Sphere(float r, std::vector<float> p) {
radius = r;
position = p;
}
float Sphere::getRadius() const{
return radius;
}
std::vector<float> Sphere::getPos() const{
return position;
}
void Sphere::resize(float r) {
radius = r;
}
void Sphere::setPos(std::vector<float> p) {
position = p;
}
std::vector<float> Sphere::pointNormal(std::vector<float> point) {
std::vector<float> s(3);
s[0] = point[0] - position[0];
s[1] = point[1] - position[1];
s[2] = point[2] - position[2];
return s;
}
Intersect Sphere::intersect(Ray r) {
Intersect::Intersect ret = *new Intersect::Intersect();
std::vector<float> d = r.getDir();
std::vector<float> e = r.getEye();
std::vector<float> eminusp(3);
eminusp[0] = e[0] - position[0];
eminusp[1] = e[1] - position[1];
eminusp[2] = e[2] - position[2];
float ddotd = d[0]*d[0]+d[1]*d[1]+d[2]*d[2];
float empdot = eminusp[0]*eminusp[0] + eminusp[1]*eminusp[1] + eminusp[2]*eminusp[2];
float ddoteminusp = d[0]*eminusp[0] + d[1]*eminusp[1] + d[2]*eminusp[2];
float discriminant = sqrt(ddoteminusp * ddoteminusp - ddotd*(empdot - radius*radius));
if (discriminant >= 0) {
ret.setHit(true);
float scalar = -1.0f;
float * scale = &scalar;
std::vector<float> scaled(3);
scaled = vScale(-1, d);
//scaled[0] = d[0]*(-1);
//scaled[1] = d[1]*(-1);
//scaled[2] = d[2]*(-1);
float sdotemp = scaled[0]*eminusp[0] + scaled[1]*eminusp[1] + scaled[2]*eminusp[2];
ret.setPoint(r.project((sdotemp+discriminant)/ddotd));
}
return ret;
}
//===============================================
// Triangle
//===============================================
Triangle::Triangle(Vertex va, Vertex vb, Vertex vc) {
Vertex::Vertex v1 = va;
Vertex::Vertex v2 = vb;
Vertex::Vertex v3 = vc;
std::vector<float> normal(3);
normal[0] = (v1.getY()*v2.getZ() - v1.getZ()*v2.getY());
normal[1] = (v1.getX()*v2.getZ() - v1.getZ()*v2.getX());
normal[2] = (v1.getX()*v2.getY() - v1.getY()*v2.getX());
}
Vertex Triangle::getVertex(int n) const{
if (n == 0) {
return v1;
}
else if (n == 1) {
return v2;
}
else if (n == 2){
return v3;
}
return v1;
}
std::vector<float> Triangle::getNormal() const {
return normal;
}
Intersect Triangle::intersect(Ray r) {
Intersect::Intersect ret = *new Intersect::Intersect();
float a = v1.getX() - v2.getX();
float b = v1.getY() - v2.getY();
float c = v1.getZ() - v2.getZ();
float d = v1.getX() - v3.getX();
float e = v1.getY() - v3.getY();
float f = v1.getZ() - v3.getZ();
float g = r.getDir().at(0);
float h = r.getDir().at(1);
float i = r.getDir().at(2);
float j = v1.getX() - r.getEye().at(0);
float k = v1.getY() - r.getEye().at(1);
float l = v1.getZ() - r.getEye().at(2);
float eiminushf = e*i - h*f;
float gfminusdi = g*f - d*i;
float dhminuseg = d*h - e*g;
float akminusjb = a*k - j*b;
float jcminusal = j*c - a*l;
float blminuskc = b*l - k*c;
float m = a*(eiminushf) + b*(gfminusdi) + c*(dhminuseg);
float beta = (j*(eiminushf) + k*(gfminusdi) + l*(dhminuseg))/m;
if (beta < 0) {return ret;}
float gamma = (i*(akminusjb) + h*(jcminusal) + g*(blminuskc))/m;
if (gamma < 0 || beta+gamma > 1) {return ret;}
float t = (-1)*(f*(akminusjb) + e*(jcminusal) + d*(blminuskc))/m;
std::vector<float> p(3);
std::vector<float> vec1 = v1.toVec();
std::vector<float> vec2 = v2.toVec();
std::vector<float> vec3 = v3.toVec();
p[0] = vec1[0] + (vec2[0] - vec1[0])*beta + (vec3[0] - vec1[0])*gamma;
p[1] = vec1[1] + (vec2[1] - vec1[1])*beta + (vec3[1] - vec1[1])*gamma;
p[2] = vec1[2] + (vec2[2] - vec1[2])*beta + (vec3[2] - vec1[2])*gamma;
ret.setPoint(vec1);
return ret;
}
//===============================================
// Vertex
//===============================================
Vertex::Vertex() {
x = 0;
y = 0;
z = 0;
}
Vertex::Vertex(std::vector<float> v) {
x = v.at(0);
y = v.at(1);
z = v.at(2);
}
Vertex::Vertex(float a, float b, float c) {
x = a;
y = b;
z = c;
}
bool Vertex::equals(Vertex::Vertex v){
return (x==v.getX() && y==v.getY() && z==v.getZ());
}
float Vertex::getX() const{
return x;
}
float Vertex::getY() const{
return y;
}
float Vertex::getZ() const{
return z;
}
std::vector<float> Vertex::sub(Vertex v) {
std::vector<float> a(3);
a[0] = x-v.getX();
a[1] = y-v.getY();
a[2] = z-v.getZ();
return a;
}
std::vector<float> Vertex::toVec() {
std::vector<float> n(3);
n[0] = x;
n[1] = y;
n[2] = z;
return n;
}
void Vertex::print() {
std::cout<<"vtx ["<< x << ", " << y << ", " << z << "]\n";
}
//===============================================
// Intersect
//===============================================
Intersect::Intersect() {
hit = false;
}
void Intersect::setHit(bool h) {
hit = h;
}
void Intersect::setPoint(std::vector<float> p) {
point = p;
}
bool Intersect::isHit() const {
return hit;
}
std::vector<float> Intersect::getPoint() const{
return point;
}
//===============================================
// Shaders
//===============================================
/*
Shaders::Shaders() {
numLights = 0;
numSpheres = 0;
numTriangles = 0;
std::vector<Light> l(10);
lightSize = 10;
triangleSize = 10;
sphereSize = 10;
lights = l;
std::vector<Triangle> t(10);
std::vector<Sphere> s(10);
triangles = t;
spheres = s;
}
void Shaders::addLight(Light l){
lights[numLights] = l;
numLights = numLights + 1;
if(numLights == lightSize - 1){
lights.resize(lightSize * 2);
lightSize = lightSize * 2;
}
}
void Shaders::addSphere(Sphere s){
spheres[numSpheres] = s;
numSpheres = numSpheres + 1;
if(numSpheres == sphereSize - 1){
spheres.resize(sphereSize * 2);
sphereSize = sphereSize * 2;
}
}
void Shaders::addTriangle(Triangle t){
triangles[numTriangles] = t;
numTriangles = numTriangles + 1;
if(numTriangles == triangleSize - 1){
triangles.resize(triangleSize * 2);
triangleSize = triangleSize * 2;
}
}
Pixel Shaders::pixelLight(float x, float y, float z){
std::vector<float> color(3);
for (int i = 0; i < numLights; i++){
Light l = lights[i];
if(l.getType() == 0) { //directional Light
std::vector<float> lightDir(3);
lightDir[0] = l.getX();
lightDir[1] = l.getY();
lightDir[2] = l.getZ();
std::vector<float> lightColor(3);
lightColor[0] = l.getR();
lightColor[1] = l.getG();
lightColor[2] = l.getB();
std::vector<float> amb = this->ambient(lightColor);
color = vAdd(color, amb);
if(this->hasShadow(x, y, z, l) == 0){ //if light isn't blocked by another object
std::vector<float> diff = this->diffuse(surfaceDir, center, scale(-1,lightDir), lightColor);
color = vAdd(color, diff);
std::vector<float> spec = this->specular(surfaceDir, center, viewer, lightDir, lightColor);
color = vAdd(color, spec);
}
}
else { //point light
std::vector<float> lightPos(3);
lightPos[0] = l.getX();
lightPos[1] = l.getY();
lightPos[2] = l.getZ();
std::vector<float> lightColor(3);
lightColor[0] = l.getR();
lightColor[1] = l.getG();
lightColor[2] = l.getB();
std::vector<float> lightDir = vSub(surfaceDir, lightPos);
std::vector<float> a = ambient(lightColor);
color = vAdd(color, a);
if(this->hasShadow(x, y, l) == 0){ //if light isn't blocked by another object
std::vector<float> diff = diffuse(surfaceDir, center, lightDir, lightColor);
color = vAdd(color, diff);
std::vector<float> spec = specular(surfaceDir, center, viewer, scale(-1,lightDir), lightColor);
color = vAdd(color, spec);
}
}
}
}
Pixel pixelColor = new Pixel(color[0], color[1], color[2]);
return pixelColor;
}
std::vector<float> Shaders::ambient(std::vector<float> lightColor[]){
return vMult(ka, lightColor);
}
std::vector<float> Shaders::diffuse(std::vector<float> surfaceDir, std::vector<float> center, std::vector<float> lightDir, std::vector<float> lightColor){
std::vector<float> n = normalize(surfaceDir);
std::vector<float> l = normalize(scale((-1),lightDir));
float lambert = vDot(n, l);
// no max function in Ubuntu environment