stb_perlin: add non-power-of-two wrapping noise
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 26a02f8 · Browse files · 1 file·+61 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+61 added−0 removed
- other:+61 added−0 removed
Provenance
- Commit
8ca86ee- Tree
5c2f5bb- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
26a02f8
1 file changed+61 added−0 removed
modified:stb_perlin.h+61 added
79 unmodified lines80 extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);81 extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);82 extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);83 extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);84+extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed);85 #ifdef __cplusplus86 }87 #endif88 232 unmodified lines321 }322 return sum;323 }324 325+float 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;331+332+ 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;342+343+ 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;349+350+ #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)351+352+ x -= px; u = stb__perlin_ease(x);353+ y -= py; v = stb__perlin_ease(y);354+ z -= pz; w = stb__perlin_ease(z);355+356+ 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];360+361+ 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];365+366+ 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 );374+375+ 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);379+380+ n0 = stb__perlin_lerp(n00,n01,v);381+ n1 = stb__perlin_lerp(n10,n11,v);382+383+ return stb__perlin_lerp(n0,n1,u);384+}385 #endif // STB_PERLIN_IMPLEMENTATION386 387 /*388 ------------------------------------------------------------------------------39 unmodified lines