2012-01-29 17:39:56 +00:00
|
|
|
#include "noise.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tools.h"
|
2012-08-26 20:38:28 +00:00
|
|
|
#include "noisesimplex.h"
|
2012-08-28 21:13:35 +00:00
|
|
|
#include "noisenaive.h"
|
2012-08-29 12:01:51 +00:00
|
|
|
#include "noiseperlin.h"
|
2012-12-01 16:39:47 +00:00
|
|
|
#include "opencl.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
|
2012-02-27 15:42:23 +00:00
|
|
|
#define MAX_LEVEL_COUNT 30
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
struct NoiseGenerator
|
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
NoiseFunction function;
|
2012-06-17 09:40:40 +00:00
|
|
|
double height_offset;
|
2011-12-10 13:25:22 +00:00
|
|
|
int level_count;
|
2013-01-27 19:57:43 +00:00
|
|
|
NoiseLevel levels[MAX_LEVEL_COUNT];
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
double _min_value;
|
|
|
|
double _max_value;
|
2012-08-27 20:40:35 +00:00
|
|
|
double (*_func_noise_1d)(double x);
|
|
|
|
double (*_func_noise_2d)(double x, double y);
|
|
|
|
double (*_func_noise_3d)(double x, double y, double z);
|
2011-12-10 13:25:22 +00:00
|
|
|
};
|
|
|
|
|
2012-02-27 15:25:12 +00:00
|
|
|
void noiseInit()
|
|
|
|
{
|
2012-08-26 20:38:28 +00:00
|
|
|
noiseSimplexInit();
|
2012-08-29 12:01:51 +00:00
|
|
|
noisePerlinInit();
|
2012-08-28 21:13:35 +00:00
|
|
|
noiseNaiveInit();
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-08-29 20:29:41 +00:00
|
|
|
/* Noise stats */
|
|
|
|
/*NoiseGenerator* noise;
|
|
|
|
int x;
|
|
|
|
double min, max, value;
|
|
|
|
noise = noiseCreateGenerator();
|
|
|
|
noiseSetFunctionParams(noise, NOISE_FUNCTION_NAIVE, 0.0);
|
2013-01-27 19:57:43 +00:00
|
|
|
noiseAddLevelSimple(noise, 1.0, 0.0, 1.0);
|
2012-08-29 20:29:41 +00:00
|
|
|
min = 100000.0;
|
|
|
|
max = -100000.0;
|
|
|
|
for (x = 0; x < 1000000; x++)
|
|
|
|
{
|
|
|
|
value = noiseGet1DTotal(noise, toolsRandom() * 10000.0);
|
|
|
|
value = noiseGet2DTotal(noise, toolsRandom() * 10000.0, toolsRandom() * 10000.0);
|
|
|
|
value = noiseGet3DTotal(noise, toolsRandom() * 10000.0, toolsRandom() * 10000.0, toolsRandom() * 10000.0);
|
|
|
|
if (value < min) min = value;
|
|
|
|
if (value > max) max = value;
|
|
|
|
}
|
|
|
|
printf("%f %f\n", min, max);
|
|
|
|
noiseDeleteGenerator(noise);*/
|
2012-02-27 15:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void noiseQuit()
|
|
|
|
{
|
2012-08-28 21:13:35 +00:00
|
|
|
noiseNaiveQuit();
|
2012-02-27 15:25:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void noiseSave(PackStream* stream)
|
2012-02-27 15:25:12 +00:00
|
|
|
{
|
2012-08-28 21:13:35 +00:00
|
|
|
noiseNaiveSave(stream);
|
2012-02-27 15:25:12 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void noiseLoad(PackStream* stream)
|
2012-02-27 15:25:12 +00:00
|
|
|
{
|
2012-08-28 21:13:35 +00:00
|
|
|
noiseNaiveLoad(stream);
|
2012-02-27 15:25:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
NoiseGenerator* noiseCreateGenerator()
|
|
|
|
{
|
|
|
|
NoiseGenerator* result;
|
|
|
|
|
|
|
|
/* initialize */
|
|
|
|
result = malloc(sizeof(NoiseGenerator));
|
2012-08-27 20:40:35 +00:00
|
|
|
result->function.algorithm = NOISE_FUNCTION_SIMPLEX;
|
|
|
|
result->function.ridge_factor = 0.0;
|
2013-01-27 19:57:43 +00:00
|
|
|
result->function.curve_factor = 0.0;
|
2011-12-10 13:25:22 +00:00
|
|
|
result->level_count = 0;
|
|
|
|
result->height_offset = 0.0;
|
2012-08-27 20:40:35 +00:00
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
noiseRandomizeOffsets(result);
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(result);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void noiseDeleteGenerator(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
free(generator);
|
|
|
|
}
|
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
void noiseSaveGenerator(PackStream* stream, NoiseGenerator* generator)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
x = (int)generator->function.algorithm;
|
|
|
|
packWriteInt(stream, &x);
|
|
|
|
packWriteDouble(stream, &generator->function.ridge_factor);
|
2013-01-27 19:57:43 +00:00
|
|
|
packWriteDouble(stream, &generator->function.curve_factor);
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
packWriteDouble(stream, &generator->height_offset);
|
|
|
|
packWriteInt(stream, &generator->level_count);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
for (x = 0; x < generator->level_count; x++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-08-26 20:02:10 +00:00
|
|
|
NoiseLevel* level = generator->levels + x;
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
packWriteDouble(stream, &level->wavelength);
|
|
|
|
packWriteDouble(stream, &level->amplitude);
|
|
|
|
packWriteDouble(stream, &level->minvalue);
|
2012-06-17 09:40:40 +00:00
|
|
|
packWriteDouble(stream, &level->xoffset);
|
|
|
|
packWriteDouble(stream, &level->yoffset);
|
|
|
|
packWriteDouble(stream, &level->zoffset);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
void noiseLoadGenerator(PackStream* stream, NoiseGenerator* generator)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
packReadInt(stream, &x);
|
|
|
|
generator->function.algorithm = (NoiseFunctionAlgorithm)x;
|
|
|
|
packReadDouble(stream, &generator->function.ridge_factor);
|
2013-01-27 19:57:43 +00:00
|
|
|
packReadDouble(stream, &generator->function.curve_factor);
|
2012-08-27 20:40:35 +00:00
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
packReadDouble(stream, &generator->height_offset);
|
|
|
|
packReadInt(stream, &generator->level_count);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
for (x = 0; x < generator->level_count; x++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-08-26 20:02:10 +00:00
|
|
|
NoiseLevel* level = generator->levels + x;
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
packReadDouble(stream, &level->wavelength);
|
|
|
|
packReadDouble(stream, &level->amplitude);
|
|
|
|
packReadDouble(stream, &level->minvalue);
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &level->xoffset);
|
|
|
|
packReadDouble(stream, &level->yoffset);
|
|
|
|
packReadDouble(stream, &level->zoffset);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-08-26 20:02:10 +00:00
|
|
|
noiseValidate(generator);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination)
|
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
destination->function = source->function;
|
2011-12-10 13:25:22 +00:00
|
|
|
destination->height_offset = source->height_offset;
|
|
|
|
destination->level_count = source->level_count;
|
|
|
|
|
|
|
|
memcpy(destination->levels, source->levels, sizeof(NoiseLevel) * destination->level_count);
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
void noiseValidate(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
int x;
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
if (generator->function.algorithm < 0 || generator->function.algorithm > NOISE_FUNCTION_NAIVE)
|
|
|
|
{
|
|
|
|
generator->function.algorithm = NOISE_FUNCTION_SIMPLEX;
|
|
|
|
}
|
|
|
|
switch (generator->function.algorithm)
|
|
|
|
{
|
|
|
|
case NOISE_FUNCTION_SIMPLEX:
|
|
|
|
generator->_func_noise_1d = noiseSimplexGet1DValue;
|
|
|
|
generator->_func_noise_2d = noiseSimplexGet2DValue;
|
|
|
|
generator->_func_noise_3d = noiseSimplexGet3DValue;
|
|
|
|
break;
|
|
|
|
case NOISE_FUNCTION_PERLIN:
|
2012-08-29 12:01:51 +00:00
|
|
|
generator->_func_noise_1d = noisePerlinGet1DValue;
|
|
|
|
generator->_func_noise_2d = noisePerlinGet2DValue;
|
|
|
|
generator->_func_noise_3d = noisePerlinGet3DValue;
|
2012-08-27 20:40:35 +00:00
|
|
|
break;
|
|
|
|
case NOISE_FUNCTION_NAIVE:
|
2012-08-28 21:13:35 +00:00
|
|
|
generator->_func_noise_1d = noiseNaiveGet1DValue;
|
|
|
|
generator->_func_noise_2d = noiseNaiveGet2DValue;
|
|
|
|
generator->_func_noise_3d = noiseNaiveGet3DValue;
|
2012-08-27 20:40:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-01 16:39:47 +00:00
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
if (generator->function.ridge_factor > 0.5)
|
|
|
|
{
|
|
|
|
generator->function.ridge_factor = 0.5;
|
|
|
|
}
|
|
|
|
if (generator->function.ridge_factor < -0.5)
|
|
|
|
{
|
|
|
|
generator->function.ridge_factor = -0.5;
|
|
|
|
}
|
2013-01-27 19:57:43 +00:00
|
|
|
if (generator->function.curve_factor > 1.0)
|
|
|
|
{
|
|
|
|
generator->function.curve_factor = 1.0;
|
|
|
|
}
|
|
|
|
if (generator->function.curve_factor < -1.0)
|
|
|
|
{
|
|
|
|
generator->function.curve_factor = -1.0;
|
|
|
|
}
|
2012-06-17 16:46:30 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
generator->_min_value = generator->height_offset;
|
|
|
|
generator->_max_value = generator->height_offset;
|
2012-06-17 16:46:30 +00:00
|
|
|
for (x = 0; x < generator->level_count; x++)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
double min_value = generator->levels[x].minvalue;
|
|
|
|
double max_value = min_value + generator->levels[x].amplitude;
|
2012-06-17 16:46:30 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
if (min_value < generator->_min_value)
|
|
|
|
{
|
|
|
|
generator->_min_value = min_value;
|
|
|
|
}
|
|
|
|
if (max_value > generator->_max_value)
|
|
|
|
{
|
|
|
|
generator->_max_value = max_value;
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
void noiseRandomizeOffsets(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_LEVEL_COUNT; i++)
|
|
|
|
{
|
2013-01-30 14:30:06 +00:00
|
|
|
generator->levels[i].xoffset = toolsRandom();
|
|
|
|
generator->levels[i].yoffset = toolsRandom();
|
|
|
|
generator->levels[i].zoffset = toolsRandom();
|
2013-01-30 14:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
NoiseFunction noiseGetFunction(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
return generator->function;
|
|
|
|
}
|
|
|
|
|
|
|
|
void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function)
|
|
|
|
{
|
|
|
|
generator->function = *function;
|
|
|
|
noiseValidate(generator);
|
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor)
|
2012-08-27 20:40:35 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
NoiseFunction function = {algorithm, ridge_factor, curve_factor};
|
2012-08-27 20:40:35 +00:00
|
|
|
noiseSetFunction(generator, &function);
|
|
|
|
}
|
|
|
|
|
2012-07-01 13:27:57 +00:00
|
|
|
void noiseForceValue(NoiseGenerator* generator, double value)
|
|
|
|
{
|
|
|
|
noiseClearLevels(generator);
|
|
|
|
generator->height_offset = value;
|
2013-01-27 19:57:43 +00:00
|
|
|
noiseAddLevelSimple(generator, 1.0, 0.0, 0.0); /* FIXME Should not be needed */
|
2012-07-01 13:27:57 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
void noiseGetRange(NoiseGenerator* generator, double* minvalue, double* maxvalue)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
*minvalue = generator->_min_value;
|
|
|
|
*maxvalue = generator->_max_value;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int noiseGetLevelCount(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
return generator->level_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void noiseClearLevels(NoiseGenerator* generator)
|
|
|
|
{
|
|
|
|
generator->level_count = 0;
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(generator);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
void noiseAddLevel(NoiseGenerator* generator, NoiseLevel level, int protect_offsets)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-02-27 15:42:23 +00:00
|
|
|
if (generator->level_count < MAX_LEVEL_COUNT)
|
|
|
|
{
|
2013-01-30 14:11:59 +00:00
|
|
|
NoiseLevel baselevel = generator->levels[generator->level_count];
|
|
|
|
|
|
|
|
if (protect_offsets)
|
|
|
|
{
|
|
|
|
level.xoffset = baselevel.xoffset;
|
|
|
|
level.yoffset = baselevel.yoffset;
|
|
|
|
level.zoffset = baselevel.zoffset;
|
|
|
|
}
|
|
|
|
|
2012-02-27 15:42:23 +00:00
|
|
|
generator->levels[generator->level_count] = level;
|
|
|
|
generator->level_count++;
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(generator);
|
2012-02-27 15:42:23 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
void noiseAddLevelSimple(NoiseGenerator* generator, double scaling, double minvalue, double maxvalue)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
NoiseLevel level;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
level.wavelength = scaling;
|
|
|
|
level.minvalue = minvalue;
|
|
|
|
level.amplitude = maxvalue - minvalue;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
noiseAddLevel(generator, level, 1);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
void noiseAddLevels(NoiseGenerator* generator, int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < level_count; i++)
|
|
|
|
{
|
2013-01-30 14:11:59 +00:00
|
|
|
noiseAddLevel(generator, start_level, 1);
|
2013-01-27 19:57:43 +00:00
|
|
|
start_level.minvalue += start_level.amplitude * (1.0 - amplitude_factor) * center_factor;
|
|
|
|
start_level.wavelength *= scaling_factor;
|
|
|
|
start_level.amplitude *= amplitude_factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 21:36:49 +00:00
|
|
|
void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double scaling, double minvalue, double maxvalue, double center_factor)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
NoiseLevel level;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
level.wavelength = scaling;
|
|
|
|
level.minvalue = minvalue;
|
|
|
|
level.amplitude = maxvalue - minvalue;
|
2013-01-30 21:36:49 +00:00
|
|
|
noiseAddLevels(generator, level_count, level, 0.5, 0.5, center_factor);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 18:39:42 +00:00
|
|
|
void noiseRemoveLevel(NoiseGenerator* generator, int level)
|
|
|
|
{
|
|
|
|
if (level >= 0 && level < generator->level_count)
|
|
|
|
{
|
|
|
|
if (generator->level_count > 1 && level < generator->level_count - 1)
|
|
|
|
{
|
|
|
|
memmove(generator->levels + level, generator->levels + level + 1, sizeof(NoiseLevel) * (generator->level_count - level - 1));
|
|
|
|
}
|
|
|
|
generator->level_count--;
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(generator);
|
2012-01-22 18:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params)
|
|
|
|
{
|
|
|
|
if (level >= 0 && level < generator->level_count)
|
|
|
|
{
|
|
|
|
*params = generator->levels[level];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
void noiseSetLevel(NoiseGenerator* generator, int index, NoiseLevel level, int protect_offsets)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-30 14:11:59 +00:00
|
|
|
if (index >= 0 && index < generator->level_count)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-30 14:11:59 +00:00
|
|
|
NoiseLevel baselevel = generator->levels[index];
|
|
|
|
|
|
|
|
if (protect_offsets)
|
|
|
|
{
|
|
|
|
level.xoffset = baselevel.xoffset;
|
|
|
|
level.yoffset = baselevel.yoffset;
|
|
|
|
level.zoffset = baselevel.zoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
generator->levels[index] = level;
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(generator);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
void noiseSetLevelSimple(NoiseGenerator* generator, int index, double scaling, double minvalue, double maxvalue)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-30 14:11:59 +00:00
|
|
|
NoiseLevel level;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
level.wavelength = scaling;
|
|
|
|
level.minvalue = minvalue;
|
|
|
|
level.amplitude = maxvalue - minvalue;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2013-01-30 14:11:59 +00:00
|
|
|
noiseSetLevel(generator, index, level, 1);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
void noiseNormalizeAmplitude(NoiseGenerator* generator, double minvalue, double maxvalue, int adjust_scaling)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2013-01-27 19:57:43 +00:00
|
|
|
double current_minvalue, current_maxvalue, current_amplitude;
|
|
|
|
double target_amplitude, factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
if (generator->level_count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
target_amplitude = maxvalue - minvalue;
|
|
|
|
noiseGetRange(generator, ¤t_minvalue, ¤t_maxvalue);
|
|
|
|
current_amplitude = current_maxvalue - current_minvalue;
|
|
|
|
factor = target_amplitude / current_amplitude;
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
generator->levels[level].minvalue = minvalue + (generator->levels[level].minvalue - current_minvalue) * factor;
|
|
|
|
generator->levels[level].amplitude *= factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
if (adjust_scaling)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
generator->levels[level].wavelength *= factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-27 19:57:43 +00:00
|
|
|
/*generator->height_offset = minvalue + (generator->height_offset - current_minvalue) * factor;*/
|
2012-06-17 16:46:30 +00:00
|
|
|
noiseValidate(generator);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 19:57:43 +00:00
|
|
|
static inline double _fixValue(double value, double ridge, double curve)
|
2012-08-27 20:40:35 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
if (value < 0.0)
|
|
|
|
{
|
|
|
|
value = 0.0;
|
|
|
|
}
|
|
|
|
else if (value > 1.0)
|
|
|
|
{
|
|
|
|
value = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curve > 0.0)
|
|
|
|
{
|
|
|
|
value = value * (1.0 - curve) + sqrt(value) * curve;
|
|
|
|
}
|
|
|
|
else if (curve < 0.0)
|
|
|
|
{
|
|
|
|
value = value * (1.0 - curve) + value * value * curve;
|
|
|
|
}
|
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
if (ridge > 0.0)
|
2012-08-30 20:28:55 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
return fabs(value - ridge) / (1.0 - ridge);
|
2012-08-30 20:28:55 +00:00
|
|
|
}
|
|
|
|
else if (ridge < 0.0)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
return 1.0 - fabs(value - 1.0 - ridge) / (1.0 + ridge);
|
2012-08-30 20:28:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
/*if (ridge > 0.0)
|
2012-08-27 20:40:35 +00:00
|
|
|
{
|
|
|
|
return fabs(value + 0.5 - ridge) - 0.5 + ridge;
|
|
|
|
}
|
|
|
|
else if (ridge < 0.0)
|
|
|
|
{
|
|
|
|
return -fabs(value - 0.5 - ridge) + 0.5 + ridge;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return value;
|
2012-08-30 20:28:55 +00:00
|
|
|
}*/
|
2012-08-27 20:40:35 +00:00
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline double _get1DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
return level->minvalue + _fixValue(generator->_func_noise_1d(x / level->wavelength + level->xoffset), generator->function.ridge_factor, generator->function.curve_factor) * level->amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet1DLevel(NoiseGenerator* generator, int level, double x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
if (level >= 0 && level < generator->level_count)
|
|
|
|
{
|
|
|
|
return _get1DLevelValue(generator, generator->levels + level, x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet1DTotal(NoiseGenerator* generator, double x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
|
|
|
result += _get1DLevelValue(generator, generator->levels + level, x);
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet1DDetail(NoiseGenerator* generator, double x, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result, height, factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
height = generator->levels[level].amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
factor = 1.0;
|
|
|
|
if (height < detail * 0.25)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (height < detail * 0.5)
|
|
|
|
{
|
|
|
|
factor = (detail * 0.5 - height) / 0.25;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += _get1DLevelValue(generator, generator->levels + level, x) * factor;
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline double _get2DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x, double y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
return level->minvalue + _fixValue(generator->_func_noise_2d(x / level->wavelength + level->xoffset, y / level->wavelength + level->yoffset), generator->function.ridge_factor, generator->function.curve_factor) * level->amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet2DLevel(NoiseGenerator* generator, int level, double x, double y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
if (level >= 0 && level < generator->level_count)
|
|
|
|
{
|
|
|
|
return _get2DLevelValue(generator, generator->levels + level, x, y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet2DTotal(NoiseGenerator* generator, double x, double y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
|
|
|
result += _get2DLevelValue(generator, generator->levels + level, x, y);
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet2DDetail(NoiseGenerator* generator, double x, double y, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result, height, factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
height = generator->levels[level].amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
factor = 1.0;
|
|
|
|
if (height < detail * 0.25)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (height < detail * 0.5)
|
|
|
|
{
|
|
|
|
factor = (detail * 0.5 - height) / 0.25;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += _get2DLevelValue(generator, generator->levels + level, x, y) * factor;
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline double _get3DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x, double y, double z)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
return level->minvalue + _fixValue(generator->_func_noise_3d(x / level->wavelength + level->xoffset, y / level->wavelength + level->yoffset, z / level->wavelength + level->zoffset), generator->function.ridge_factor, generator->function.curve_factor) * level->amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet3DLevel(NoiseGenerator* generator, int level, double x, double y, double z)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
if (level >= 0 && level < generator->level_count)
|
|
|
|
{
|
|
|
|
return _get3DLevelValue(generator, generator->levels + level, x, y, z);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet3DTotal(NoiseGenerator* generator, double x, double y, double z)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
|
|
|
result += _get3DLevelValue(generator, generator->levels + level, x, y, z);
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double noiseGet3DDetail(NoiseGenerator* generator, double x, double y, double z, double detail)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int level;
|
2012-06-17 09:40:40 +00:00
|
|
|
double result, height, factor;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
result = 0.0;
|
|
|
|
for (level = 0; level < generator->level_count; level++)
|
|
|
|
{
|
2013-01-27 19:57:43 +00:00
|
|
|
height = generator->levels[level].amplitude;
|
2011-12-10 13:25:22 +00:00
|
|
|
factor = 1.0;
|
|
|
|
if (height < detail * 0.25)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (height < detail * 0.5)
|
|
|
|
{
|
|
|
|
factor = (detail * 0.5 - height) / 0.25;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += _get3DLevelValue(generator, generator->levels + level, x, y, z) * factor;
|
|
|
|
}
|
|
|
|
return result + generator->height_offset;
|
|
|
|
}
|