-
Notifications
You must be signed in to change notification settings - Fork 2
/
path tracer.cpp
1777 lines (1450 loc) · 40.5 KB
/
path tracer.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
// Yiheng Zhang 515030910216
// CG Course Assignment2
// part of the code like math ultilities are taken from smallVCM open source code
#include <vector>
#include <cmath>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>
#include <set>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <fstream>
#define EPS_PHONG 1e-3f
#define PI_F 3.14159265358979f
#define INV_PI_F (1.f / PI_F)
#define EPS_COSINE 1e-6f
#define EPS_RAY 1e-4f
//////////////////////////////////////////////////////////////////////////
// Math and Utilities
// References: http://www.smallvcm.com
#pragma region MATHUTIL
template<typename T>
T Sqr(const T& a) { return a*a; }
typedef unsigned uint;
template<typename T>
class Vec2x
{
public:
Vec2x() {}
Vec2x(T a) :x(a), y(a) {}
Vec2x(T a, T b) :x(a), y(b) {}
const T& Get(int a) const { return reinterpret_cast<const T*>(this)[a]; }
T& Get(int a) { return reinterpret_cast<T*>(this)[a]; }
// unary minus
Vec2x<T> operator-() const
{
Vec2x<T> res; for (int i = 0; i < 2; i++) res.Get(i) = -Get(i); return res;
}
// binary operations
friend Vec2x<T> operator+(const Vec2x& a, const Vec2x& b)
{
Vec2x<T> res; for (int i = 0; i < 2; i++) res.Get(i) = a.Get(i) + b.Get(i); return res;
}
friend Vec2x<T> operator-(const Vec2x& a, const Vec2x& b)
{
Vec2x<T> res; for (int i = 0; i < 2; i++) res.Get(i) = a.Get(i) - b.Get(i); return res;
}
friend Vec2x<T> operator*(const Vec2x& a, const Vec2x& b)
{
Vec2x<T> res; for (int i = 0; i < 2; i++) res.Get(i) = a.Get(i) * b.Get(i); return res;
}
friend Vec2x<T> operator/(const Vec2x& a, const Vec2x& b)
{
Vec2x<T> res; for (int i = 0; i < 2; i++) res.Get(i) = a.Get(i) / b.Get(i); return res;
}
Vec2x<T>& operator+=(const Vec2x& a)
{
for (int i = 0; i < 2; i++) Get(i) += a.Get(i); return *this;
}
Vec2x<T>& operator-=(const Vec2x& a)
{
for (int i = 0; i < 2; i++) Get(i) -= a.Get(i); return *this;
}
Vec2x<T>& operator*=(const Vec2x& a)
{
for (int i = 0; i < 2; i++) Get(i) *= a.Get(i); return *this;
}
Vec2x<T>& operator/=(const Vec2x& a)
{
for (int i = 0; i < 2; i++) Get(i) /= a.Get(i); return *this;
}
friend T Dot(const Vec2x& a, const Vec2x& b)
{
T res(0); for (int i = 0; i < 2; i++) res += a.Get(i) * b.Get(i); return res;
}
public:
T x, y;
};
typedef Vec2x<float> Vec2f;
typedef Vec2x<int> Vec2i;
template<typename T>
class Vec3x
{
public:
Vec3x() {}
Vec3x(T a) :x(a), y(a), z(a) {}
Vec3x(T a, T b, T c) :x(a), y(b), z(c) {}
const T& Get(int a) const { return reinterpret_cast<const T*>(this)[a]; }
T& Get(int a) { return reinterpret_cast<T*>(this)[a]; }
Vec2x<T> GetXY() const { return Vec2x<T>(x, y); }
T Max() const { T res = Get(0); for (int i = 1; i < 3; i++) res = std::max(res, Get(i)); return res; }
bool IsZero() const
{
for (int i = 0; i < 3; i++)
if (Get(i) != 0)
return false;
return true;
}
// unary minus
Vec3x<T> operator-() const
{
Vec3x<T> res; for (int i = 0; i < 3; i++) res.Get(i) = -Get(i); return res;
}
// binary operations
friend Vec3x<T> operator+(const Vec3x& a, const Vec3x& b)
{
Vec3x<T> res; for (int i = 0; i < 3; i++) res.Get(i) = a.Get(i) + b.Get(i); return res;
}
friend Vec3x<T> operator-(const Vec3x& a, const Vec3x& b)
{
Vec3x<T> res; for (int i = 0; i < 3; i++) res.Get(i) = a.Get(i) - b.Get(i); return res;
}
friend Vec3x<T> operator*(const Vec3x& a, const Vec3x& b)
{
Vec3x<T> res; for (int i = 0; i < 3; i++) res.Get(i) = a.Get(i) * b.Get(i); return res;
}
friend Vec3x<T> operator/(const Vec3x& a, const Vec3x& b)
{
Vec3x<T> res; for (int i = 0; i < 3; i++) res.Get(i) = a.Get(i) / b.Get(i); return res;
}
Vec3x<T>& operator+=(const Vec3x& a)
{
for (int i = 0; i < 3; i++) Get(i) += a.Get(i); return *this;
}
Vec3x<T>& operator-=(const Vec3x& a)
{
for (int i = 0; i < 3; i++) Get(i) -= a.Get(i); return *this;
}
Vec3x<T>& operator*=(const Vec3x& a)
{
for (int i = 0; i < 3; i++) Get(i) *= a.Get(i); return *this;
}
Vec3x<T>& operator/=(const Vec3x& a)
{
for (int i = 0; i < 3; i++) Get(i) /= a.Get(i); return *this;
}
friend T Dot(const Vec3x& a, const Vec3x& b)
{
T res(0); for (int i = 0; i < 3; i++) res += a.Get(i) * b.Get(i); return res;
}
float LenSqr() const { return Dot(*this, *this); }
float Length() const { return std::sqrt(LenSqr()); }
public:
T x, y, z;
};
typedef Vec3x<float> Vec3f;
typedef Vec3x<int> Vec3i;
Vec3f Cross(
const Vec3f &a,
const Vec3f &b)
{
Vec3f res;
res.x = a.y * b.z - a.z * b.y;
res.y = a.z * b.x - a.x * b.z;
res.z = a.x * b.y - a.y * b.x;
return res;
}
Vec3f Normalize(const Vec3f& a)
{
const float lenSqr = Dot(a, a);
const float len = std::sqrt(lenSqr);
return a / len;
}
class Mat4f
{
public:
Mat4f() {}
Mat4f(float a) { for (int i = 0; i < 16; i++) GetPtr()[i] = a; }
const float* GetPtr() const { return reinterpret_cast<const float*>(this); }
float* GetPtr() { return reinterpret_cast<float*>(this); }
const float& Get(int r, int c) const { return GetPtr()[r + c * 4]; }
float& Get(int r, int c) { return GetPtr()[r + c * 4]; }
void SetRow(int r, float a, float b, float c, float d)
{
Get(r, 0) = a;
Get(r, 1) = b;
Get(r, 2) = c;
Get(r, 3) = d;
}
void SetRow(int r, const Vec3f &a, float b)
{
for (int i = 0; i < 3; i++)
Get(r, i) = a.Get(i);
Get(r, 3) = b;
}
Vec3f TransformVector(const Vec3f& aVec) const
{
Vec3f res(0);
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
res.Get(r) += aVec.Get(c) * Get(r, c);
return res;
}
Vec3f TransformPoint(const Vec3f& aVec) const
{
float w = Get(3, 3);
for (int c = 0; c < 3; c++)
w += Get(3, c) * aVec.Get(c);
const float invW = 1.f / w;
Vec3f res(0);
for (int r = 0; r < 3; r++)
{
res.Get(r) = Get(r, 3);
for (int c = 0; c < 3; c++)
res.Get(r) += aVec.Get(c) * Get(r, c);
res.Get(r) *= invW;
}
return res;
}
static Mat4f Zero() { Mat4f res(0); return res; }
static Mat4f Identity()
{
Mat4f res(0);
for (int i = 0; i < 4; i++) res.Get(i, i) = 1.f;
return res;
}
static Mat4f Scale(const Vec3f& aScale)
{
Mat4f res = Mat4f::Identity();
for (int i = 0; i < 3; i++) res.Get(i, i) = aScale.Get(i);
res.Get(3, 3) = 1;
return res;
}
static Mat4f Translate(const Vec3f& aScale)
{
Mat4f res = Mat4f::Identity();
for (int i = 0; i < 3; i++) res.Get(i, 3) = aScale.Get(i);
res.Get(3, 3) = 1;
return res;
}
static Mat4f Perspective(
float aFov,
float aNear,
float aFar)
{
// Camera points towards -z. 0 < near < far.
// Matrix maps z range [-near, -far] to [-1, 1], after homogeneous division.
float f = 1.f / (std::tan(aFov * PI_F / 360.0f));
float d = 1.f / (aNear - aFar);
Mat4f r;
r.m00 = f; r.m01 = 0.0f; r.m02 = 0.0f; r.m03 = 0.0f;
r.m10 = 0.0f; r.m11 = -f; r.m12 = 0.0f; r.m13 = 0.0f;
r.m20 = 0.0f; r.m21 = 0.0f; r.m22 = (aNear + aFar) * d; r.m23 = 2.0f * aNear * aFar * d;
r.m30 = 0.0f; r.m31 = 0.0f; r.m32 = -1.0f; r.m33 = 0.0f;
return r;
}
public:
// m_row_col; stored column major
float m00, m10, m20, m30;
float m01, m11, m21, m31;
float m02, m12, m22, m32;
float m03, m13, m23, m33;
};
Mat4f operator*(const Mat4f& left, const Mat4f& right)
{
Mat4f res(0);
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
for (int i = 0; i < 4; i++)
res.Get(row, col) += left.Get(row, i) * right.Get(i, col);
return res;
}
// Code for inversion taken from:
// http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
Mat4f Invert(const Mat4f& aMatrix)
{
const float *m = aMatrix.GetPtr();
float inv[16], det;
int i;
inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return Mat4f::Identity();
det = 1.f / det;
Mat4f res;
for (i = 0; i < 16; i++)
res.GetPtr()[i] = inv[i] * det;
return res;
}
float Luminance(const Vec3f& aRGB)
{
return 0.212671f * aRGB.x +
0.715160f * aRGB.y +
0.072169f * aRGB.z;
}
float FresnelDielectric(
float aCosInc,
float IOR)
{
if (IOR < 0)
return 1.f;
float eta;
if (aCosInc < 0.f)
{
aCosInc = -aCosInc;
eta = IOR;
}
else
{
eta = 1.f / IOR;
}
const float sinTrans2 = Sqr(eta) * (1.f - Sqr(aCosInc));
const float cosTrans = std::sqrt(std::max(0.f, 1.f - sinTrans2));
const float term1 = eta * cosTrans;
const float rParallel =
(aCosInc - term1) / (aCosInc + term1);
const float term2 = eta * aCosInc;
const float rPerpendicular =
(term2 - cosTrans) / (term2 + cosTrans);
return 0.5f * (Sqr(rParallel) + Sqr(rPerpendicular));
}
Vec3f ReflectLocal(const Vec3f& aVector)
{
return Vec3f(-aVector.x, -aVector.y, aVector.z);
}
//////////////////////////////////////////////////////////////////////////
// Cosine lobe hemisphere sampling
Vec3f SamplePowerCosHemisphereW(
const Vec2f &aSamples,
const float aPower,
float *oPdfW)
{
const float term1 = 2.f * PI_F * aSamples.x;
const float term2 = std::pow(aSamples.y, 1.f / (aPower + 1.f));
const float term3 = std::sqrt(1.f - term2 * term2);
if (oPdfW)
{
*oPdfW = (aPower + 1.f) * std::pow(term2, aPower) * (0.5f * INV_PI_F);
}
return Vec3f(
std::cos(term1) * term3,
std::sin(term1) * term3,
term2);
}
float PowerCosHemispherePdfW(
const Vec3f &aNormal,
const Vec3f &aDirection,
const float aPower)
{
const float cosTheta = std::max(0.f, Dot(aNormal, aDirection));
return (aPower + 1.f) * std::pow(cosTheta, aPower) * (INV_PI_F * 0.5f);
}
//////////////////////////////////////////////////////////////////////////
/// Sample direction in the upper hemisphere with cosine-proportional pdf
/** The returned PDF is with respect to solid angle measure */
Vec3f SampleCosHemisphereW(
const Vec2f &aSamples,
float *oPdfW)
{
const float term1 = 2.f * PI_F * aSamples.x;
const float term2 = std::sqrt(1.f - aSamples.y);
const Vec3f ret(
std::cos(term1) * term2,
std::sin(term1) * term2,
std::sqrt(aSamples.y));
if (oPdfW)
{
*oPdfW = ret.z * INV_PI_F;
}
return ret;
}
// Sample Triangle
// returns barycentric coordinates
Vec2f SampleUniformTriangle(const Vec2f &aSamples)
{
const float term = std::sqrt(aSamples.x);
return Vec2f(1.f - term, aSamples.y * term);
}
float PdfAtoW(
const float aPdfA,
const float aDist,
const float aCosThere)
{
return aPdfA * Sqr(aDist) / std::abs(aCosThere);
}
#pragma endregion MATHUTIL
//////////////////////////////////////////////////////////////////////////
// Random Number Generator = RNG
class RNG
{
public:
RNG(int aSeed = 1234) : randGen(aSeed) {}
float GetFloat()
{
return mDistFloat(randGen);
}
Vec2f GetVec2f()
{
return Vec2f(GetFloat(), GetFloat());
}
Vec3f GetVec3f()
{
return Vec3f(GetFloat(), GetFloat(), GetFloat());
}
private:
std::mt19937_64 randGen;
std::uniform_real_distribution<float> mDistFloat;
};
//////////////////////////////////////////////////////////////////////////
// BMP TOOL
// References: http://www.smallvcm.com
#pragma region BMPUTIL
class Frame
{
public:
Frame()
{
mX = Vec3f(1, 0, 0);
mY = Vec3f(0, 1, 0);
mZ = Vec3f(0, 0, 1);
};
Frame(const Vec3f& x, const Vec3f& y, const Vec3f& z) : mX(x), mY(y), mZ(z) {}
void SetFromZ(const Vec3f& z)
{
Vec3f tmpZ = mZ = Normalize(z);
Vec3f tmpX = (std::abs(tmpZ.x) > 0.99f) ? Vec3f(0, 1, 0) : Vec3f(1, 0, 0);
mY = Normalize(Cross(tmpZ, tmpX));
mX = Cross(mY, tmpZ);
}
Vec3f ToWorld(const Vec3f& a) const
{
return mX * a.x + mY * a.y + mZ * a.z;
}
Vec3f ToLocal(const Vec3f& a) const
{
return Vec3f(Dot(a, mX), Dot(a, mY), Dot(a, mZ));
}
public:
Vec3f mX, mY, mZ;
};
class Framebuffer
{
public:
Framebuffer()
{}
void AddColor(
const Vec2f& aSample,
const Vec3f& aColor)
{
if (aSample.x < 0 || aSample.x >= resX)
return;
if (aSample.y < 0 || aSample.y >= resY)
return;
int x = int(aSample.x);
int y = int(aSample.y);
mColor[x + y * resX] = mColor[x + y * resX] + aColor;
}
void Setup(const Vec2f& aResolution)
{
resX = int(aResolution.x);
resY = int(aResolution.y);
mColor.resize(resX * resY);
memset(&mColor[0], 0, sizeof(Vec3f) * mColor.size());
}
void Scale(float aScale)
{
for (size_t i = 0; i < mColor.size(); i++)
mColor[i] = mColor[i] * Vec3f(aScale);
}
// Saving BMP
struct BmpHeader
{
uint mFileSize; // Size of file in bytes
uint mReserved01; // 2x 2 reserved bytes
uint mDataOffset; // Offset in bytes where data can be found (54)
uint mHeaderSize; // 40B
int mWidth; // Width in pixels
int mHeight; // Height in pixels
short mColorPlates; // Must be 1
short mBitsPerPixel; // We use 24bpp
uint mCompression; // We use BI_RGB ~ 0, uncompressed
uint mImageSize; // mWidth x mHeight x 3B
uint mHorizRes; // Pixels per meter (75dpi ~ 2953ppm)
uint mVertRes; // Pixels per meter (75dpi ~ 2953ppm)
uint mPaletteColors; // Not using palette - 0
uint mImportantColors; // 0 - all are important
};
void SaveBMP(const char *aFilename)
{
std::ofstream bmp(aFilename, std::ios::binary);
BmpHeader header;
bmp.write("BM", 2);
header.mFileSize = uint(sizeof(BmpHeader) + 2) + resX * resY * 3;
header.mReserved01 = 0;
header.mDataOffset = uint(sizeof(BmpHeader) + 2);
header.mHeaderSize = 40;
header.mWidth = resX;
header.mHeight = resY;
header.mColorPlates = 1;
header.mBitsPerPixel = 24;
header.mCompression = 0;
header.mImageSize = resX * resY * 3;
header.mHorizRes = 2953;
header.mVertRes = 2953;
header.mPaletteColors = 0;
header.mImportantColors = 0;
bmp.write((char*)&header, sizeof(header));
const float gamma = 0.55f;
for (int y = 0; y < resY; y++)
{
for (int x = 0; x < resX; x++)
{
// bmp is stored from bottom up
const Vec3f &rgbF = mColor[x + (resY - y - 1)*resX];
typedef unsigned char byte;
float gammaBgr[3];
gammaBgr[0] = std::pow(rgbF.z, gamma) * 255.f;
gammaBgr[1] = std::pow(rgbF.y, gamma) * 255.f;
gammaBgr[2] = std::pow(rgbF.x, gamma) * 255.f;
byte bgrB[3];
bgrB[0] = byte(std::min(255.f, std::max(0.f, gammaBgr[0])));
bgrB[1] = byte(std::min(255.f, std::max(0.f, gammaBgr[1])));
bgrB[2] = byte(std::min(255.f, std::max(0.f, gammaBgr[2])));
bmp.write((char*)&bgrB, sizeof(bgrB));
}
}
}
private:
std::vector<Vec3f> mColor;
int resX;
int resY;
};
#pragma endregion BMPUTIL
//////////////////////////////////////////////////////////////////////////
// Materials
class Material
{
public:
Material()
{
Reset();
}
void Reset()
{
diffuseFact = Vec3f(0);
PhongFact = Vec3f(0);
PhongExp = 1.f;
mirrorFact = Vec3f(0);
IOR = -1.f;
}
public:
Vec3f diffuseFact;
Vec3f PhongFact;
float PhongExp;
Vec3f mirrorFact;
float IOR;
};
class Ray
{
public:
Ray()
{}
Ray(const Vec3f& aOrg, const Vec3f& aDir) : org(aOrg), dir(aDir) {}
public:
Vec3f org; //!< Ray origin
Vec3f dir; //!< Ray direction
};
class Intersection
{
public:
Intersection()
{}
Intersection(float aMaxDist) :dist(aMaxDist)
{}
public:
float dist; //!< Distance to closest intersection (serves as ray.tmax)
int matID; //!< ID of intersected material
Vec3f normal; //!< Normal at the intersection
};
//////////////////////////////////////////////////////////////////////////
// Geometry
// References: http://www.smallvcm.com
#pragma region GEOMETRY
class AbstractGeometry
{
public:
virtual ~AbstractGeometry() {};
// Finds the closest intersection
virtual bool Intersect(const Ray& aRay, Intersection& oResult) const = 0;
// Finds any intersection, default calls Intersect
virtual bool IntersectP(const Ray& aRay, Intersection& oResult) const
{
return Intersect(aRay, oResult);
}
// Grows given bounding box by this object
virtual void GrowBBox(Vec3f &aoBBoxMin, Vec3f &aoBBoxMax) = 0;
};
class GeometryList : public AbstractGeometry
{
public:
virtual ~GeometryList()
{
for (int i = 0; i < (int)geomtry.size(); i++)
delete geomtry[i];
};
virtual bool Intersect(const Ray& aRay, Intersection& oResult) const
{
bool anyIntersection = false;
for (int i = 0; i < (int)geomtry.size(); i++)
{
bool hit = geomtry[i]->Intersect(aRay, oResult);
if (hit)
anyIntersection = hit;
}
return anyIntersection;
}
virtual bool IntersectP(
const Ray &aRay,
Intersection &oResult) const
{
for (int i = 0; i < (int)geomtry.size(); i++)
{
if (geomtry[i]->IntersectP(aRay, oResult))
return true;
}
return false;
}
virtual void GrowBBox(
Vec3f &aoBBoxMin,
Vec3f &aoBBoxMax)
{
for (int i = 0; i < (int)geomtry.size(); i++)
geomtry[i]->GrowBBox(aoBBoxMin, aoBBoxMax);
}
public:
std::vector<AbstractGeometry*> geomtry;
};
class Triangle : public AbstractGeometry
{
public:
Triangle() {}
Triangle(
const Vec3f &p0,
const Vec3f &p1,
const Vec3f &p2,
int aMatID)
{
p[0] = p0;
p[1] = p1;
p[2] = p2;
matID = aMatID;
mNormal = Normalize(Cross(p[1] - p[0], p[2] - p[0]));
}
virtual bool Intersect(
const Ray &aRay,
Intersection &oResult) const
{
const Vec3f ao = p[0] - aRay.org;
const Vec3f bo = p[1] - aRay.org;
const Vec3f co = p[2] - aRay.org;
const Vec3f v0 = Cross(co, bo);
const Vec3f v1 = Cross(bo, ao);
const Vec3f v2 = Cross(ao, co);
const float v0d = Dot(v0, aRay.dir);
const float v1d = Dot(v1, aRay.dir);
const float v2d = Dot(v2, aRay.dir);
if (((v0d < 0.f) && (v1d < 0.f) && (v2d < 0.f)) ||
((v0d >= 0.f) && (v1d >= 0.f) && (v2d >= 0.f)))
{
const float distance = Dot(mNormal, ao) / Dot(mNormal, aRay.dir);
if ((distance > 0) && (distance < oResult.dist))
{
oResult.normal = mNormal;
oResult.matID = matID;
oResult.dist = distance;
return true;
}
}
return false;
}
virtual void GrowBBox(
Vec3f &aoBBoxMin,
Vec3f &aoBBoxMax)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
aoBBoxMin.Get(j) = std::min(aoBBoxMin.Get(j), p[i].Get(j));
aoBBoxMax.Get(j) = std::max(aoBBoxMax.Get(j), p[i].Get(j));
}
}
}
public:
Vec3f p[3];
int matID;
Vec3f mNormal;
};
class Sphere : public AbstractGeometry
{
public:
Sphere() {}
Sphere(
const Vec3f &aCenter,
float aRadius,
int aMatID)
{
center = aCenter;
radius = aRadius;
matID = aMatID;
}
// Taken from:
// http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection
virtual bool Intersect(
const Ray &aRay,
Intersection &oResult) const
{
// we transform ray origin into object space (center == origin)
const Vec3f transformedOrigin = aRay.org - center;
const float A = Dot(aRay.dir, aRay.dir);
const float B = 2 * Dot(aRay.dir, transformedOrigin);
const float C = Dot(transformedOrigin, transformedOrigin) - (radius * radius);
// Must use doubles, because when B ~ sqrt(B*B - 4*A*C)
// the resulting t is imprecise enough to get around ray epsilons
const double disc = B*B - 4 * A*C;
if (disc < 0)
return false;
const double discSqrt = std::sqrt(disc);
const double q = (B < 0) ? ((-B - discSqrt) / 2.f) : ((-B + discSqrt) / 2.f);
double t0 = q / A;
double t1 = C / q;
if (t0 > t1) std::swap(t0, t1);
float resT;
if (t0 > 0 && t0 < oResult.dist)
resT = float(t0);
else if (t1 > 0 && t1 < oResult.dist)
resT = float(t1);
else