From 418fe54b26aae821a86f01fb02a3447e878569df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 27 Feb 2012 15:25:12 +0000 Subject: [PATCH] paysages : Factorizing noise random pool (WIP). git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@273 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- lib_paysages/clouds.c | 4 +- lib_paysages/noise.c | 251 +++++++++++++++++++++------------------- lib_paysages/noise.h | 9 +- lib_paysages/scenery.c | 4 + lib_paysages/terrain.c | 4 +- lib_paysages/textures.c | 4 +- lib_paysages/water.c | 4 +- 7 files changed, 149 insertions(+), 131 deletions(-) diff --git a/lib_paysages/clouds.c b/lib_paysages/clouds.c index 658a7e5..e1d8ebf 100644 --- a/lib_paysages/clouds.c +++ b/lib_paysages/clouds.c @@ -45,7 +45,7 @@ void cloudsSave(FILE* f, CloudsDefinition* definition) toolsSaveDouble(f, &layer->ycenter); toolsSaveDouble(f, &layer->ymin); toolsSaveDouble(f, &layer->ymax); - noiseSave(f, layer->noise); + noiseSaveGenerator(f, layer->noise); materialSave(f, &layer->material); toolsSaveDouble(f, &layer->transparencydepth); toolsSaveDouble(f, &layer->lighttraversal); @@ -73,7 +73,7 @@ void cloudsLoad(FILE* f, CloudsDefinition* definition) toolsLoadDouble(f, &layer->ycenter); toolsLoadDouble(f, &layer->ymin); toolsLoadDouble(f, &layer->ymax); - noiseLoad(f, layer->noise); + noiseLoadGenerator(f, layer->noise); materialLoad(f, &layer->material); toolsLoadDouble(f, &layer->transparencydepth); toolsLoadDouble(f, &layer->lighttraversal); diff --git a/lib_paysages/noise.c b/lib_paysages/noise.c index b72433b..67587c8 100644 --- a/lib_paysages/noise.c +++ b/lib_paysages/noise.c @@ -13,17 +13,60 @@ struct NoiseGenerator int size1; int size2; int size3; - double* noise; double height_offset; int level_count; struct NoiseLevel* levels; }; +static int _noise_pool_size; +static double* _noise_pool; + static inline double _cubicInterpolate(double* p, double x) { return p[1] + 0.5 * x * (p[2] - p[0] + x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] + x * (3.0 * (p[1] - p[2]) + p[3] - p[0]))); } +void noiseInit() +{ + int i; + + _noise_pool_size = 1048576; + _noise_pool = malloc(sizeof(double) * _noise_pool_size); + + for (i = 0; i < _noise_pool_size; i++) + { + _noise_pool[i] = toolsRandom() - 0.5; + } +} + +void noiseQuit() +{ + free(_noise_pool); +} + +void noiseSave(FILE* f) +{ + int i; + + toolsSaveInt(f, &_noise_pool_size); + for (i = 0; i < _noise_pool_size; i++) + { + toolsSaveDouble(f, _noise_pool + i); + } +} + +void noiseLoad(FILE* f) +{ + int i; + + toolsLoadInt(f, &_noise_pool_size); + _noise_pool = realloc(_noise_pool, sizeof(double) * _noise_pool_size); + for (i = 0; i < _noise_pool_size; i++) + { + toolsLoadDouble(f, _noise_pool + i); + } +} + NoiseGenerator* noiseCreateGenerator() { NoiseGenerator* result; @@ -33,8 +76,6 @@ NoiseGenerator* noiseCreateGenerator() result->size1 = 1; result->size2 = 1; result->size3 = 1; - result->noise = malloc(sizeof(double)); - result->noise[0] = 0.0; result->level_count = 0; result->levels = malloc(sizeof(NoiseLevel)); result->height_offset = 0.0; @@ -44,15 +85,13 @@ NoiseGenerator* noiseCreateGenerator() void noiseDeleteGenerator(NoiseGenerator* generator) { - free(generator->noise); free(generator->levels); free(generator); } -void noiseSave(FILE* f, NoiseGenerator* perlin) +void noiseSaveGenerator(FILE* f, NoiseGenerator* perlin) { int x; - double* it_noise; toolsSaveInt(f, &perlin->size1); toolsSaveInt(f, &perlin->size2); @@ -60,12 +99,6 @@ void noiseSave(FILE* f, NoiseGenerator* perlin) toolsSaveDouble(f, &perlin->height_offset); toolsSaveInt(f, &perlin->level_count); - it_noise = perlin->noise; - for (x = 0; x < perlin->size1; x++) - { - toolsSaveDouble(f, it_noise++); - } - for (x = 0; x < perlin->level_count; x++) { NoiseLevel* level = perlin->levels + x; @@ -78,10 +111,9 @@ void noiseSave(FILE* f, NoiseGenerator* perlin) } } -void noiseLoad(FILE* f, NoiseGenerator* perlin) +void noiseLoadGenerator(FILE* f, NoiseGenerator* perlin) { int x; - double* it_noise; toolsLoadInt(f, &perlin->size1); toolsLoadInt(f, &perlin->size2); @@ -89,13 +121,6 @@ void noiseLoad(FILE* f, NoiseGenerator* perlin) toolsLoadDouble(f, &perlin->height_offset); toolsLoadInt(f, &perlin->level_count); - perlin->noise = realloc(perlin->noise, sizeof(double) * perlin->size1); - it_noise = perlin->noise; - for (x = 0; x < perlin->size1; x++) - { - toolsLoadDouble(f, it_noise++); - } - perlin->levels = realloc(perlin->levels, sizeof(NoiseLevel) * perlin->level_count); for (x = 0; x < perlin->level_count; x++) { @@ -117,31 +142,18 @@ void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination) destination->height_offset = source->height_offset; destination->level_count = source->level_count; - destination->noise = realloc(destination->noise, sizeof(double) * destination->size1); - memcpy(destination->noise, source->noise, sizeof(double) * destination->size1); - destination->levels = realloc(destination->levels, sizeof(NoiseLevel) * destination->level_count); memcpy(destination->levels, source->levels, sizeof(NoiseLevel) * destination->level_count); } void noiseGenerateBaseNoise(NoiseGenerator* generator, int size) { - int x; - double* it_noise; - size = (size < 1) ? 1 : size; size = (size > 4000000) ? 4000000 : size; generator->size1 = size; generator->size2 = (int)floor(sqrt((float)size)); generator->size3 = (int)floor(cbrt((float)size)); - generator->noise = realloc(generator->noise, sizeof(double) * size); - - it_noise = generator->noise; - for (x = 0; x < size; x++) - { - *(it_noise++) = toolsRandom() - 0.5; - } } int noiseGetBaseSize(NoiseGenerator* generator) @@ -296,7 +308,6 @@ void noiseNormalizeHeight(NoiseGenerator* generator, double min_height, double m static inline double _get1DRawNoiseValue(NoiseGenerator* generator, double x) { - double* noise = generator->noise; int size = generator->size1; int xbase = (int)floor(x); @@ -326,10 +337,10 @@ static inline double _get1DRawNoiseValue(NoiseGenerator* generator, double x) double buf_cubic_x[4]; - buf_cubic_x[0] = noise[x0]; - buf_cubic_x[1] = noise[x1]; - buf_cubic_x[2] = noise[x2]; - buf_cubic_x[3] = noise[x3]; + buf_cubic_x[0] = _noise_pool[x0 % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[x1 % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[x2 % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[x3 % _noise_pool_size]; return _cubicInterpolate(buf_cubic_x, xinternal); } @@ -393,7 +404,6 @@ double noiseGet1DDetail(NoiseGenerator* generator, double x, double detail) static inline double _get2DRawNoiseValue(NoiseGenerator* generator, double x, double y) { - double* noise = generator->noise; int size = generator->size2; int xbase = (int)floor(x); @@ -447,28 +457,28 @@ static inline double _get2DRawNoiseValue(NoiseGenerator* generator, double x, do double buf_cubic_x[4]; double buf_cubic_y[4]; - buf_cubic_x[0] = noise[y0 * size + x0]; - buf_cubic_x[1] = noise[y0 * size + x1]; - buf_cubic_x[2] = noise[y0 * size + x2]; - buf_cubic_x[3] = noise[y0 * size + x3]; + buf_cubic_x[0] = _noise_pool[(y0 * size + x0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y0 * size + x1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y0 * size + x2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y0 * size + x3) % _noise_pool_size]; buf_cubic_y[0] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y1 * size + x0]; - buf_cubic_x[1] = noise[y1 * size + x1]; - buf_cubic_x[2] = noise[y1 * size + x2]; - buf_cubic_x[3] = noise[y1 * size + x3]; + buf_cubic_x[0] = _noise_pool[(y1 * size + x0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y1 * size + x1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y1 * size + x2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y1 * size + x3) % _noise_pool_size]; buf_cubic_y[1] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y2 * size + x0]; - buf_cubic_x[1] = noise[y2 * size + x1]; - buf_cubic_x[2] = noise[y2 * size + x2]; - buf_cubic_x[3] = noise[y2 * size + x3]; + buf_cubic_x[0] = _noise_pool[(y2 * size + x0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y2 * size + x1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y2 * size + x2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y2 * size + x3) % _noise_pool_size]; buf_cubic_y[2] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y3 * size + x0]; - buf_cubic_x[1] = noise[y3 * size + x1]; - buf_cubic_x[2] = noise[y3 * size + x2]; - buf_cubic_x[3] = noise[y3 * size + x3]; + buf_cubic_x[0] = _noise_pool[(y3 * size + x0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y3 * size + x1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y3 * size + x2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y3 * size + x3) % _noise_pool_size]; buf_cubic_y[3] = _cubicInterpolate(buf_cubic_x, xinternal); return _cubicInterpolate(buf_cubic_y, yinternal); @@ -533,7 +543,6 @@ double noiseGet2DDetail(NoiseGenerator* generator, double x, double y, double de static inline double _get3DRawNoiseValue(NoiseGenerator* generator, double x, double y, double z) { - double* noise = generator->noise; int size = generator->size3; int xbase = (int)floor(x); @@ -611,106 +620,106 @@ static inline double _get3DRawNoiseValue(NoiseGenerator* generator, double x, do double buf_cubic_y[4]; double buf_cubic_z[4]; - buf_cubic_x[0] = noise[y0 * size * size + x0 * size + z0]; - buf_cubic_x[1] = noise[y0 * size * size + x1 * size + z0]; - buf_cubic_x[2] = noise[y0 * size * size + x2 * size + z0]; - buf_cubic_x[3] = noise[y0 * size * size + x3 * size + z0]; + buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y0 * size * size + x1 * size + z0) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y0 * size * size + x2 * size + z0) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y0 * size * size + x3 * size + z0) % _noise_pool_size]; buf_cubic_y[0] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y1 * size * size + x0 * size + z0]; - buf_cubic_x[1] = noise[y1 * size * size + x1 * size + z0]; - buf_cubic_x[2] = noise[y1 * size * size + x2 * size + z0]; - buf_cubic_x[3] = noise[y1 * size * size + x3 * size + z0]; + buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y1 * size * size + x1 * size + z0) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y1 * size * size + x2 * size + z0) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y1 * size * size + x3 * size + z0) % _noise_pool_size]; buf_cubic_y[1] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y2 * size * size + x0 * size + z0]; - buf_cubic_x[1] = noise[y2 * size * size + x1 * size + z0]; - buf_cubic_x[2] = noise[y2 * size * size + x2 * size + z0]; - buf_cubic_x[3] = noise[y2 * size * size + x3 * size + z0]; + buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y2 * size * size + x1 * size + z0) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y2 * size * size + x2 * size + z0) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y2 * size * size + x3 * size + z0) % _noise_pool_size]; buf_cubic_y[2] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y3 * size * size + x0 * size + z0]; - buf_cubic_x[1] = noise[y3 * size * size + x1 * size + z0]; - buf_cubic_x[2] = noise[y3 * size * size + x2 * size + z0]; - buf_cubic_x[3] = noise[y3 * size * size + x3 * size + z0]; + buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z0) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y3 * size * size + x1 * size + z0) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y3 * size * size + x2 * size + z0) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y3 * size * size + x3 * size + z0) % _noise_pool_size]; buf_cubic_y[3] = _cubicInterpolate(buf_cubic_x, xinternal); buf_cubic_z[0] = _cubicInterpolate(buf_cubic_y, yinternal); - buf_cubic_x[0] = noise[y0 * size * size + x0 * size + z1]; - buf_cubic_x[1] = noise[y0 * size * size + x1 * size + z1]; - buf_cubic_x[2] = noise[y0 * size * size + x2 * size + z1]; - buf_cubic_x[3] = noise[y0 * size * size + x3 * size + z1]; + buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z1) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y0 * size * size + x1 * size + z1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y0 * size * size + x2 * size + z1) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y0 * size * size + x3 * size + z1) % _noise_pool_size]; buf_cubic_y[0] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y1 * size * size + x0 * size + z1]; - buf_cubic_x[1] = noise[y1 * size * size + x1 * size + z1]; - buf_cubic_x[2] = noise[y1 * size * size + x2 * size + z1]; - buf_cubic_x[3] = noise[y1 * size * size + x3 * size + z1]; + buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z1) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y1 * size * size + x1 * size + z1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y1 * size * size + x2 * size + z1) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y1 * size * size + x3 * size + z1) % _noise_pool_size]; buf_cubic_y[1] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y2 * size * size + x0 * size + z1]; - buf_cubic_x[1] = noise[y2 * size * size + x1 * size + z1]; - buf_cubic_x[2] = noise[y2 * size * size + x2 * size + z1]; - buf_cubic_x[3] = noise[y2 * size * size + x3 * size + z1]; + buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z1) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y2 * size * size + x1 * size + z1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y2 * size * size + x2 * size + z1) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y2 * size * size + x3 * size + z1) % _noise_pool_size]; buf_cubic_y[2] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y3 * size * size + x0 * size + z1]; - buf_cubic_x[1] = noise[y3 * size * size + x1 * size + z1]; - buf_cubic_x[2] = noise[y3 * size * size + x2 * size + z1]; - buf_cubic_x[3] = noise[y3 * size * size + x3 * size + z1]; + buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z1) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y3 * size * size + x1 * size + z1) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y3 * size * size + x2 * size + z1) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y3 * size * size + x3 * size + z1) % _noise_pool_size]; buf_cubic_y[3] = _cubicInterpolate(buf_cubic_x, xinternal); buf_cubic_z[1] = _cubicInterpolate(buf_cubic_y, yinternal); - buf_cubic_x[0] = noise[y0 * size * size + x0 * size + z2]; - buf_cubic_x[1] = noise[y0 * size * size + x1 * size + z2]; - buf_cubic_x[2] = noise[y0 * size * size + x2 * size + z2]; - buf_cubic_x[3] = noise[y0 * size * size + x3 * size + z2]; + buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z2) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y0 * size * size + x1 * size + z2) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y0 * size * size + x2 * size + z2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y0 * size * size + x3 * size + z2) % _noise_pool_size]; buf_cubic_y[0] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y1 * size * size + x0 * size + z2]; - buf_cubic_x[1] = noise[y1 * size * size + x1 * size + z2]; - buf_cubic_x[2] = noise[y1 * size * size + x2 * size + z2]; - buf_cubic_x[3] = noise[y1 * size * size + x3 * size + z2]; + buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z2) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y1 * size * size + x1 * size + z2) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y1 * size * size + x2 * size + z2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y1 * size * size + x3 * size + z2) % _noise_pool_size]; buf_cubic_y[1] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y2 * size * size + x0 * size + z2]; - buf_cubic_x[1] = noise[y2 * size * size + x1 * size + z2]; - buf_cubic_x[2] = noise[y2 * size * size + x2 * size + z2]; - buf_cubic_x[3] = noise[y2 * size * size + x3 * size + z2]; + buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z2) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y2 * size * size + x1 * size + z2) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y2 * size * size + x2 * size + z2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y2 * size * size + x3 * size + z2) % _noise_pool_size]; buf_cubic_y[2] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y3 * size * size + x0 * size + z2]; - buf_cubic_x[1] = noise[y3 * size * size + x1 * size + z2]; - buf_cubic_x[2] = noise[y3 * size * size + x2 * size + z2]; - buf_cubic_x[3] = noise[y3 * size * size + x3 * size + z2]; + buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z2) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y3 * size * size + x1 * size + z2) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y3 * size * size + x2 * size + z2) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y3 * size * size + x3 * size + z2) % _noise_pool_size]; buf_cubic_y[3] = _cubicInterpolate(buf_cubic_x, xinternal); buf_cubic_z[2] = _cubicInterpolate(buf_cubic_y, yinternal); - buf_cubic_x[0] = noise[y0 * size * size + x0 * size + z3]; - buf_cubic_x[1] = noise[y0 * size * size + x1 * size + z3]; - buf_cubic_x[2] = noise[y0 * size * size + x2 * size + z3]; - buf_cubic_x[3] = noise[y0 * size * size + x3 * size + z3]; + buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z3) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y0 * size * size + x1 * size + z3) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y0 * size * size + x2 * size + z3) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y0 * size * size + x3 * size + z3) % _noise_pool_size]; buf_cubic_y[0] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y1 * size * size + x0 * size + z3]; - buf_cubic_x[1] = noise[y1 * size * size + x1 * size + z3]; - buf_cubic_x[2] = noise[y1 * size * size + x2 * size + z3]; - buf_cubic_x[3] = noise[y1 * size * size + x3 * size + z3]; + buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z3) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y1 * size * size + x1 * size + z3) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y1 * size * size + x2 * size + z3) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y1 * size * size + x3 * size + z3) % _noise_pool_size]; buf_cubic_y[1] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y2 * size * size + x0 * size + z3]; - buf_cubic_x[1] = noise[y2 * size * size + x1 * size + z3]; - buf_cubic_x[2] = noise[y2 * size * size + x2 * size + z3]; - buf_cubic_x[3] = noise[y2 * size * size + x3 * size + z3]; + buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z3) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y2 * size * size + x1 * size + z3) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y2 * size * size + x2 * size + z3) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y2 * size * size + x3 * size + z3) % _noise_pool_size]; buf_cubic_y[2] = _cubicInterpolate(buf_cubic_x, xinternal); - buf_cubic_x[0] = noise[y3 * size * size + x0 * size + z3]; - buf_cubic_x[1] = noise[y3 * size * size + x1 * size + z3]; - buf_cubic_x[2] = noise[y3 * size * size + x2 * size + z3]; - buf_cubic_x[3] = noise[y3 * size * size + x3 * size + z3]; + buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z3) % _noise_pool_size]; + buf_cubic_x[1] = _noise_pool[(y3 * size * size + x1 * size + z3) % _noise_pool_size]; + buf_cubic_x[2] = _noise_pool[(y3 * size * size + x2 * size + z3) % _noise_pool_size]; + buf_cubic_x[3] = _noise_pool[(y3 * size * size + x3 * size + z3) % _noise_pool_size]; buf_cubic_y[3] = _cubicInterpolate(buf_cubic_x, xinternal); buf_cubic_z[3] = _cubicInterpolate(buf_cubic_y, yinternal); diff --git a/lib_paysages/noise.h b/lib_paysages/noise.h index 1cc2d82..d80e722 100644 --- a/lib_paysages/noise.h +++ b/lib_paysages/noise.h @@ -18,10 +18,15 @@ struct NoiseLevel typedef struct NoiseLevel NoiseLevel; typedef struct NoiseGenerator NoiseGenerator; +void noiseInit(); +void noiseQuit(); +void noiseSave(FILE* f); +void noiseLoad(FILE* f); + NoiseGenerator* noiseCreateGenerator(); void noiseDeleteGenerator(NoiseGenerator* generator); -void noiseSave(FILE* f, NoiseGenerator* perlin); -void noiseLoad(FILE* f, NoiseGenerator* perlin); +void noiseSaveGenerator(FILE* f, NoiseGenerator* perlin); +void noiseLoadGenerator(FILE* f, NoiseGenerator* perlin); void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination); void noiseGenerateBaseNoise(NoiseGenerator* generator, int size); int noiseGetBaseSize(NoiseGenerator* generator); diff --git a/lib_paysages/scenery.c b/lib_paysages/scenery.c index bb7e379..5353038 100644 --- a/lib_paysages/scenery.c +++ b/lib_paysages/scenery.c @@ -17,6 +17,7 @@ WaterDefinition _water; void sceneryInit() { + noiseInit(); atmosphereInit(); cameraInit(); cloudsInit(); @@ -55,12 +56,14 @@ void sceneryQuit() terrainQuit(); texturesQuit(); waterQuit(); + noiseQuit(); } void scenerySaveToFile(char* filepath) { FILE* f = fopen(filepath, "wb"); + noiseSave(f); atmosphereSave(f, &_atmosphere); cameraSave(f, &_camera); cloudsSave(f, &_clouds); @@ -80,6 +83,7 @@ void sceneryLoadFromFile(char* filepath) /* TODO Use intermediary definitions ? */ + noiseLoad(f); atmosphereLoad(f, &_atmosphere); cameraLoad(f, &_camera); cloudsLoad(f, &_clouds); diff --git a/lib_paysages/terrain.c b/lib_paysages/terrain.c index e14865a..7326387 100644 --- a/lib_paysages/terrain.c +++ b/lib_paysages/terrain.c @@ -25,7 +25,7 @@ void terrainSave(FILE* f, TerrainDefinition* definition) { int i; - noiseSave(f, definition->height_noise); + noiseSaveGenerator(f, definition->height_noise); toolsSaveDouble(f, &definition->height_factor); toolsSaveDouble(f, &definition->scaling); @@ -41,7 +41,7 @@ void terrainLoad(FILE* f, TerrainDefinition* definition) int i, n; HeightModifier* modifier; - noiseLoad(f, definition->height_noise); + noiseLoadGenerator(f, definition->height_noise); toolsLoadDouble(f, &definition->height_factor); toolsLoadDouble(f, &definition->scaling); diff --git a/lib_paysages/textures.c b/lib_paysages/textures.c index 4424130..a2b8814 100644 --- a/lib_paysages/textures.c +++ b/lib_paysages/textures.c @@ -33,7 +33,7 @@ void texturesSave(FILE* f, TexturesDefinition* definition) for (i = 0; i < definition->nbtextures; i++) { zoneSave(f, definition->textures[i].zone); - noiseSave(f, definition->textures[i].bump_noise); + noiseSaveGenerator(f, definition->textures[i].bump_noise); colorSave(f, &definition->textures[i].color); } } @@ -54,7 +54,7 @@ void texturesLoad(FILE* f, TexturesDefinition* definition) layer = definition->textures + texturesAddLayer(definition); zoneLoad(f, layer->zone); - noiseLoad(f, layer->bump_noise); + noiseLoadGenerator(f, layer->bump_noise); colorLoad(f, &layer->color); } diff --git a/lib_paysages/water.c b/lib_paysages/water.c index edff3b0..5db69fa 100644 --- a/lib_paysages/water.c +++ b/lib_paysages/water.c @@ -28,7 +28,7 @@ void waterSave(FILE* f, WaterDefinition* definition) toolsSaveDouble(f, &definition->transparency); toolsSaveDouble(f, &definition->reflection); toolsSaveDouble(f, &definition->lighting_depth); - noiseSave(f, definition->waves_noise); + noiseSaveGenerator(f, definition->waves_noise); toolsSaveDouble(f, &definition->waves_noise_height); toolsSaveDouble(f, &definition->waves_noise_scale); } @@ -42,7 +42,7 @@ void waterLoad(FILE* f, WaterDefinition* definition) toolsLoadDouble(f, &definition->transparency); toolsLoadDouble(f, &definition->reflection); toolsLoadDouble(f, &definition->lighting_depth); - noiseLoad(f, definition->waves_noise); + noiseLoadGenerator(f, definition->waves_noise); toolsLoadDouble(f, &definition->waves_noise_height); toolsLoadDouble(f, &definition->waves_noise_scale);