Merge branch 'perlin' of https://github.com/Auburns/stb into working
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 · parents a28a6b1, 15685cc · Browse files · 1 file·+82 added−50 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+82 added−50 removed
- other:+82 added−50 removed
Provenance
- Commit
ca980dd- Tree
204409f- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent 1
a28a6b1- Parent 2
15685cc
1 file changed+82 added−50 removed
modified:stb_perlin.h+82 added-50 removed
38 unmodified lines39 // provided. Note that each function will call stb_perlin_noise3 40 // 'octaves' times, so this parameter will affect runtime.41 //42 // float stb_perlin_ridge_noise3(float x, float y, float z,43-// float lacunarity, float gain, float offset, int octaves,44-// int x_wrap, int y_wrap, int z_wrap);43+// float lacunarity, float gain, float offset, int octaves)44 //45 // float stb_perlin_fbm_noise3(float x, float y, float z,47-// float lacunarity, float gain, int octaves,48-// int x_wrap, int y_wrap, int z_wrap);46+// float lacunarity, float gain, int octaves)47 //48 // float stb_perlin_turbulence_noise3(float x, float y, float z,51-// float lacunarity, float gain,int octaves,52-// int x_wrap, int y_wrap, int z_wrap);49+// float lacunarity, float gain, int octaves)50 //51 // Typical values to start playing with:52 // octaves = 6 -- number of "octaves" of noise3() to sum53 // lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output)2 unmodified lines56 // 57 //58 // Contributors:59 // Jack Mott - additional noise functions60+// Jordan Peck - seeded noise61 //62 63 64 #ifdef __cplusplus65 extern "C" {66 #endif67 extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);70-extern float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap);71-extern float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);72-extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);68+extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);69+extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);70+extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);71 #ifdef __cplusplus72 }73 #endif74 75 #ifdef STB_PERLIN_IMPLEMENTATION76 77+#include <math.h> // fabs()78+79 // not same permutation table as Perlin's reference to avoid copyright issues;80 // Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/81-// @OPTIMIZE: should this be unsigned char instead of int for cache?81 static unsigned char stb__perlin_randtab[512] =82 {83 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, 84 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, 28 unmodified lines113 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221, 114 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62, 115 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135, 116 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5, 117+}; 118+119+120+// perlin's gradient has 12 cases so some get used 1/16th of the time121+// and some 2/16ths. We reduce bias by changing those fractions122+// to 5/64ths and 6/64ths123+124+// this array is designed to match the previous implementation125+// of gradient hash: indices[stb__perlin_randtab[i]&63]126+static unsigned char stb__perlin_randtab_grad_idx[512] =127+{128+ 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,129+ 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,130+ 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,131+ 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,132+ 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,133+ 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,134+ 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,135+ 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,136+ 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,137+ 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,138+ 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,139+ 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,140+ 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,141+ 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,142+ 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,143+ 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,144+145+ // and a second copy so we don't need an extra mask or static initializer146+ 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,147+ 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,148+ 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,149+ 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,150+ 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,151+ 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,152+ 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,153+ 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,154+ 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,155+ 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,156+ 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,157+ 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,158+ 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,159+ 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,160+ 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,161+ 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,162 };163 164 static float stb__perlin_lerp(float a, float b, float t)165 {1 unmodified line167 }168 169 static int stb__perlin_fastfloor(float a)170 {127- int ai = (int) a;128- return (a < ai) ? ai-1 : ai;171+ int ai = (int) a;172+ return (a < ai) ? ai-1 : ai;173 }174 175 // different grad function from Perlin's, but easy to modify to match reference132-static float stb__perlin_grad(int hash, float x, float y, float z)176+static float stb__perlin_grad(int grad_idx, float x, float y, float z)177 {178 static float basis[12][4] =179 {180 { 1, 1, 0 },9 unmodified lines190 { 0, 1,-1 },191 { 0,-1,-1 },192 };193 150- // perlin's gradient has 12 cases so some get used 1/16th of the time151- // and some 2/16ths. We reduce bias by changing those fractions152- // to 5/64ths and 6/64ths, and the same 4 cases get the extra weight.153- static unsigned char indices[64] =154- {155- 0,1,2,3,4,5,6,7,8,9,10,11,156- 0,9,1,11,157- 0,1,2,3,4,5,6,7,8,9,10,11,158- 0,1,2,3,4,5,6,7,8,9,10,11,159- 0,1,2,3,4,5,6,7,8,9,10,11,160- 0,1,2,3,4,5,6,7,8,9,10,11,161- };162-163- // if you use reference permutation table, change 63 below to 15 to match reference164- // (this is why the ordering of the table above is funky)165- float *grad = basis[indices[hash & 63]];194+ float *grad = basis[grad_idx];195 return grad[0]*x + grad[1]*y + grad[2]*z;196 }197 169-float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)198+float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)199 {200 float u,v,w;201 float n000,n001,n010,n011,n100,n101,n110,n111;202 float n00,n01,n10,n11;15 unmodified lines218 x -= px; u = stb__perlin_ease(x);219 y -= py; v = stb__perlin_ease(y);220 z -= pz; w = stb__perlin_ease(z);221 193- r0 = stb__perlin_randtab[x0];194- r1 = stb__perlin_randtab[x1];222+ r0 = stb__perlin_randtab[x0+seed];223+ r1 = stb__perlin_randtab[x1+seed];224 225 r00 = stb__perlin_randtab[r0+y0];226 r01 = stb__perlin_randtab[r0+y1];227 r10 = stb__perlin_randtab[r1+y0];228 r11 = stb__perlin_randtab[r1+y1];229 201- n000 = stb__perlin_grad(stb__perlin_randtab[r00+z0], x , y , z );202- n001 = stb__perlin_grad(stb__perlin_randtab[r00+z1], x , y , z-1 );203- n010 = stb__perlin_grad(stb__perlin_randtab[r01+z0], x , y-1, z );204- n011 = stb__perlin_grad(stb__perlin_randtab[r01+z1], x , y-1, z-1 );205- n100 = stb__perlin_grad(stb__perlin_randtab[r10+z0], x-1, y , z );206- n101 = stb__perlin_grad(stb__perlin_randtab[r10+z1], x-1, y , z-1 );207- n110 = stb__perlin_grad(stb__perlin_randtab[r11+z0], x-1, y-1, z );208- n111 = stb__perlin_grad(stb__perlin_randtab[r11+z1], x-1, y-1, z-1 );230+ n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );231+ n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );232+ n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );233+ n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );234+ n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );235+ n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );236+ n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );237+ n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );238 239 n00 = stb__perlin_lerp(n000,n001,w);240 n01 = stb__perlin_lerp(n010,n011,w);241 n10 = stb__perlin_lerp(n100,n101,w);4 unmodified lines246 247 return stb__perlin_lerp(n0,n1,u);248 }249 221-float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap)250+float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)251 {252+ return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap,0);253+}254+255+float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)256+{257 int i;258 float frequency = 1.0f;259 float prev = 1.0f;260 float amplitude = 0.5f;261 float sum = 0.0f;262 263 for (i = 0; i < octaves; i++) {230- float r = (float)(stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap));231- r = r<0 ? -r : r; // fabs()232- r = offset - r;264+ float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i);265+ r = offset - fabs(r);266 r = r*r;267 sum += r*amplitude*prev;268 prev = r;269 frequency *= lacunarity;1 unmodified line271 }272 return sum;273 }274 242-float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)275+float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)276 {277 int i;278 float frequency = 1.0f;279 float amplitude = 1.0f;280 float sum = 0.0f;281 282 for (i = 0; i < octaves; i++) {250- sum += stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;283+ sum += stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;284 frequency *= lacunarity;285 amplitude *= gain;286 }287 return sum;288 }289 257-float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)290+float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)291 {292 int i;293 float frequency = 1.0f;294 float amplitude = 1.0f;295 float sum = 0.0f;296 297 for (i = 0; i < octaves; i++) {265- float r = stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;266- r = r<0 ? -r : r; // fabs()267- sum += r;298+ float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;299+ sum += fabs(r);300 frequency *= lacunarity;301 amplitude *= gain;302 }303 return sum;45 unmodified lines