Skip to content
Myr

stb_perlin.h

This file

Path

stb_perlin.h

427 lines · 17.5 KiB · C

Last changed by

stb_perlin: add non-power-of-two wrapping noiseSean Barrettbefore adoption8ca86ee

History of this file

Take it

The walk stopped after 200 commits: lines older than 0c1232f point at it.

update version numbersSean Barrettbefore adoption63b59b4

line 1why, one step back
1// stb_perlin.h - v0.4 - perlin noise

version numberSean Barrettbefore adoption0c1232f

lines 2–11why, one step back
2// public domain single-file C implementation by Sean Barrett
3//4// LICENSE5//6// See end of file.7//8//9// to create the implementation,10// #define STB_PERLIN_IMPLEMENTATION11// in *one* C/CPP file that includes this file.

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 12–13why, one step back
12//
13//

version numberSean Barrettbefore adoption0c1232f

lines 14–33why, one step back
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 fluctuates25// its randomness with period 1, i.e. takes on wholly unrelated values26// at integer points. Specifically, this implements Ken Perlin's27// revised noise function from 2002.28//29// The "wrap" parameters can be used to create wraparound noise that30// wraps at powers of two. The numbers MUST be powers of two. Specify31// 0 to mean "don't care". (The noise always wraps every 256 due32// details of the implementation, even if you ask for larger or no33// wrapping.)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

line 34why, one step back
34//

added integer seed to noise function, use it in multi-octave noisesSean Barrettbefore adoptioncc53512

lines 35–47why, one step back
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 the44// noise function. The current implementation only uses the bottom 8 bits45// of 'seed', but possibly in the future more bits will be used.46//47//

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 48–55why, one step back
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,

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 56why, one step back
56// float lacunarity, float gain, float offset, int octaves)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 57–58why, one step back
57//
58// float stb_perlin_fbm_noise3(float x, float y, float z,

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 59why, one step back
59// float lacunarity, float gain, int octaves)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 60–61why, one step back
60//
61// float stb_perlin_turbulence_noise3(float x, float y, float z,

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 62why, one step back
62// float lacunarity, float gain, int octaves)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 63–72why, one step back
63//
64// Typical values to start playing with:65// octaves = 6 -- number of "octaves" of noise3() to sum66// lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output)67// gain = 0.5 -- relative weighting applied to each successive octave68// offset = 1.0? -- used to invert the ridges, may need to be larger, not sure69// 70//71// Contributors:72// Jack Mott - additional noise functions

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 73why, one step back
73// Jordan Peck - seeded noise

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

line 74why, one step back
74//

version numberSean Barrettbefore adoption0c1232f

lines 75–77why, one step back
75
7677#ifdef __cplusplus

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 78–79why, one step back
78extern "C" {
79#endif

version numberSean Barrettbefore adoption0c1232f

line 80why, one step back
80extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 81–83why, one step back
81extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);
82extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);83extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);

stb_perlin: add non-power-of-two wrapping noiseSean Barrettbefore adoption8ca86ee

line 84why, one step back
84extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed);

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 85–86why, one step back
85#ifdef __cplusplus
86}

version numberSean Barrettbefore adoption0c1232f

lines 87–90why, one step back
87#endif
8889#ifdef STB_PERLIN_IMPLEMENTATION90

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 91–92why, one step back
91#include <math.h> // fabs()
92

version numberSean Barrettbefore adoption0c1232f

lines 93–94why, one step back
93// not same permutation table as Perlin's reference to avoid copyright issues;
94// Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

line 95why, one step back
95static unsigned char stb__perlin_randtab[512] =

version numberSean Barrettbefore adoption0c1232f

lines 96–130why, one step back
96{
97 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, 98 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, 99 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240, 100 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57, 101 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233, 102 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172, 103 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243, 104 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122, 105 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76, 106 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246, 107 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3, 108 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231, 109 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221, 110 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62, 111 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135, 112 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5, 113114 // and a second copy so we don't need an extra mask or static initializer115 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, 116 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, 117 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240, 118 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57, 119 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233, 120 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172, 121 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243, 122 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122, 123 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76, 124 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246, 125 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3, 126 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231, 127 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221, 128 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62, 129 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135, 130 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 131–175why, one step back
131};
132133134// perlin's gradient has 12 cases so some get used 1/16th of the time135// and some 2/16ths. We reduce bias by changing those fractions136// to 5/64ths and 6/64ths137138// this array is designed to match the previous implementation139// of gradient hash: indices[stb__perlin_randtab[i]&63]140static unsigned char stb__perlin_randtab_grad_idx[512] =141{142 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,143 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,144 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,145 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,146 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,147 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,148 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,149 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,150 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,151 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,152 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,153 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,154 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,155 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,156 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,157 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,158159 // and a second copy so we don't need an extra mask or static initializer160 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,161 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,162 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,163 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,164 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,165 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,166 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,167 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,168 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,169 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,170 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,171 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,172 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,173 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,174 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,175 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,

version numberSean Barrettbefore adoption0c1232f

lines 176–182why, one step back
176};
177178static float stb__perlin_lerp(float a, float b, float t)179{180 return a + (b-a) * t;181}182

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 183–184why, one step back
183static int stb__perlin_fastfloor(float a)
184{

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 185–186why, one step back
185 int ai = (int) a;
186 return (a < ai) ? ai-1 : ai;

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 187–188why, one step back
187}
188

version numberSean Barrettbefore adoption0c1232f

line 189why, one step back
189// different grad function from Perlin's, but easy to modify to match reference

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 190why, one step back
190static float stb__perlin_grad(int grad_idx, float x, float y, float z)

version numberSean Barrettbefore adoption0c1232f

lines 191–207why, one step back
191{
192 static float basis[12][4] =193 {194 { 1, 1, 0 },195 { -1, 1, 0 },196 { 1,-1, 0 },197 { -1,-1, 0 },198 { 1, 0, 1 },199 { -1, 0, 1 },200 { 1, 0,-1 },201 { -1, 0,-1 },202 { 0, 1, 1 },203 { 0,-1, 1 },204 { 0, 1,-1 },205 { 0,-1,-1 },206 };207

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 208why, one step back
208 float *grad = basis[grad_idx];

version numberSean Barrettbefore adoption0c1232f

lines 209–211why, one step back
209 return grad[0]*x + grad[1]*y + grad[2]*z;
210}211

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 212why, one step back
212float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)

version numberSean Barrettbefore adoption0c1232f

lines 213–221why, one step back
213{
214 float u,v,w;215 float n000,n001,n010,n011,n100,n101,n110,n111;216 float n00,n01,n10,n11;217 float n0,n1;218219 unsigned int x_mask = (x_wrap-1) & 255;220 unsigned int y_mask = (y_wrap-1) & 255;221 unsigned int z_mask = (z_wrap-1) & 255;

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 222–224why, one step back
222 int px = stb__perlin_fastfloor(x);
223 int py = stb__perlin_fastfloor(y);224 int pz = stb__perlin_fastfloor(z);

version numberSean Barrettbefore adoption0c1232f

lines 225–235why, one step back
225 int x0 = px & x_mask, x1 = (px+1) & x_mask;
226 int y0 = py & y_mask, y1 = (py+1) & y_mask;227 int z0 = pz & z_mask, z1 = (pz+1) & z_mask;228 int r0,r1, r00,r01,r10,r11;229230 #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)231232 x -= px; u = stb__perlin_ease(x);233 y -= py; v = stb__perlin_ease(y);234 z -= pz; w = stb__perlin_ease(z);235

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 236–237why, one step back
236 r0 = stb__perlin_randtab[x0+seed];
237 r1 = stb__perlin_randtab[x1+seed];

version numberSean Barrettbefore adoption0c1232f

lines 238–243why, one step back
238
239 r00 = stb__perlin_randtab[r0+y0];240 r01 = stb__perlin_randtab[r0+y1];241 r10 = stb__perlin_randtab[r1+y0];242 r11 = stb__perlin_randtab[r1+y1];243

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 244–251why, one step back
244 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );
245 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );246 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );247 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );248 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );249 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );250 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );251 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );

version numberSean Barrettbefore adoption0c1232f

lines 252–262why, one step back
252
253 n00 = stb__perlin_lerp(n000,n001,w);254 n01 = stb__perlin_lerp(n010,n011,w);255 n10 = stb__perlin_lerp(n100,n101,w);256 n11 = stb__perlin_lerp(n110,n111,w);257258 n0 = stb__perlin_lerp(n00,n01,v);259 n1 = stb__perlin_lerp(n10,n11,v);260261 return stb__perlin_lerp(n0,n1,u);262}

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

line 263why, one step back
263

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 264why, one step back
264float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

line 265why, one step back
265{

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 266–268why, one step back
266 return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap,0);
267}268

added integer seed to noise function, use it in multi-octave noisesSean Barrettbefore adoptioncc53512

lines 269–273why, one step back
269float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed)
270{271 return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap, (unsigned char) seed);272}273

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

lines 274–275why, one step back
274float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)
275{

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 276–282why, one step back
276 int i;
277 float frequency = 1.0f;278 float prev = 1.0f;279 float amplitude = 0.5f;280 float sum = 0.0f;281282 for (i = 0; i < octaves; i++) {

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 283why, one step back
283 float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i);

added integer seed to noise function, use it in multi-octave noisesSean Barrettbefore adoptioncc53512

line 284why, one step back
284 r = offset - (float) fabs(r);

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 285–293why, one step back
285 r = r*r;
286 sum += r*amplitude*prev;287 prev = r;288 frequency *= lacunarity;289 amplitude *= gain;290 }291 return sum;292}293

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 294why, one step back
294float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 295–301why, one step back
295{
296 int i;297 float frequency = 1.0f;298 float amplitude = 1.0f;299 float sum = 0.0f;300 301 for (i = 0; i < octaves; i++) {

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 302why, one step back
302 sum += stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 303–308why, one step back
303 frequency *= lacunarity;
304 amplitude *= gain;305 }306 return sum;307}308

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 309why, one step back
309float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 310–316why, one step back
310{
311 int i;312 float frequency = 1.0f;313 float amplitude = 1.0f;314 float sum = 0.0f;315 316 for (i = 0; i < octaves; i++) {

Merge branch 'perlin' of https://github.com/Auburns/stb into workingSean Barrettbefore adoptionca980dd

line 317why, one step back
317 float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;

added integer seed to noise function, use it in multi-octave noisesSean Barrettbefore adoptioncc53512

line 318why, one step back
318 sum += (float) fabs(r);

Merge branch 'master' into workingSean Barrettbefore adoption98e825a

lines 319–324why, one step back
319 frequency *= lacunarity;
320 amplitude *= gain;321 }322 return sum;323}324

stb_perlin: add non-power-of-two wrapping noiseSean Barrettbefore adoption8ca86ee

lines 325–384why, one step back
325float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
326{327 float u,v,w;328 float n000,n001,n010,n011,n100,n101,n110,n111;329 float n00,n01,n10,n11;330 float n0,n1;331332 int px = stb__perlin_fastfloor(x);333 int py = stb__perlin_fastfloor(y);334 int pz = stb__perlin_fastfloor(z);335 int x_wrap2 = (x_wrap ? x_wrap : 256);336 int y_wrap2 = (y_wrap ? y_wrap : 256);337 int z_wrap2 = (z_wrap ? z_wrap : 256);338 int x0 = px % x_wrap2, x1;339 int y0 = py % y_wrap2, y1;340 int z0 = pz % z_wrap2, z1;341 int r0,r1, r00,r01,r10,r11;342343 if (x0 < 0) x0 += x_wrap2;344 if (y0 < 0) y0 += y_wrap2;345 if (z0 < 0) z0 += z_wrap2;346 x1 = (x0+1) % x_wrap2;347 y1 = (y0+1) % y_wrap2;348 z1 = (z0+1) % z_wrap2;349350 #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)351352 x -= px; u = stb__perlin_ease(x);353 y -= py; v = stb__perlin_ease(y);354 z -= pz; w = stb__perlin_ease(z);355356 r0 = stb__perlin_randtab[x0];357 r0 = stb__perlin_randtab[r0+seed];358 r1 = stb__perlin_randtab[x1];359 r1 = stb__perlin_randtab[r1+seed];360361 r00 = stb__perlin_randtab[r0+y0];362 r01 = stb__perlin_randtab[r0+y1];363 r10 = stb__perlin_randtab[r1+y0];364 r11 = stb__perlin_randtab[r1+y1];365366 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );367 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );368 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );369 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );370 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );371 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );372 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );373 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );374375 n00 = stb__perlin_lerp(n000,n001,w);376 n01 = stb__perlin_lerp(n010,n011,w);377 n10 = stb__perlin_lerp(n100,n101,w);378 n11 = stb__perlin_lerp(n110,n111,w);379380 n0 = stb__perlin_lerp(n00,n01,v);381 n1 = stb__perlin_lerp(n10,n11,v);382383 return stb__perlin_lerp(n0,n1,u);384}

version numberSean Barrettbefore adoption0c1232f

lines 385–427why, one step back
385#endif // STB_PERLIN_IMPLEMENTATION
386387/*388------------------------------------------------------------------------------389This software is available under 2 licenses -- choose whichever you prefer.390------------------------------------------------------------------------------391ALTERNATIVE A - MIT License392Copyright (c) 2017 Sean Barrett393Permission is hereby granted, free of charge, to any person obtaining a copy of 394this software and associated documentation files (the "Software"), to deal in 395the Software without restriction, including without limitation the rights to 396use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 397of the Software, and to permit persons to whom the Software is furnished to do 398so, subject to the following conditions:399The above copyright notice and this permission notice shall be included in all 400copies or substantial portions of the Software.401THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 402IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 403FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 404AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 405LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 406OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 407SOFTWARE.408------------------------------------------------------------------------------409ALTERNATIVE B - Public Domain (www.unlicense.org)410This is free and unencumbered software released into the public domain.411Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 412software, either in source code form or as a compiled binary, for any purpose, 413commercial or non-commercial, and by any means.414In jurisdictions that recognize copyright laws, the author or authors of this 415software dedicate any and all copyright interest in the software to the public 416domain. We make this dedication for the benefit of the public at large and to 417the detriment of our heirs and successors. We intend this dedication to be an 418overt act of relinquishment in perpetuity of all present and future rights to 419this software under copyright law.420THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 421IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 422FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 423AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 424ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 425WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.426------------------------------------------------------------------------------427*/