SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
stb_perlin.h
Go to the documentation of this file.
1// stb_perlin.h - v0.5 - perlin noise
2// public domain single-file C implementation by Sean Barrett
3//
4// LICENSE
5//
6// See end of file.
7//
8//
9// to create the implementation,
10// #define STB_PERLIN_IMPLEMENTATION
11// in *one* C/CPP file that includes this file.
12//
13//
14// Documentation:
15//
16// float stb_perlin_noise3( float x,
17// float y,
18// float z,
19// int x_wrap=0,
20// int y_wrap=0,
21// int z_wrap=0)
22//
23// This function computes a random value at the coordinate (x,y,z).
24// Adjacent random values are continuous but the noise fluctuates
25// its randomness with period 1, i.e. takes on wholly unrelated values
26// at integer points. Specifically, this implements Ken Perlin's
27// revised noise function from 2002.
28//
29// The "wrap" parameters can be used to create wraparound noise that
30// wraps at powers of two. The numbers MUST be powers of two. Specify
31// 0 to mean "don't care". (The noise always wraps every 256 due
32// details of the implementation, even if you ask for larger or no
33// wrapping.)
34//
35// float stb_perlin_noise3_seed( float x,
36// float y,
37// float z,
38// int x_wrap=0,
39// int y_wrap=0,
40// int z_wrap=0,
41// int seed)
42//
43// As above, but 'seed' selects from multiple different variations of the
44// noise function. The current implementation only uses the bottom 8 bits
45// of 'seed', but possibly in the future more bits will be used.
46//
47//
48// Fractal Noise:
49//
50// Three common fractal noise functions are included, which produce
51// a wide variety of nice effects depending on the parameters
52// provided. Note that each function will call stb_perlin_noise3
53// 'octaves' times, so this parameter will affect runtime.
54//
55// float stb_perlin_ridge_noise3(float x, float y, float z,
56// float lacunarity, float gain, float offset, int octaves)
57//
58// float stb_perlin_fbm_noise3(float x, float y, float z,
59// float lacunarity, float gain, int octaves)
60//
61// float stb_perlin_turbulence_noise3(float x, float y, float z,
62// float lacunarity, float gain, int octaves)
63//
64// Typical values to start playing with:
65// octaves = 6 -- number of "octaves" of noise3() to sum
66// lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping
67// output) gain = 0.5 -- relative weighting applied to each successive octave offset
68// = 1.0? -- used to invert the ridges, may need to be larger, not sure
69//
70//
71// Contributors:
72// Jack Mott - additional noise functions
73// Jordan Peck - seeded noise
74//
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);
80extern float
81stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed);
83 float x,
84 float y,
85 float z,
86 float lacunarity,
87 float gain,
88 float offset,
89 int octaves
90);
91extern float
92stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
93extern float
94stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
96 float x,
97 float y,
98 float z,
99 int x_wrap,
100 int y_wrap,
101 int z_wrap,
102 unsigned char seed
103);
104#ifdef __cplusplus
105}
106#endif
107
108#ifdef STB_PERLIN_IMPLEMENTATION
109
110#include <math.h> // fabs()
111
112// not same permutation table as Perlin's reference to avoid copyright issues;
113// Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/
114static unsigned char stb__perlin_randtab[512] = {
115 23,
116 125,
117 161,
118 52,
119 103,
120 117,
121 70,
122 37,
123 247,
124 101,
125 203,
126 169,
127 124,
128 126,
129 44,
130 123,
131 152,
132 238,
133 145,
134 45,
135 171,
136 114,
137 253,
138 10,
139 192,
140 136,
141 4,
142 157,
143 249,
144 30,
145 35,
146 72,
147 175,
148 63,
149 77,
150 90,
151 181,
152 16,
153 96,
154 111,
155 133,
156 104,
157 75,
158 162,
159 93,
160 56,
161 66,
162 240,
163 8,
164 50,
165 84,
166 229,
167 49,
168 210,
169 173,
170 239,
171 141,
172 1,
173 87,
174 18,
175 2,
176 198,
177 143,
178 57,
179 225,
180 160,
181 58,
182 217,
183 168,
184 206,
185 245,
186 204,
187 199,
188 6,
189 73,
190 60,
191 20,
192 230,
193 211,
194 233,
195 94,
196 200,
197 88,
198 9,
199 74,
200 155,
201 33,
202 15,
203 219,
204 130,
205 226,
206 202,
207 83,
208 236,
209 42,
210 172,
211 165,
212 218,
213 55,
214 222,
215 46,
216 107,
217 98,
218 154,
219 109,
220 67,
221 196,
222 178,
223 127,
224 158,
225 13,
226 243,
227 65,
228 79,
229 166,
230 248,
231 25,
232 224,
233 115,
234 80,
235 68,
236 51,
237 184,
238 128,
239 232,
240 208,
241 151,
242 122,
243 26,
244 212,
245 105,
246 43,
247 179,
248 213,
249 235,
250 148,
251 146,
252 89,
253 14,
254 195,
255 28,
256 78,
257 112,
258 76,
259 250,
260 47,
261 24,
262 251,
263 140,
264 108,
265 186,
266 190,
267 228,
268 170,
269 183,
270 139,
271 39,
272 188,
273 244,
274 246,
275 132,
276 48,
277 119,
278 144,
279 180,
280 138,
281 134,
282 193,
283 82,
284 182,
285 120,
286 121,
287 86,
288 220,
289 209,
290 3,
291 91,
292 241,
293 149,
294 85,
295 205,
296 150,
297 113,
298 216,
299 31,
300 100,
301 41,
302 164,
303 177,
304 214,
305 153,
306 231,
307 38,
308 71,
309 185,
310 174,
311 97,
312 201,
313 29,
314 95,
315 7,
316 92,
317 54,
318 254,
319 191,
320 118,
321 34,
322 221,
323 131,
324 11,
325 163,
326 99,
327 234,
328 81,
329 227,
330 147,
331 156,
332 176,
333 17,
334 142,
335 69,
336 12,
337 110,
338 62,
339 27,
340 255,
341 0,
342 194,
343 59,
344 116,
345 242,
346 252,
347 19,
348 21,
349 187,
350 53,
351 207,
352 129,
353 64,
354 135,
355 61,
356 40,
357 167,
358 237,
359 102,
360 223,
361 106,
362 159,
363 197,
364 189,
365 215,
366 137,
367 36,
368 32,
369 22,
370 5,
371
372 // and a second copy so we don't need an extra mask or static initializer
373 23,
374 125,
375 161,
376 52,
377 103,
378 117,
379 70,
380 37,
381 247,
382 101,
383 203,
384 169,
385 124,
386 126,
387 44,
388 123,
389 152,
390 238,
391 145,
392 45,
393 171,
394 114,
395 253,
396 10,
397 192,
398 136,
399 4,
400 157,
401 249,
402 30,
403 35,
404 72,
405 175,
406 63,
407 77,
408 90,
409 181,
410 16,
411 96,
412 111,
413 133,
414 104,
415 75,
416 162,
417 93,
418 56,
419 66,
420 240,
421 8,
422 50,
423 84,
424 229,
425 49,
426 210,
427 173,
428 239,
429 141,
430 1,
431 87,
432 18,
433 2,
434 198,
435 143,
436 57,
437 225,
438 160,
439 58,
440 217,
441 168,
442 206,
443 245,
444 204,
445 199,
446 6,
447 73,
448 60,
449 20,
450 230,
451 211,
452 233,
453 94,
454 200,
455 88,
456 9,
457 74,
458 155,
459 33,
460 15,
461 219,
462 130,
463 226,
464 202,
465 83,
466 236,
467 42,
468 172,
469 165,
470 218,
471 55,
472 222,
473 46,
474 107,
475 98,
476 154,
477 109,
478 67,
479 196,
480 178,
481 127,
482 158,
483 13,
484 243,
485 65,
486 79,
487 166,
488 248,
489 25,
490 224,
491 115,
492 80,
493 68,
494 51,
495 184,
496 128,
497 232,
498 208,
499 151,
500 122,
501 26,
502 212,
503 105,
504 43,
505 179,
506 213,
507 235,
508 148,
509 146,
510 89,
511 14,
512 195,
513 28,
514 78,
515 112,
516 76,
517 250,
518 47,
519 24,
520 251,
521 140,
522 108,
523 186,
524 190,
525 228,
526 170,
527 183,
528 139,
529 39,
530 188,
531 244,
532 246,
533 132,
534 48,
535 119,
536 144,
537 180,
538 138,
539 134,
540 193,
541 82,
542 182,
543 120,
544 121,
545 86,
546 220,
547 209,
548 3,
549 91,
550 241,
551 149,
552 85,
553 205,
554 150,
555 113,
556 216,
557 31,
558 100,
559 41,
560 164,
561 177,
562 214,
563 153,
564 231,
565 38,
566 71,
567 185,
568 174,
569 97,
570 201,
571 29,
572 95,
573 7,
574 92,
575 54,
576 254,
577 191,
578 118,
579 34,
580 221,
581 131,
582 11,
583 163,
584 99,
585 234,
586 81,
587 227,
588 147,
589 156,
590 176,
591 17,
592 142,
593 69,
594 12,
595 110,
596 62,
597 27,
598 255,
599 0,
600 194,
601 59,
602 116,
603 242,
604 252,
605 19,
606 21,
607 187,
608 53,
609 207,
610 129,
611 64,
612 135,
613 61,
614 40,
615 167,
616 237,
617 102,
618 223,
619 106,
620 159,
621 197,
622 189,
623 215,
624 137,
625 36,
626 32,
627 22,
628 5,
629};
630
631// perlin's gradient has 12 cases so some get used 1/16th of the time
632// and some 2/16ths. We reduce bias by changing those fractions
633// to 5/64ths and 6/64ths
634
635// this array is designed to match the previous implementation
636// of gradient hash: indices[stb__perlin_randtab[i]&63]
637static unsigned char stb__perlin_randtab_grad_idx[512] = {
638 7,
639 9,
640 5,
641 0,
642 11,
643 1,
644 6,
645 9,
646 3,
647 9,
648 11,
649 1,
650 8,
651 10,
652 4,
653 7,
654 8,
655 6,
656 1,
657 5,
658 3,
659 10,
660 9,
661 10,
662 0,
663 8,
664 4,
665 1,
666 5,
667 2,
668 7,
669 8,
670 7,
671 11,
672 9,
673 10,
674 1,
675 0,
676 4,
677 7,
678 5,
679 0,
680 11,
681 6,
682 1,
683 4,
684 2,
685 8,
686 8,
687 10,
688 4,
689 9,
690 9,
691 2,
692 5,
693 7,
694 9,
695 1,
696 7,
697 2,
698 2,
699 6,
700 11,
701 5,
702 5,
703 4,
704 6,
705 9,
706 0,
707 1,
708 1,
709 0,
710 7,
711 6,
712 9,
713 8,
714 4,
715 10,
716 3,
717 1,
718 2,
719 8,
720 8,
721 9,
722 10,
723 11,
724 5,
725 11,
726 11,
727 2,
728 6,
729 10,
730 3,
731 4,
732 2,
733 4,
734 9,
735 10,
736 3,
737 2,
738 6,
739 3,
740 6,
741 10,
742 5,
743 3,
744 4,
745 10,
746 11,
747 2,
748 9,
749 11,
750 1,
751 11,
752 10,
753 4,
754 9,
755 4,
756 11,
757 0,
758 4,
759 11,
760 4,
761 0,
762 0,
763 0,
764 7,
765 6,
766 10,
767 4,
768 1,
769 3,
770 11,
771 5,
772 3,
773 4,
774 2,
775 9,
776 1,
777 3,
778 0,
779 1,
780 8,
781 0,
782 6,
783 7,
784 8,
785 7,
786 0,
787 4,
788 6,
789 10,
790 8,
791 2,
792 3,
793 11,
794 11,
795 8,
796 0,
797 2,
798 4,
799 8,
800 3,
801 0,
802 0,
803 10,
804 6,
805 1,
806 2,
807 2,
808 4,
809 5,
810 6,
811 0,
812 1,
813 3,
814 11,
815 9,
816 5,
817 5,
818 9,
819 6,
820 9,
821 8,
822 3,
823 8,
824 1,
825 8,
826 9,
827 6,
828 9,
829 11,
830 10,
831 7,
832 5,
833 6,
834 5,
835 9,
836 1,
837 3,
838 7,
839 0,
840 2,
841 10,
842 11,
843 2,
844 6,
845 1,
846 3,
847 11,
848 7,
849 7,
850 2,
851 1,
852 7,
853 3,
854 0,
855 8,
856 1,
857 1,
858 5,
859 0,
860 6,
861 10,
862 11,
863 11,
864 0,
865 2,
866 7,
867 0,
868 10,
869 8,
870 3,
871 5,
872 7,
873 1,
874 11,
875 1,
876 0,
877 7,
878 9,
879 0,
880 11,
881 5,
882 10,
883 3,
884 2,
885 3,
886 5,
887 9,
888 7,
889 9,
890 8,
891 4,
892 6,
893 5,
894
895 // and a second copy so we don't need an extra mask or static initializer
896 7,
897 9,
898 5,
899 0,
900 11,
901 1,
902 6,
903 9,
904 3,
905 9,
906 11,
907 1,
908 8,
909 10,
910 4,
911 7,
912 8,
913 6,
914 1,
915 5,
916 3,
917 10,
918 9,
919 10,
920 0,
921 8,
922 4,
923 1,
924 5,
925 2,
926 7,
927 8,
928 7,
929 11,
930 9,
931 10,
932 1,
933 0,
934 4,
935 7,
936 5,
937 0,
938 11,
939 6,
940 1,
941 4,
942 2,
943 8,
944 8,
945 10,
946 4,
947 9,
948 9,
949 2,
950 5,
951 7,
952 9,
953 1,
954 7,
955 2,
956 2,
957 6,
958 11,
959 5,
960 5,
961 4,
962 6,
963 9,
964 0,
965 1,
966 1,
967 0,
968 7,
969 6,
970 9,
971 8,
972 4,
973 10,
974 3,
975 1,
976 2,
977 8,
978 8,
979 9,
980 10,
981 11,
982 5,
983 11,
984 11,
985 2,
986 6,
987 10,
988 3,
989 4,
990 2,
991 4,
992 9,
993 10,
994 3,
995 2,
996 6,
997 3,
998 6,
999 10,
1000 5,
1001 3,
1002 4,
1003 10,
1004 11,
1005 2,
1006 9,
1007 11,
1008 1,
1009 11,
1010 10,
1011 4,
1012 9,
1013 4,
1014 11,
1015 0,
1016 4,
1017 11,
1018 4,
1019 0,
1020 0,
1021 0,
1022 7,
1023 6,
1024 10,
1025 4,
1026 1,
1027 3,
1028 11,
1029 5,
1030 3,
1031 4,
1032 2,
1033 9,
1034 1,
1035 3,
1036 0,
1037 1,
1038 8,
1039 0,
1040 6,
1041 7,
1042 8,
1043 7,
1044 0,
1045 4,
1046 6,
1047 10,
1048 8,
1049 2,
1050 3,
1051 11,
1052 11,
1053 8,
1054 0,
1055 2,
1056 4,
1057 8,
1058 3,
1059 0,
1060 0,
1061 10,
1062 6,
1063 1,
1064 2,
1065 2,
1066 4,
1067 5,
1068 6,
1069 0,
1070 1,
1071 3,
1072 11,
1073 9,
1074 5,
1075 5,
1076 9,
1077 6,
1078 9,
1079 8,
1080 3,
1081 8,
1082 1,
1083 8,
1084 9,
1085 6,
1086 9,
1087 11,
1088 10,
1089 7,
1090 5,
1091 6,
1092 5,
1093 9,
1094 1,
1095 3,
1096 7,
1097 0,
1098 2,
1099 10,
1100 11,
1101 2,
1102 6,
1103 1,
1104 3,
1105 11,
1106 7,
1107 7,
1108 2,
1109 1,
1110 7,
1111 3,
1112 0,
1113 8,
1114 1,
1115 1,
1116 5,
1117 0,
1118 6,
1119 10,
1120 11,
1121 11,
1122 0,
1123 2,
1124 7,
1125 0,
1126 10,
1127 8,
1128 3,
1129 5,
1130 7,
1131 1,
1132 11,
1133 1,
1134 0,
1135 7,
1136 9,
1137 0,
1138 11,
1139 5,
1140 10,
1141 3,
1142 2,
1143 3,
1144 5,
1145 9,
1146 7,
1147 9,
1148 8,
1149 4,
1150 6,
1151 5,
1152};
1153
1154static float stb__perlin_lerp(float a, float b, float t) { return a + (b - a) * t; }
1155
1156static int stb__perlin_fastfloor(float a) {
1157 int ai = (int)a;
1158 return (a < ai) ? ai - 1 : ai;
1159}
1160
1161// different grad function from Perlin's, but easy to modify to match reference
1162static float stb__perlin_grad(int grad_idx, float x, float y, float z) {
1163 static float basis[12][4] = {
1164 { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 },
1165 { 1, 0, -1 }, { -1, 0, -1 }, { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 },
1166 };
1167
1168 float* grad = basis[grad_idx];
1169 return grad[0] * x + grad[1] * y + grad[2] * z;
1170}
1171
1172float stb_perlin_noise3_internal(
1173 float x,
1174 float y,
1175 float z,
1176 int x_wrap,
1177 int y_wrap,
1178 int z_wrap,
1179 unsigned char seed
1180) {
1181 float u, v, w;
1182 float n000, n001, n010, n011, n100, n101, n110, n111;
1183 float n00, n01, n10, n11;
1184 float n0, n1;
1185
1186 unsigned int x_mask = (x_wrap - 1) & 255;
1187 unsigned int y_mask = (y_wrap - 1) & 255;
1188 unsigned int z_mask = (z_wrap - 1) & 255;
1189 int px = stb__perlin_fastfloor(x);
1190 int py = stb__perlin_fastfloor(y);
1191 int pz = stb__perlin_fastfloor(z);
1192 int x0 = px & x_mask, x1 = (px + 1) & x_mask;
1193 int y0 = py & y_mask, y1 = (py + 1) & y_mask;
1194 int z0 = pz & z_mask, z1 = (pz + 1) & z_mask;
1195 int r0, r1, r00, r01, r10, r11;
1196
1197#define stb__perlin_ease(a) (((a * 6 - 15) * a + 10) * a * a * a)
1198
1199 x -= px;
1200 u = stb__perlin_ease(x);
1201 y -= py;
1202 v = stb__perlin_ease(y);
1203 z -= pz;
1204 w = stb__perlin_ease(z);
1205
1206 r0 = stb__perlin_randtab[x0 + seed];
1207 r1 = stb__perlin_randtab[x1 + seed];
1208
1209 r00 = stb__perlin_randtab[r0 + y0];
1210 r01 = stb__perlin_randtab[r0 + y1];
1211 r10 = stb__perlin_randtab[r1 + y0];
1212 r11 = stb__perlin_randtab[r1 + y1];
1213
1214 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z0], x, y, z);
1215 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z1], x, y, z - 1);
1216 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z0], x, y - 1, z);
1217 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z1], x, y - 1, z - 1);
1218 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z0], x - 1, y, z);
1219 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z1], x - 1, y, z - 1);
1220 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z0], x - 1, y - 1, z);
1221 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z1], x - 1, y - 1, z - 1);
1222
1223 n00 = stb__perlin_lerp(n000, n001, w);
1224 n01 = stb__perlin_lerp(n010, n011, w);
1225 n10 = stb__perlin_lerp(n100, n101, w);
1226 n11 = stb__perlin_lerp(n110, n111, w);
1227
1228 n0 = stb__perlin_lerp(n00, n01, v);
1229 n1 = stb__perlin_lerp(n10, n11, v);
1230
1231 return stb__perlin_lerp(n0, n1, u);
1232}
1233
1234float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap) {
1235 return stb_perlin_noise3_internal(x, y, z, x_wrap, y_wrap, z_wrap, 0);
1236}
1237
1239 float x,
1240 float y,
1241 float z,
1242 int x_wrap,
1243 int y_wrap,
1244 int z_wrap,
1245 int seed
1246) {
1247 return stb_perlin_noise3_internal(x, y, z, x_wrap, y_wrap, z_wrap, (unsigned char)seed);
1248}
1249
1251 float x,
1252 float y,
1253 float z,
1254 float lacunarity,
1255 float gain,
1256 float offset,
1257 int octaves
1258) {
1259 int i;
1260 float frequency = 1.0f;
1261 float prev = 1.0f;
1262 float amplitude = 0.5f;
1263 float sum = 0.0f;
1264
1265 for (i = 0; i < octaves; i++) {
1266 float r = stb_perlin_noise3_internal(
1267 x * frequency,
1268 y * frequency,
1269 z * frequency,
1270 0,
1271 0,
1272 0,
1273 (unsigned char)i
1274 );
1275 r = offset - (float)fabs(r);
1276 r = r * r;
1277 sum += r * amplitude * prev;
1278 prev = r;
1279 frequency *= lacunarity;
1280 amplitude *= gain;
1281 }
1282 return sum;
1283}
1284
1285float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves) {
1286 int i;
1287 float frequency = 1.0f;
1288 float amplitude = 1.0f;
1289 float sum = 0.0f;
1290
1291 for (i = 0; i < octaves; i++) {
1292 sum += stb_perlin_noise3_internal(
1293 x * frequency,
1294 y * frequency,
1295 z * frequency,
1296 0,
1297 0,
1298 0,
1299 (unsigned char)i
1300 ) *
1301 amplitude;
1302 frequency *= lacunarity;
1303 amplitude *= gain;
1304 }
1305 return sum;
1306}
1307
1309 float x,
1310 float y,
1311 float z,
1312 float lacunarity,
1313 float gain,
1314 int octaves
1315) {
1316 int i;
1317 float frequency = 1.0f;
1318 float amplitude = 1.0f;
1319 float sum = 0.0f;
1320
1321 for (i = 0; i < octaves; i++) {
1322 float r = stb_perlin_noise3_internal(
1323 x * frequency,
1324 y * frequency,
1325 z * frequency,
1326 0,
1327 0,
1328 0,
1329 (unsigned char)i
1330 ) *
1331 amplitude;
1332 sum += (float)fabs(r);
1333 frequency *= lacunarity;
1334 amplitude *= gain;
1335 }
1336 return sum;
1337}
1338
1340 float x,
1341 float y,
1342 float z,
1343 int x_wrap,
1344 int y_wrap,
1345 int z_wrap,
1346 unsigned char seed
1347) {
1348 float u, v, w;
1349 float n000, n001, n010, n011, n100, n101, n110, n111;
1350 float n00, n01, n10, n11;
1351 float n0, n1;
1352
1353 int px = stb__perlin_fastfloor(x);
1354 int py = stb__perlin_fastfloor(y);
1355 int pz = stb__perlin_fastfloor(z);
1356 int x_wrap2 = (x_wrap ? x_wrap : 256);
1357 int y_wrap2 = (y_wrap ? y_wrap : 256);
1358 int z_wrap2 = (z_wrap ? z_wrap : 256);
1359 int x0 = px % x_wrap2, x1;
1360 int y0 = py % y_wrap2, y1;
1361 int z0 = pz % z_wrap2, z1;
1362 int r0, r1, r00, r01, r10, r11;
1363
1364 if (x0 < 0)
1365 x0 += x_wrap2;
1366 if (y0 < 0)
1367 y0 += y_wrap2;
1368 if (z0 < 0)
1369 z0 += z_wrap2;
1370 x1 = (x0 + 1) % x_wrap2;
1371 y1 = (y0 + 1) % y_wrap2;
1372 z1 = (z0 + 1) % z_wrap2;
1373
1374#define stb__perlin_ease(a) (((a * 6 - 15) * a + 10) * a * a * a)
1375
1376 x -= px;
1377 u = stb__perlin_ease(x);
1378 y -= py;
1379 v = stb__perlin_ease(y);
1380 z -= pz;
1381 w = stb__perlin_ease(z);
1382
1383 r0 = stb__perlin_randtab[x0];
1384 r0 = stb__perlin_randtab[r0 + seed];
1385 r1 = stb__perlin_randtab[x1];
1386 r1 = stb__perlin_randtab[r1 + seed];
1387
1388 r00 = stb__perlin_randtab[r0 + y0];
1389 r01 = stb__perlin_randtab[r0 + y1];
1390 r10 = stb__perlin_randtab[r1 + y0];
1391 r11 = stb__perlin_randtab[r1 + y1];
1392
1393 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z0], x, y, z);
1394 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z1], x, y, z - 1);
1395 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z0], x, y - 1, z);
1396 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z1], x, y - 1, z - 1);
1397 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z0], x - 1, y, z);
1398 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z1], x - 1, y, z - 1);
1399 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z0], x - 1, y - 1, z);
1400 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z1], x - 1, y - 1, z - 1);
1401
1402 n00 = stb__perlin_lerp(n000, n001, w);
1403 n01 = stb__perlin_lerp(n010, n011, w);
1404 n10 = stb__perlin_lerp(n100, n101, w);
1405 n11 = stb__perlin_lerp(n110, n111, w);
1406
1407 n0 = stb__perlin_lerp(n00, n01, v);
1408 n1 = stb__perlin_lerp(n10, n11, v);
1409
1410 return stb__perlin_lerp(n0, n1, u);
1411}
1412#endif // STB_PERLIN_IMPLEMENTATION
1413
1414/*
1415------------------------------------------------------------------------------
1416This software is available under 2 licenses -- choose whichever you prefer.
1417------------------------------------------------------------------------------
1418ALTERNATIVE A - MIT License
1419Copyright (c) 2017 Sean Barrett
1420Permission is hereby granted, free of charge, to any person obtaining a copy of
1421this software and associated documentation files (the "Software"), to deal in
1422the Software without restriction, including without limitation the rights to
1423use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1424of the Software, and to permit persons to whom the Software is furnished to do
1425so, subject to the following conditions:
1426The above copyright notice and this permission notice shall be included in all
1427copies or substantial portions of the Software.
1428THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1429IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1430FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1431AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1432LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1433OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1434SOFTWARE.
1435------------------------------------------------------------------------------
1436ALTERNATIVE B - Public Domain (www.unlicense.org)
1437This is free and unencumbered software released into the public domain.
1438Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1439software, either in source code form or as a compiled binary, for any purpose,
1440commercial or non-commercial, and by any means.
1441In jurisdictions that recognize copyright laws, the author or authors of this
1442software dedicate any and all copyright interest in the software to the public
1443domain. We make this dedication for the benefit of the public at large and to
1444the detriment of our heirs and successors. We intend this dedication to be an
1445overt act of relinquishment in perpetuity of all present and future rights to
1446this software under copyright law.
1447THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1448IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1449FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1450AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1451ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1452WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1453------------------------------------------------------------------------------
1454*/
float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed)
float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)
float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)
float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)