-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.glsl
More file actions
3281 lines (2847 loc) · 70.8 KB
/
common.glsl
File metadata and controls
3281 lines (2847 loc) · 70.8 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
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
#define SHADERTOY 0
precision highp float;
precision highp int;
/*_________________ constants _________________*/
// scene IDs
#define SCENE_CORNELL_BOX 0
#define SCENE_LIGHT_IN_OTHER_ROOM 1
// current scene selector
#define CURRENT_SCENE SCENE_CORNELL_BOX
// using 1 spp will cause blurriness when using frame blending due to subpixel
// jittering. the issue is solved when using a higher sample count because the
// first sample is always at the center of the pixel, resulting in sharp
// temporal accumulation (see the path tracing buffer).
const int N_SAMPLES_PER_PIXEL = 3;
// number of extra samples to collect when frame blending isn't possible
const int N_EXTRA_SAMPLES_WHEN_NOT_BLENDING = 2;
// if this is false, N_SAMPLES_PER_PIXEL + N_EXTRA_SAMPLES_WHEN_NOT_BLENDING
// samples will be used.
const bool TEMPORAL_ACCUMULATION = true;
// number of bounces
#if CURRENT_SCENE == SCENE_CORNELL_BOX
const int N_BOUNCES = 3;
#elif CURRENT_SCENE == SCENE_LIGHT_IN_OTHER_ROOM
const int N_BOUNCES = 5;
#else
const int N_BOUNCES = 3;
#endif
const float BOUNCE_OFFSET = .0005;
// render resolution factor. must be in the 0-1 range.
const float RES_FAC = .8;
// final image rendering
const bool IMAGE_DEBUG_DEPTH = false;
const bool IMAGE_DEBUG_MOTION_VEC = false;
const bool IMAGE_DEBUG_BLEND_ITER = false;
const bool IMAGE_APPLY_CDL = true;
const bool IMAGE_USE_FLIM = true;
const bool IMAGE_DITHER = true;
// input controls
const float MOUSE_SENSITIVITY = 1.2;
const float MOVEMENT_SPEED = 1.6;
// if we read from an uninitialized pixel, we might get 0, that's why the
// default value for object IDs and material IDs is 1. other IDs should start
// from 2.
const int OBJ_NONE = 1;
const int MAT_NONE = 1;
/*_________________ math utils ________________*/
const float PI = 3.141592653589793238462643383;
const float TAU = 6.283185307179586476925286767;
const float PI_OVER_2 = 1.570796326794896619231321692;
const float INV_PI = .318309886183790671537767527;
const float INV_TAU = .159154943091895335768883763;
#define FUNC_LERP(T) \
T lerp(T a, T b, float t) \
{ \
return a + t * (b - a); \
}
#define FUNC_WRAP(T) \
T wrap(T v, float start, float end) \
{ \
return start + mod(v - start, end - start); \
}
#define FUNC_REMAP(T) \
T remap(T v, float inp_start, float inp_end, float out_start, float out_end) \
{ \
return out_start \
+ ((out_end - out_start) / (inp_end - inp_start)) * (v - inp_start); \
}
#define FUNC_REMAP_CLAMP(T) \
T remap_clamp( \
T v, \
float inp_start, \
float inp_end, \
float out_start, \
float out_end \
) \
{ \
T t = clamp((v - inp_start) / (inp_end - inp_start), 0., 1.); \
return out_start + t * (out_end - out_start); \
}
#define FUNC_REMAP01(T) \
T remap01(T v, float inp_start, float inp_end) \
{ \
return clamp((v - inp_start) / (inp_end - inp_start), 0., 1.); \
}
#define FUNC_LENGTH_SQ(T) \
float length_sq(T v) \
{ \
return dot(v, v); \
}
#define FUNC_DIST_SQ(T) \
float dist_sq(T a, T b) \
{ \
a -= b; \
return dot(a, a); \
}
FUNC_LERP(float)
FUNC_LERP(vec2)
FUNC_LERP(vec3)
FUNC_LERP(vec4)
FUNC_WRAP(float)
FUNC_WRAP(vec2)
FUNC_WRAP(vec3)
FUNC_WRAP(vec4)
FUNC_REMAP(float)
FUNC_REMAP(vec2)
FUNC_REMAP(vec3)
FUNC_REMAP(vec4)
FUNC_REMAP_CLAMP(float)
FUNC_REMAP_CLAMP(vec2)
FUNC_REMAP_CLAMP(vec3)
FUNC_REMAP_CLAMP(vec4)
FUNC_REMAP01(float)
FUNC_REMAP01(vec2)
FUNC_REMAP01(vec3)
FUNC_REMAP01(vec4)
FUNC_LENGTH_SQ(vec2)
FUNC_LENGTH_SQ(vec3)
FUNC_LENGTH_SQ(vec4)
FUNC_DIST_SQ(vec2)
FUNC_DIST_SQ(vec3)
FUNC_DIST_SQ(vec4)
int imin(int a, int b)
{
if (a < b)
{
return a;
}
return b;
}
int imax(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int iclamp(int v, int start, int end)
{
if (v < start)
{
v = start;
}
if (v > end)
{
v = end;
}
return v;
}
float min_component(vec2 v)
{
return min(v.x, v.y);
}
float min_component(vec3 v)
{
return min(min(v.x, v.y), v.z);
}
float min_component(vec4 v)
{
return min(min(min(v.x, v.y), v.z), v.w);
}
float max_component(vec2 v)
{
return max(v.x, v.y);
}
float max_component(vec3 v)
{
return max(max(v.x, v.y), v.z);
}
float max_component(vec4 v)
{
return max(max(max(v.x, v.y), v.z), v.w);
}
int min_component(ivec2 v)
{
return imin(v.x, v.y);
}
int min_component(ivec3 v)
{
return imin(imin(v.x, v.y), v.z);
}
int min_component(ivec4 v)
{
return imin(imin(imin(v.x, v.y), v.z), v.w);
}
int max_component(ivec2 v)
{
return imax(v.x, v.y);
}
int max_component(ivec3 v)
{
return imax(imax(v.x, v.y), v.z);
}
int max_component(ivec4 v)
{
return imax(imax(imax(v.x, v.y), v.z), v.w);
}
// |a| * |b| * sin(theta)
float cross2d(vec2 a, vec2 b)
{
return a.x * b.y - a.y * b.x;
}
// references for barycentric coordinates
// https://www.desmos.com/calculator/8g8xjejuox
// https://www.shadertoy.com/view/mdjBWK
vec3 cartesian_to_barycentric(
vec2 p,
vec2 v0,
vec2 v1,
vec2 v2,
bool clamp_,
out bool p_is_outside
)
{
vec3 b = vec3(
cross2d(v1 - p, v2 - p),
cross2d(v2 - p, v0 - p),
cross2d(v0 - p, v1 - p)
) / cross2d(v1 - v0, v2 - v0);
p_is_outside = min(min(b.x, b.y), b.z) < 0.;
if (clamp_)
{
b = max(b, 0.);
b /= (b.x + b.y + b.z);
}
return b;
}
vec3 cartesian_to_barycentric(
vec3 p,
vec3 v0,
vec3 v1,
vec3 v2,
bool clamp_,
out bool p_is_outside
)
{
vec3 b = vec3(
length(cross(v1 - p, v2 - p)),
length(cross(v2 - p, v0 - p)),
length(cross(v0 - p, v1 - p))
) / length(cross(v1 - v0, v2 - v0));
p_is_outside = min(min(b.x, b.y), b.z) < 0.;
if (clamp_)
{
b = max(b, 0.);
b /= (b.x + b.y + b.z);
}
return b;
}
float barycentric_interpolate(vec3 b, float v0, float v1, float v2)
{
return dot(b, vec3(v0, v1, v2));
}
vec2 barycentric_interpolate(vec3 b, vec2 v0, vec2 v1, vec2 v2)
{
return b.x * v0 + b.y * v1 + b.z * v2;
}
vec3 barycentric_interpolate(vec3 b, vec3 v0, vec3 v1, vec3 v2)
{
return b.x * v0 + b.y * v1 + b.z * v2;
}
vec4 barycentric_interpolate(vec3 b, vec4 v0, vec4 v1, vec4 v2)
{
return b.x * v0 + b.y * v1 + b.z * v2;
}
float barycentric_interpolate(vec2 b, float v0, float v1, float v2)
{
return barycentric_interpolate(
vec3(b.x, b.y, 1. - b.x - b.y),
v0, v1, v2
);
}
vec2 barycentric_interpolate(vec2 b, vec2 v0, vec2 v1, vec2 v2)
{
return barycentric_interpolate(
vec3(b.x, b.y, 1. - b.x - b.y),
v0, v1, v2
);
}
vec3 barycentric_interpolate(vec2 b, vec3 v0, vec3 v1, vec3 v2)
{
return barycentric_interpolate(
vec3(b.x, b.y, 1. - b.x - b.y),
v0, v1, v2
);
}
vec4 barycentric_interpolate(vec2 b, vec4 v0, vec4 v1, vec4 v2)
{
return barycentric_interpolate(
vec3(b.x, b.y, 1. - b.x - b.y),
v0, v1, v2
);
}
// angle from 0 to TAU
float get_angle(vec2 p)
{
float a = atan(p.y, p.x);
if (a < 0.)
{
return a + TAU;
}
return a;
}
mat2 rotate_2d(float angle)
{
float s = sin(angle);
float c = cos(angle);
return mat2(
c, s,
-s, c
);
}
vec3 spherical_to_cartesian(vec2 s)
{
float sin_theta = sin(s.x);
return vec3(
sin_theta * cos(s.y),
sin_theta * sin(s.y),
cos(s.x)
);
}
vec2 screen_to_uv01(vec2 coord, vec2 res)
{
return coord / res;
}
vec2 screen_to_uv_horizontal(vec2 coord, vec2 res)
{
return (2. * coord - res) / res.x;
}
vec2 screen_to_uv_vertical(vec2 coord, vec2 res)
{
return (2. * coord - res) / res.y;
}
vec2 screen_to_uv_fit(vec2 coord, vec2 res)
{
return (2. * coord - res) / min_component(res);
}
vec2 screen_to_uv_fill(vec2 coord, vec2 res)
{
return (2. * coord - res) / max_component(res);
}
#define idiv_ceil(a, b) ((a + b - 1) / b)
// for some reason we can't use intBitsToFloat() or floatBitsToInt() to store
// integers below this value in a buffer.
const int BUFFER_MIN_INTEGER = 8388608;
// * x must not be higher than 4,286,578,688 (see BUFFER_MIN_INTEGER above)
// * for your sanity, don't use negative values
float encode_int_for_buffer(int v)
{
return intBitsToFloat(v + BUFFER_MIN_INTEGER);
}
int decode_int_from_buffer(float v)
{
return floatBitsToInt(v) - BUFFER_MIN_INTEGER;
}
// pack two 16-bit integers in a single 32-bit integer
// * both arguments should be in the 0-65535 range
// * a must be less than 65279 (see the functions above)
int pack_i16(int a, int b)
{
return (a << 16) | b;
}
// unpack two 16-bit integers from a single 32-bit integer
void unpack_i16(int v, out int a, out int b)
{
a = (v >> 16) & 65535;
b = v & 65535;
}
// unpack the first 16-bit integer from a 32-bit integer
int unpack_i16_a(int v)
{
return (v >> 16) & 65535;
}
// unpack the second 16-bit integer from a 32-bit integer
int unpack_i16_b(int v)
{
return v & 65535;
}
/*_______ pseudo-random number generator ______*/
// source: https://www.shadertoy.com/view/WdSSRt
// (heavily modified)
/* usage example:
void mainImage(out vec4 frag_col, in vec2 frag_coord)
{
// initialize PRNG
prng_init(vec3(frag_coord / iResolution.y, iTime));
// use the function
float a = random();
vec3 b = vec3(random(), random(), random());
...
}
*/
uint prng_state[2];
uint prng_rot(uint x, int k)
{
return (x << k) | (x >> (32 - k));
}
// random uint from 0 to 2^32-1
uint randomui()
{
uint s0 = prng_state[0];
uint s1 = prng_state[1];
uint result = prng_rot(s0 * 2654435771u, 5) * 5u;
s1 ^= s0;
prng_state[0] = prng_rot(s0, 26) ^ s1 ^ (s1 << 9);
prng_state[1] = prng_rot(s1, 13);
return result;
}
// random int from 0 to 2^31-1
int randomi()
{
return int(randomui() % 0x7FFFFFFFu);
}
// random float from 0 to 1
float random()
{
return float(randomui()) / float(0xffffffffu);
}
// generate two normally distributed random numbers using the
// Box-Muller transform
// https://www.baeldung.com/cs/uniform-to-normal-distribution
vec2 random_gauss()
{
float u1 = random();
float u2 = random() * TAU;
float temp = sqrt(-2. * log(u1));
return temp * vec2(cos(u2), sin(u2));
// unoptimized version
//float u1 = random();
//float u2 = random();
//return vec2(cos(TAU * u2), sin(TAU * u2)) * sqrt(-2. * log(u1));
}
vec2 random_on_circle()
{
vec2 v = vec2(1);
float lensqr;
for (int i = 0; i < 20; i++)
{
v = vec2(2. * random() - 1., 2. * random() - 1.);
lensqr = dot(v, v);
if (lensqr == 0.)
i--;
else if (lensqr <= 1.)
break;
}
return v / sqrt(lensqr);
}
vec3 random_on_sphere()
{
vec3 v = vec3(1);
float lensqr;
for (int i = 0; i < 20; i++)
{
v = vec3(2. * random() - 1., 2. * random() - 1., 2. * random() - 1.);
lensqr = dot(v, v);
if (lensqr == 0.)
i--;
else if (lensqr <= 1.)
break;
}
return v / sqrt(lensqr);
}
vec2 random_in_circle()
{
vec2 v = vec2(1);
for (int i = 0; i < 20; i++)
{
v = vec2(2. * random() - 1., 2. * random() - 1.);
if (dot(v, v) <= 1.)
return v;
}
return v;
}
vec3 random_in_sphere()
{
vec3 v = vec3(1);
for (int i = 0; i < 20; i++)
{
v = vec3(2. * random() - 1., 2. * random() - 1., 2. * random() - 1.);
if (dot(v, v) <= 1.)
return v;
}
return v;
}
vec3 random_on_hemisphere(vec3 normal)
{
vec3 v = random_on_sphere();
return v * sign(dot(v, normal));
}
vec3 random_in_hemisphere(vec3 normal)
{
vec3 v = random_in_sphere();
return v * sign(dot(v, normal));
}
// initialize with uvec2
void prng_init(uvec2 seed)
{
seed += uvec2(1317, 944573125);
seed *= 464973573u;
prng_state[0] = seed.x;
prng_state[1] = seed.y;
randomi();
}
// initialize with uint
void prng_init(uint seed)
{
prng_init(uvec2(seed, 1));
}
// initialize with vec3
void prng_init(vec3 seed)
{
seed += 3.49276101561702;
seed.xy *= (seed.z + 10.258);
prng_state[0] = floatBitsToUint(seed.x);
prng_state[1] = floatBitsToUint(seed.y);
randomui();
}
// initialize with vec2
void prng_init(vec2 seed)
{
prng_init(vec3(seed, 1));
}
// initialize with float
void prng_init(float seed)
{
prng_init(vec3(seed, 1, 1));
}
/*______________ halton sequence ______________*/
// * idx starts at 1
float halton(int base, int idx)
{
float result = 0.;
float digit_weight = 1.;
while (idx > 0)
{
digit_weight /= float(base);
result += float(idx % base) * digit_weight;
idx /= base;
}
return result;
}
// * idx starts at 1
vec2 halton_2d(int idx)
{
return vec2(halton(2, idx), halton(3, idx));
}
// * idx starts at 1
vec3 halton_3d(int idx)
{
return vec3(halton(2, idx), halton(3, idx), halton(5, idx));
}
// * idx starts at 1
vec4 halton_4d(int idx)
{
return vec4(
halton(2, idx),
halton(3, idx),
halton(5, idx),
halton(7, idx)
);
}
/*__________________ keyboard _________________*/
// put this code in every buffer that uses the keyboard input:
/*
#if SHADERTOY
// iChannelX must be set to Keyboard in Shadertoy
#define SHADERTOY_KEYBOARD_CH iChannelX
#else
#iKeyboard
#endif
define_keyboard_utils
*/
#if SHADERTOY
#define define_keyboard_utils \
\
/* https://github.com/stevensona/shader-toy/blob/c4833b972649d78a2ee090af60b79a3907e8e091/src/extensions/keyboard/keyboard_shader_extension.ts#L11 */ \
const int \
Key_Backspace = 8, Key_Tab = 9, Key_Enter = 13, Key_Shift = 16, \
Key_Ctrl = 17, Key_Alt = 18, Key_Pause = 19, Key_Caps = 20, \
Key_Escape = 27, Key_PageUp = 33, Key_PageDown = 34, Key_End = 35, \
Key_Home = 36, Key_LeftArrow = 37, Key_UpArrow = 38, Key_RightArrow = 39, \
Key_DownArrow = 40, Key_Insert = 45, Key_Delete = 46, Key_0 = 48, \
Key_1 = 49, Key_2 = 50, Key_3 = 51, Key_4 = 52, Key_5 = 53, Key_6 = 54, \
Key_7 = 55, Key_8 = 56, Key_9 = 57, Key_A = 65, Key_B = 66, Key_C = 67, \
Key_D = 68, Key_E = 69, Key_F = 70, Key_G = 71, Key_H = 72, \
Key_I = 73, Key_J = 74, Key_K = 75, Key_L = 76, Key_M = 77, Key_N = 78, \
Key_O = 79, Key_P = 80, Key_Q = 81, Key_R = 82, Key_S = 83, Key_T = 84, \
Key_U = 85, Key_V = 86, Key_W = 87, Key_X = 88, Key_Y = 89, Key_Z = 90, \
Key_LeftWindow = 91, Key_RightWindows = 92, Key_Select = 93, \
Key_Numpad0 = 96, Key_Numpad1 = 97, Key_Numpad2 = 98, Key_Numpad3 = 99, \
Key_Numpad4 = 100, Key_Numpad5 = 101, Key_Numpad6 = 102, \
Key_Numpad7 = 103, Key_Numpad8 = 104, Key_Numpad9 = 105, \
Key_NumpadMultiply = 106, Key_NumpadAdd = 107, Key_NumpadSubtract = 109, \
Key_NumpadPeriod = 110, Key_NumpadDivide = 111, Key_F1 = 112, \
Key_F2 = 113, Key_F3 = 114, Key_F4 = 115, Key_F5 = 116, Key_F6 = 117, \
Key_F7 = 118, Key_F8 = 119, Key_F9 = 120, Key_F10 = 121, Key_F11 = 122, \
Key_F12 = 123, Key_NumLock = 144, Key_ScrollLock = 145, \
Key_SemiColon = 186, Key_Equal = 187, Key_Comma = 188, Key_Dash = 189, \
Key_Period = 190, Key_ForwardSlash = 191, Key_GraveAccent = 192, \
Key_OpenBracket = 219, Key_BackSlash = 220, Key_CloseBraket = 221, \
Key_SingleQuote = 222; \
const int Key_Space = 32; \
\
bool is_key_pressed(int key) \
{ \
return texelFetch(SHADERTOY_KEYBOARD_CH, ivec2(key, 1), 0).x > .5; \
} \
\
bool is_key_down(int key) \
{ \
return texelFetch(SHADERTOY_KEYBOARD_CH, ivec2(key, 0), 0).x > .5; \
} \
\
bool is_key_toggled(int key) \
{ \
return texelFetch(SHADERTOY_KEYBOARD_CH, ivec2(key, 2), 0).x > .5; \
}
#else
#define define_keyboard_utils \
\
const int Key_Space = 32; \
\
bool is_key_pressed(int key) \
{ \
return isKeyPressed(key); \
} \
\
bool is_key_down(int key) \
{ \
return isKeyDown(key); \
} \
\
bool is_key_toggled(int key) \
{ \
return isKeyToggled(key); \
}
#endif
/*______________ ray tracing (1) ______________*/
struct Ray
{
vec3 orig;
vec3 dir;
};
struct OrthonormalBasis
{
vec3 right;
vec3 forward;
vec3 up;
};
// * all three arguments must be normalized and perpendicular to each other
OrthonormalBasis OrthonormalBasis_new(vec3 right, vec3 forward, vec3 up)
{
OrthonormalBasis onb;
onb.right = right;
onb.forward = forward;
onb.up = up;
return onb;
}
// * forward must be normalized
OrthonormalBasis OrthonormalBasis_from_forward_and_world_up(
vec3 forward,
vec3 world_up
)
{
OrthonormalBasis onb;
onb.forward = forward;
onb.right = normalize(cross(onb.forward, world_up));
onb.up = cross(onb.right, onb.forward);
return onb;
}
vec3 OrthonormalBasis_localize(in OrthonormalBasis self, vec3 p)
{
return vec3(
dot(p, self.right),
dot(p, self.forward),
dot(p, self.up)
);
}
vec3 OrthonormalBasis_delocalize(in OrthonormalBasis self, vec3 p)
{
return (p.x * self.right) + (p.y * self.forward) + (p.z * self.up);
}
// basic perspective camera
struct Camera
{
vec3 pos;
float sensor_width;
float sensor_height;
float focal_length;
OrthonormalBasis _onb;
};
// Camera: adjust focal length based on horizontal FOV and sensor width
void Camera_set_fov_horizontal(inout Camera self, float fov)
{
self.focal_length = .5 * self.sensor_width / tan(.5 * fov);
}
// Camera: calculate the horizontal FOV
float Camera_get_fov_horizontal(in Camera self)
{
return 2. * atan(.5 * self.sensor_width / self.focal_length);
}
// Camera: adjust focal length based on vertical FOV and sensor height
void Camera_set_fov_vertical(inout Camera self, float fov)
{
self.focal_length = .5 * self.sensor_height / tan(.5 * fov);
}
// Camera: calculate the vertical FOV
float Camera_get_fov_vertical(in Camera self)
{
return 2. * atan(.5 * self.sensor_height / self.focal_length);
}
// Camera: adjust focal length based on diagonal FOV and sensor size
void Camera_set_fov_diagonal(inout Camera self, float fov)
{
float sensor_size = length(vec2(self.sensor_width, self.sensor_height));
self.focal_length = .5 * sensor_size / tan(.5 * fov);
}
// Camera: calculate the diagonal FOV
float Camera_get_fov_diagonal(in Camera self)
{
float sensor_size = length(vec2(self.sensor_width, self.sensor_height));
return 2. * atan(.5 * sensor_size / self.focal_length);
}
// Camera: look at a point
void Camera_look_at(inout Camera self, vec3 target, vec3 world_up)
{
self._onb = OrthonormalBasis_from_forward_and_world_up(
normalize(target - self.pos),
world_up
);
}
// Camera: look along a direction
// * dir must be normalized
void Camera_look_along(inout Camera self, vec3 dir, vec3 world_up)
{
self._onb = OrthonormalBasis_from_forward_and_world_up(
dir,
world_up
);
}
// Camera: generate a camera ray for given UV coordinates
Ray Camera_gen_ray(in Camera self, vec2 uv01)
{
Ray r;
r.orig = self.pos + OrthonormalBasis_delocalize(self._onb, vec3(
(uv01.x - .5) * self.sensor_width,
self.focal_length,
(uv01.y - .5) * self.sensor_height
));
r.dir = normalize(r.orig - self.pos);
return r;
}
// Camera: retrieve UV coordinates from a point in space
// * returns vec2(-1e9) on failure
vec2 Camera_retrieve_uv01_from_point(in Camera self, vec3 p)
{
// project into camera space
vec3 world_space = p - self.pos;
vec3 cam_space = OrthonormalBasis_localize(self._onb, world_space);
// p is not in front of the sensor
if (cam_space.y < self.focal_length - .00001)
{
return vec2(-1e9);
}
// linearly normalize p so that it falls on the focal plane
cam_space /= cam_space.y;
cam_space *= self.focal_length;
// extract the UV coordinates
return (cam_space.xz / vec2(self.sensor_width, self.sensor_height)) + .5;
}
// Camera: retrieve UV coordinates from a direction towards the sky (used when
// no object is hit)
// * returns vec2(-1e9) on failure
vec2 Camera_retrieve_uv01_from_dir(in Camera self, vec3 dir)
{
// project into camera space
vec3 p = dir;
vec3 cam_space = OrthonormalBasis_localize(self._onb, p);
// p is not in front of the sensor
if (cam_space.y < .00001)
{
return vec2(-1e9);
}
// linearly normalize p so that it falls on the focal plane
cam_space /= cam_space.y;
cam_space *= self.focal_length;
// extract the UV coordinates
return (cam_space.xz / vec2(self.sensor_width, self.sensor_height)) + .5;
}
// Camera: generate a defocused camera ray for given UV coordinates
Ray Camera_gen_ray_defocused(
in Camera self,
vec2 uv01,
float focus_dist,
float jitter
)
{
// generate normal ray without normalizing the direction
Ray r;
vec3 r_dir_unnormalized = OrthonormalBasis_delocalize(self._onb, vec3(
(uv01.x - .5) * self.sensor_width,
self.focal_length,
(uv01.y - .5) * self.sensor_height
));
r.orig = self.pos + r_dir_unnormalized;
// point on the focus plane
vec3 fp = r.orig + focus_dist * (r_dir_unnormalized / self.focal_length);
// randomly offset the ray origin
vec2 offs = jitter * random_in_circle();
r.orig += OrthonormalBasis_delocalize(self._onb, vec3(
offs.x,
0.,
offs.y
));
// look at the point on the focus plane
r.dir = normalize(fp - r.orig);
return r;
}
struct Hit
{
bool hit;
float t;
vec3 pos;
vec3 normal;
int obj_id;
int mat_id;
vec2 uv; // only works with triangles and quads
};
Hit Hit_new()
{
Hit h;
h.hit = false;
h.t = 1e9;
h.obj_id = OBJ_NONE;
h.mat_id = MAT_NONE;
h.uv = vec2(-1e9);
return h;
}
struct Material
{
vec3 diffuse;
float roughness;
vec3 emission;
bool no_bounce;
};
float Material_bsdf(