diff --git a/lib_paysages/noise.c b/lib_paysages/noise.c index c5b30cf..b84af22 100644 --- a/lib_paysages/noise.c +++ b/lib_paysages/noise.c @@ -7,6 +7,7 @@ #include "tools.h" #include "noisesimplex.h" #include "noisenaive.h" +#include "noiseperlin.h" #define MAX_LEVEL_COUNT 30 @@ -28,6 +29,7 @@ struct NoiseGenerator void noiseInit() { noiseSimplexInit(); + noisePerlinInit(); noiseNaiveInit(); } @@ -143,10 +145,11 @@ void noiseValidate(NoiseGenerator* generator) generator->_func_noise_3d = noiseSimplexGet3DValue; break; case NOISE_FUNCTION_PERLIN: - /*TODO*/ + generator->_func_noise_1d = noisePerlinGet1DValue; + generator->_func_noise_2d = noisePerlinGet2DValue; + generator->_func_noise_3d = noisePerlinGet3DValue; break; case NOISE_FUNCTION_NAIVE: - /* TODO */ generator->_func_noise_1d = noiseNaiveGet1DValue; generator->_func_noise_2d = noiseNaiveGet2DValue; generator->_func_noise_3d = noiseNaiveGet3DValue; diff --git a/lib_paysages/noiseperlin.c b/lib_paysages/noiseperlin.c new file mode 100644 index 0000000..0009250 --- /dev/null +++ b/lib_paysages/noiseperlin.c @@ -0,0 +1,189 @@ +#include "noisesimplex.h" + +/* + * Perlin noise implementation. + * + * Based on Ken Perlin implementation. + */ + +#include +#include +#include + +#include +#include +#include + +#define B 0x100 +#define BM 0xff + +#define N 0x1000 +#define NP 12 /* 2^N */ +#define NM 0xfff + +static int p[B + B + 2]; +static double g3[B + B + 2][3]; +static double g2[B + B + 2][2]; +static double g1[B + B + 2]; + +#define s_curve(t) ( t * t * (3. - 2. * t) ) + +#define lerp(t, a, b) ( a + t * (b - a) ) + +#define setup(i,b0,b1,r0,r1)\ + t = vec[i] + N;\ + b0 = ((int)t) & BM;\ + b1 = (b0+1) & BM;\ + r0 = t - (int)t;\ + r1 = r0 - 1.; + +double noisePerlinGet1DValue(double x) +{ + double vec[1] = {x}; + int bx0, bx1; + double rx0, rx1, sx, t, u, v; + + setup(0, bx0,bx1, rx0,rx1); + + sx = s_curve(rx0); + + u = rx0 * g1[ p[ bx0 ] ]; + v = rx1 * g1[ p[ bx1 ] ]; + + return lerp(sx, u, v); +} + +double noisePerlinGet2DValue(double x, double y) +{ + double vec[2] = {x, y}; + int bx0, bx1, by0, by1, b00, b10, b01, b11; + double rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v; + int i, j; + + setup(0, bx0,bx1, rx0,rx1); + setup(1, by0,by1, ry0,ry1); + + i = p[ bx0 ]; + j = p[ bx1 ]; + + b00 = p[ i + by0 ]; + b10 = p[ j + by0 ]; + b01 = p[ i + by1 ]; + b11 = p[ j + by1 ]; + + sx = s_curve(rx0); + sy = s_curve(ry0); + +#define at2(rx,ry) ( rx * q[0] + ry * q[1] ) + + q = g2[ b00 ] ; u = at2(rx0,ry0); + q = g2[ b10 ] ; v = at2(rx1,ry0); + a = lerp(sx, u, v); + + q = g2[ b01 ] ; u = at2(rx0,ry1); + q = g2[ b11 ] ; v = at2(rx1,ry1); + b = lerp(sx, u, v); + + return lerp(sy, a, b); +} + +double noisePerlinGet3DValue(double x, double y, double z) +{ + double vec[3] = {x, y, z}; + int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; + double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v; + int i, j; + + setup(0, bx0,bx1, rx0,rx1); + setup(1, by0,by1, ry0,ry1); + setup(2, bz0,bz1, rz0,rz1); + + i = p[ bx0 ]; + j = p[ bx1 ]; + + b00 = p[ i + by0 ]; + b10 = p[ j + by0 ]; + b01 = p[ i + by1 ]; + b11 = p[ j + by1 ]; + + t = s_curve(rx0); + sy = s_curve(ry0); + sz = s_curve(rz0); + +#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] ) + + q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0); + q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0); + a = lerp(t, u, v); + + q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0); + q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0); + b = lerp(t, u, v); + + c = lerp(sy, a, b); + + q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1); + q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1); + a = lerp(t, u, v); + + q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1); + q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1); + b = lerp(t, u, v); + + d = lerp(sy, a, b); + + return lerp(sz, c, d); +} + +static void _normalize2(double v[2]) +{ + double s; + + s = sqrt(v[0] * v[0] + v[1] * v[1]); + v[0] = v[0] / s; + v[1] = v[1] / s; +} + +static void _normalize3(double v[3]) +{ + double s; + + s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); + v[0] = v[0] / s; + v[1] = v[1] / s; + v[2] = v[2] / s; +} + +void noisePerlinInit(void) +{ + int i, j, k; + + for (i = 0 ; i < B ; i++) { + p[i] = i; + + g1[i] = (double)((random() % (B + B)) - B) / B; + + for (j = 0 ; j < 2 ; j++) + g2[i][j] = (double)((random() % (B + B)) - B) / B; + _normalize2(g2[i]); + + for (j = 0 ; j < 3 ; j++) + g3[i][j] = (double)((random() % (B + B)) - B) / B; + _normalize3(g3[i]); + } + + while (--i) { + k = p[i]; + p[i] = p[j = random() % B]; + p[j] = k; + } + + for (i = 0 ; i < B + 2 ; i++) { + p[B + i] = p[i]; + g1[B + i] = g1[i]; + for (j = 0 ; j < 2 ; j++) + g2[B + i][j] = g2[i][j]; + for (j = 0 ; j < 3 ; j++) + g3[B + i][j] = g3[i][j]; + } +} diff --git a/lib_paysages/noiseperlin.h b/lib_paysages/noiseperlin.h new file mode 100644 index 0000000..d45f91a --- /dev/null +++ b/lib_paysages/noiseperlin.h @@ -0,0 +1,18 @@ +#ifndef _PAYSAGES_NOISEPERLIN_H_ +#define _PAYSAGES_NOISEPERLIN_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void noisePerlinInit(); +double noisePerlinGet1DValue(double x); +double noisePerlinGet2DValue(double x, double y); +double noisePerlinGet3DValue(double x, double y, double z); +/*double noiseSimplexGet4DValue(double x, double y, double z, double w);*/ + +#ifdef __cplusplus +} +#endif + +#endif