Made a class of NoiseGenerator

This commit is contained in:
Michaël Lemaire 2013-11-03 15:46:39 +01:00
parent 1a69b1de1d
commit b5c50d16d4
55 changed files with 1333 additions and 1225 deletions

View file

@ -0,0 +1,5 @@
#include "Interpolation.h"
Interpolation::Interpolation()
{
}

View file

@ -0,0 +1,23 @@
#ifndef INTERPOLATION_H
#define INTERPOLATION_H
#include "basics_global.h"
namespace paysages {
namespace basics {
class Interpolation
{
public:
Interpolation();
static inline double cubic(double p[4], 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])));
}
};
}
}
#endif // INTERPOLATION_H

View file

@ -1,17 +1,23 @@
#include "noisenaive.h" #include "NoiseFunctionNaive.h"
/* /*
* Naive noise implementation, based on pseudo-random grids. * Naive noise implementation, based on pseudo-random grids.
*/ */
#include <stdlib.h> #include <cstdlib>
#include <math.h> #include <cmath>
#include <string.h> #include <cstring>
#include "tools.h" #include "Interpolation.h"
#include "RandomGenerator.h"
#include "PackStream.h"
static int _noise_pool_size; static int _noise_pool_size;
static double* _noise_pool; static double* _noise_pool;
NoiseFunctionNaive::NoiseFunctionNaive()
{
}
void noiseNaiveInit() void noiseNaiveInit()
{ {
int i; int i;
@ -21,7 +27,7 @@ void noiseNaiveInit()
for (i = 0; i < _noise_pool_size; i++) for (i = 0; i < _noise_pool_size; i++)
{ {
_noise_pool[i] = toolsRandom() - 0.5; _noise_pool[i] = RandomGenerator::random() - 0.5;
} }
} }
@ -91,7 +97,7 @@ double noiseNaiveGet1DValue(double x)
buf_cubic_x[2] = _noise_pool[x2 % _noise_pool_size]; buf_cubic_x[2] = _noise_pool[x2 % _noise_pool_size];
buf_cubic_x[3] = _noise_pool[x3 % _noise_pool_size]; buf_cubic_x[3] = _noise_pool[x3 % _noise_pool_size];
return toolsCubicInterpolate(buf_cubic_x, xinternal) * 0.837 + 0.5; return Interpolation::cubic(buf_cubic_x, xinternal) * 0.837 + 0.5;
} }
double noiseNaiveGet2DValue(double x, double y) double noiseNaiveGet2DValue(double x, double y)
@ -156,27 +162,27 @@ double noiseNaiveGet2DValue(double x, double y)
buf_cubic_x[1] = _noise_pool[(y0 * size + x1) % _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[2] = _noise_pool[(y0 * size + x2) % _noise_pool_size];
buf_cubic_x[3] = _noise_pool[(y0 * size + x3) % _noise_pool_size]; buf_cubic_x[3] = _noise_pool[(y0 * size + x3) % _noise_pool_size];
buf_cubic_y[0] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[0] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y1 * size + x0) % _noise_pool_size]; 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[1] = _noise_pool[(y1 * size + x1) % _noise_pool_size];
buf_cubic_x[2] = _noise_pool[(y1 * size + x2) % _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_x[3] = _noise_pool[(y1 * size + x3) % _noise_pool_size];
buf_cubic_y[1] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[1] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y2 * size + x0) % _noise_pool_size]; 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[1] = _noise_pool[(y2 * size + x1) % _noise_pool_size];
buf_cubic_x[2] = _noise_pool[(y2 * size + x2) % _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_x[3] = _noise_pool[(y2 * size + x3) % _noise_pool_size];
buf_cubic_y[2] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[2] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y3 * size + x0) % _noise_pool_size]; 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[1] = _noise_pool[(y3 * size + x1) % _noise_pool_size];
buf_cubic_x[2] = _noise_pool[(y3 * size + x2) % _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_x[3] = _noise_pool[(y3 * size + x3) % _noise_pool_size];
buf_cubic_y[3] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[3] = Interpolation::cubic(buf_cubic_x, xinternal);
return toolsCubicInterpolate(buf_cubic_y, yinternal) * 0.723 + 0.5; return Interpolation::cubic(buf_cubic_y, yinternal) * 0.723 + 0.5;
} }
double noiseNaiveGet3DValue(double x, double y, double z) double noiseNaiveGet3DValue(double x, double y, double z)
@ -266,107 +272,107 @@ double noiseNaiveGet3DValue(double x, double y, double z)
buf_cubic_x[1] = _noise_pool[(y0 * size * size + x1 * 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[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_x[3] = _noise_pool[(y0 * size * size + x3 * size + z0) % _noise_pool_size];
buf_cubic_y[0] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[0] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z0) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y1 * size * size + x3 * size + z0) % _noise_pool_size];
buf_cubic_y[1] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[1] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z0) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y2 * size * size + x3 * size + z0) % _noise_pool_size];
buf_cubic_y[2] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[2] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z0) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y3 * size * size + x3 * size + z0) % _noise_pool_size];
buf_cubic_y[3] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[3] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_z[0] = toolsCubicInterpolate(buf_cubic_y, yinternal); buf_cubic_z[0] = Interpolation::cubic(buf_cubic_y, yinternal);
buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z1) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y0 * size * size + x3 * size + z1) % _noise_pool_size];
buf_cubic_y[0] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[0] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z1) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y1 * size * size + x3 * size + z1) % _noise_pool_size];
buf_cubic_y[1] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[1] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z1) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y2 * size * size + x3 * size + z1) % _noise_pool_size];
buf_cubic_y[2] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[2] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z1) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y3 * size * size + x3 * size + z1) % _noise_pool_size];
buf_cubic_y[3] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[3] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_z[1] = toolsCubicInterpolate(buf_cubic_y, yinternal); buf_cubic_z[1] = Interpolation::cubic(buf_cubic_y, yinternal);
buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z2) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y0 * size * size + x3 * size + z2) % _noise_pool_size];
buf_cubic_y[0] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[0] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z2) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y1 * size * size + x3 * size + z2) % _noise_pool_size];
buf_cubic_y[1] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[1] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z2) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y2 * size * size + x3 * size + z2) % _noise_pool_size];
buf_cubic_y[2] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[2] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z2) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y3 * size * size + x3 * size + z2) % _noise_pool_size];
buf_cubic_y[3] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[3] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_z[2] = toolsCubicInterpolate(buf_cubic_y, yinternal); buf_cubic_z[2] = Interpolation::cubic(buf_cubic_y, yinternal);
buf_cubic_x[0] = _noise_pool[(y0 * size * size + x0 * size + z3) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y0 * size * size + x3 * size + z3) % _noise_pool_size];
buf_cubic_y[0] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[0] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y1 * size * size + x0 * size + z3) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y1 * size * size + x3 * size + z3) % _noise_pool_size];
buf_cubic_y[1] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[1] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y2 * size * size + x0 * size + z3) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y2 * size * size + x3 * size + z3) % _noise_pool_size];
buf_cubic_y[2] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[2] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_x[0] = _noise_pool[(y3 * size * size + x0 * size + z3) % _noise_pool_size]; 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[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[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_x[3] = _noise_pool[(y3 * size * size + x3 * size + z3) % _noise_pool_size];
buf_cubic_y[3] = toolsCubicInterpolate(buf_cubic_x, xinternal); buf_cubic_y[3] = Interpolation::cubic(buf_cubic_x, xinternal);
buf_cubic_z[3] = toolsCubicInterpolate(buf_cubic_y, yinternal); buf_cubic_z[3] = Interpolation::cubic(buf_cubic_y, yinternal);
return toolsCubicInterpolate(buf_cubic_z, zinternal) * 0.794 + 0.5; return Interpolation::cubic(buf_cubic_z, zinternal) * 0.794 + 0.5;
} }
/*double noiseNaiveGet4DValue(double x, double y, double z, double w) /*double noiseNaiveGet4DValue(double x, double y, double z, double w)

View file

@ -0,0 +1,30 @@
#ifndef NOISEFUNCTIONNAIVE_H
#define NOISEFUNCTIONNAIVE_H
#include "basics_global.h"
namespace paysages {
namespace system {
class PackStream;
}
namespace basics {
class NoiseFunctionNaive
{
public:
NoiseFunctionNaive();
};
}
}
void noiseNaiveInit();
void noiseNaiveQuit();
void noiseNaiveSave(PackStream* stream);
void noiseNaiveLoad(PackStream* stream);
double noiseNaiveGet1DValue(double x);
double noiseNaiveGet2DValue(double x, double y);
double noiseNaiveGet3DValue(double x, double y, double z);
/*double noiseNaiveGet4DValue(double x, double y, double z, double w);*/
#endif // NOISEFUNCTIONNAIVE_H

View file

@ -0,0 +1,188 @@
#include "NoiseFunctionPerlin.h"
/*
* Perlin noise implementation.
*
* Based on Ken Perlin implementation.
*/
#include <cstdlib>
#include <cmath>
NoiseFunctionPerlin::NoiseFunctionPerlin()
{
}
#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*2.0};
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) * 1.068 + 0.5;
}
double noisePerlinGet2DValue(double x, double y)
{
double vec[2] = {x*2.0, y*2.0};
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) * 0.709 + 0.5;
}
double noisePerlinGet3DValue(double x, double y, double z)
{
double vec[3] = {x*2.0, y*2.0, z*2.0};
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) * 0.661 + 0.5;
}
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)((rand() % (B + B)) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (double)((rand() % (B + B)) - B) / B;
_normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (double)((rand() % (B + B)) - B) / B;
_normalize3(g3[i]);
}
while (--i) {
k = p[i];
p[i] = p[j = rand() % 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];
}
}

View file

@ -0,0 +1,27 @@
#ifndef NOISEFUNCTIONPERLIN_H
#define NOISEFUNCTIONPERLIN_H
#include "basics_global.h"
namespace paysages {
namespace system {
class PackStream;
}
namespace basics {
class NoiseFunctionPerlin
{
public:
NoiseFunctionPerlin();
};
}
}
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);*/
#endif // NOISEFUNCTIONPERLIN_H

View file

@ -1,4 +1,4 @@
#include "noisesimplex.h" #include "NoiseFunctionSimplex.h"
/* /*
* Simplex noise implementation. * Simplex noise implementation.
@ -6,9 +6,13 @@
* Based on Stefan Gustavson implementation. * Based on Stefan Gustavson implementation.
*/ */
#include <stdlib.h> #include <cstdlib>
#include <math.h> #include <cmath>
#include <string.h> #include <cstring>
NoiseFunctionSimplex::NoiseFunctionSimplex()
{
}
typedef struct typedef struct
{ {

View file

@ -0,0 +1,27 @@
#ifndef NOISEFUNCTIONSIMPLEX_H
#define NOISEFUNCTIONSIMPLEX_H
#include "basics_global.h"
namespace paysages {
namespace system {
class PackStream;
}
namespace basics {
class NoiseFunctionSimplex
{
public:
NoiseFunctionSimplex();
};
}
}
void noiseSimplexInit();
double noiseSimplexGet1DValue(double x);
double noiseSimplexGet2DValue(double x, double y);
double noiseSimplexGet3DValue(double x, double y, double z);
double noiseSimplexGet4DValue(double x, double y, double z, double w);
#endif // NOISEFUNCTIONSIMPLEX_H

View file

@ -0,0 +1,597 @@
#include "NoiseGenerator.h"
#include "NoiseFunctionNaive.h"
#include "NoiseFunctionPerlin.h"
#include "NoiseFunctionSimplex.h"
#include "PackStream.h"
#include "RandomGenerator.h"
#include <cmath>
#define MAX_LEVEL_COUNT 30
void noiseInit()
{
noiseSimplexInit();
noisePerlinInit();
noiseNaiveInit();
/* Noise stats */
/*NoiseGenerator* noise;
int x;
double min, max, value;
noise = noiseCreateGenerator();
noiseSetFunctionParams(noise, NOISE_FUNCTION_NAIVE, 0.0);
noiseAddLevelSimple(noise, 1.0, 0.0, 1.0);
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);*/
}
void noiseQuit()
{
noiseNaiveQuit();
}
void noiseSave(PackStream* stream)
{
noiseNaiveSave(stream);
}
void noiseLoad(PackStream* stream)
{
noiseNaiveLoad(stream);
}
NoiseGenerator::NoiseGenerator()
{
function.algorithm = NOISE_FUNCTION_SIMPLEX;
function.ridge_factor = 0.0;
function.curve_factor = 0.0;
level_count = 0;
height_offset = 0.0;
randomizeOffsets();
validate();
}
NoiseGenerator::~NoiseGenerator()
{
}
void NoiseGenerator::save(PackStream* stream)
{
int x;
x = (int)function.algorithm;
stream->write(&x);
stream->write(&function.ridge_factor);
stream->write(&function.curve_factor);
stream->write(&height_offset);
stream->write(&level_count);
for (x = 0; x < level_count; x++)
{
NoiseLevel* level = levels + x;
stream->write(&level->wavelength);
stream->write(&level->amplitude);
stream->write(&level->minvalue);
stream->write(&level->xoffset);
stream->write(&level->yoffset);
stream->write(&level->zoffset);
}
}
void NoiseGenerator::load(PackStream* stream)
{
int x;
stream->read(&x);
function.algorithm = (NoiseFunctionAlgorithm)x;
stream->read(&function.ridge_factor);
stream->read(&function.curve_factor);
stream->read(&height_offset);
stream->read(&level_count);
for (x = 0; x < level_count; x++)
{
NoiseLevel* level = levels + x;
stream->read(&level->wavelength);
stream->read(&level->amplitude);
stream->read(&level->minvalue);
stream->read(&level->xoffset);
stream->read(&level->yoffset);
stream->read(&level->zoffset);
}
validate();
}
void NoiseGenerator::copy(NoiseGenerator* destination)
{
destination->function = function;
destination->height_offset = height_offset;
destination->level_count = level_count;
memcpy(destination->levels, levels, sizeof(NoiseLevel) * destination->level_count);
validate();
}
void NoiseGenerator::validate()
{
int x;
if (function.algorithm < 0 || function.algorithm > NOISE_FUNCTION_NAIVE)
{
function.algorithm = NOISE_FUNCTION_SIMPLEX;
}
switch (function.algorithm)
{
case NOISE_FUNCTION_SIMPLEX:
_func_noise_1d = noiseSimplexGet1DValue;
_func_noise_2d = noiseSimplexGet2DValue;
_func_noise_3d = noiseSimplexGet3DValue;
break;
case NOISE_FUNCTION_PERLIN:
_func_noise_1d = noisePerlinGet1DValue;
_func_noise_2d = noisePerlinGet2DValue;
_func_noise_3d = noisePerlinGet3DValue;
break;
case NOISE_FUNCTION_NAIVE:
_func_noise_1d = noiseNaiveGet1DValue;
_func_noise_2d = noiseNaiveGet2DValue;
_func_noise_3d = noiseNaiveGet3DValue;
break;
}
if (function.ridge_factor > 0.5)
{
function.ridge_factor = 0.5;
}
if (function.ridge_factor < -0.5)
{
function.ridge_factor = -0.5;
}
if (function.curve_factor > 1.0)
{
function.curve_factor = 1.0;
}
if (function.curve_factor < -1.0)
{
function.curve_factor = -1.0;
}
_min_value = height_offset;
_max_value = height_offset;
for (x = 0; x < level_count; x++)
{
_min_value += levels[x].minvalue;
_max_value += levels[x].minvalue + levels[x].amplitude;
}
}
void NoiseGenerator::randomizeOffsets()
{
int i;
for (i = 0; i < MAX_LEVEL_COUNT; i++)
{
levels[i].xoffset = RandomGenerator::random();
levels[i].yoffset = RandomGenerator::random();
levels[i].zoffset = RandomGenerator::random();
}
}
NoiseFunction NoiseGenerator::getFunction()
{
return function;
}
void NoiseGenerator::setFunction(NoiseFunction* function)
{
this->function = *function;
validate();
}
void NoiseGenerator::setCustomFunction(double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z))
{
this->_func_noise_1d = func1d;
this->_func_noise_2d = func2d;
this->_func_noise_3d = func3d;
}
void NoiseGenerator::setFunctionParams(NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor)
{
NoiseFunction function = {algorithm, ridge_factor, curve_factor};
setFunction(&function);
}
void NoiseGenerator::forceValue(double value)
{
clearLevels();
height_offset = value;
addLevelSimple(1.0, 0.0, 0.0); /* FIXME Should not be needed */
}
void NoiseGenerator::getRange(double* minvalue, double* maxvalue)
{
*minvalue = _min_value;
*maxvalue = _max_value;
}
int NoiseGenerator::getLevelCount()
{
return level_count;
}
void NoiseGenerator::clearLevels()
{
level_count = 0;
validate();
}
void NoiseGenerator::addLevel(NoiseLevel level, int protect_offsets)
{
if (level_count < MAX_LEVEL_COUNT)
{
NoiseLevel baselevel = levels[level_count];
if (protect_offsets)
{
level.xoffset = baselevel.xoffset;
level.yoffset = baselevel.yoffset;
level.zoffset = baselevel.zoffset;
}
levels[level_count] = level;
level_count++;
validate();
}
}
void NoiseGenerator::addLevelSimple(double scaling, double minvalue, double maxvalue)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
addLevel(level, 1);
}
void NoiseGenerator::addLevels(int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor)
{
int i;
for (i = 0; i < level_count; i++)
{
addLevel(start_level, 1);
start_level.minvalue += start_level.amplitude * (1.0 - amplitude_factor) * center_factor;
start_level.wavelength *= scaling_factor;
start_level.amplitude *= amplitude_factor;
}
}
void NoiseGenerator::addLevelsSimple(int level_count, double scaling, double minvalue, double maxvalue, double center_factor)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
addLevels(level_count, level, 0.5, 0.5, center_factor);
}
void NoiseGenerator::removeLevel(int level)
{
if (level >= 0 && level < level_count)
{
if (level_count > 1 && level < level_count - 1)
{
memmove(levels + level, levels + level + 1, sizeof(NoiseLevel) * (level_count - level - 1));
}
level_count--;
validate();
}
}
int NoiseGenerator::getLevel(int level, NoiseLevel* params)
{
if (level >= 0 && level < level_count)
{
*params = levels[level];
return 1;
}
else
{
return 0;
}
}
void NoiseGenerator::setLevel(int index, NoiseLevel level, int protect_offsets)
{
if (index >= 0 && index < level_count)
{
NoiseLevel baselevel = levels[index];
if (protect_offsets)
{
level.xoffset = baselevel.xoffset;
level.yoffset = baselevel.yoffset;
level.zoffset = baselevel.zoffset;
}
levels[index] = level;
validate();
}
}
void NoiseGenerator::setLevelSimple(int index, double scaling, double minvalue, double maxvalue)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
setLevel(index, level, 1);
}
void NoiseGenerator::normalizeAmplitude(double minvalue, double maxvalue, int adjust_scaling)
{
int level;
double current_minvalue, current_maxvalue, current_amplitude;
double target_amplitude, factor;
if (level_count == 0)
{
return;
}
target_amplitude = maxvalue - minvalue;
getRange(&current_minvalue, &current_maxvalue);
current_amplitude = current_maxvalue - current_minvalue;
factor = target_amplitude / current_amplitude;
for (level = 0; level < level_count; level++)
{
levels[level].minvalue *= factor;
levels[level].amplitude *= factor;
if (adjust_scaling)
{
levels[level].wavelength *= factor;
}
}
height_offset = minvalue + (height_offset - current_minvalue) * factor;
validate();
}
static inline double _fixValue(double value, double ridge, double curve)
{
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;
}
if (ridge > 0.0)
{
return fabs(value - ridge) / (1.0 - ridge);
}
else if (ridge < 0.0)
{
return 1.0 - fabs(value - 1.0 - ridge) / (1.0 + ridge);
}
else
{
return value;
}
/*if (ridge > 0.0)
{
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;
}*/
}
inline double NoiseGenerator::_get1DLevelValue(NoiseLevel* level, double x)
{
return level->minvalue + _fixValue(_func_noise_1d(x / level->wavelength + level->xoffset), function.ridge_factor, function.curve_factor) * level->amplitude;
}
double NoiseGenerator::get1DLevel(int level, double x)
{
if (level >= 0 && level < level_count)
{
return _get1DLevelValue(levels + level, x);
}
else
{
return 0.0;
}
}
double NoiseGenerator::get1DTotal(double x)
{
int level;
double result;
result = 0.0;
for (level = 0; level < level_count; level++)
{
result += _get1DLevelValue(levels + level, x);
}
return result + height_offset;
}
double NoiseGenerator::get1DDetail(double x, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < level_count; level++)
{
height = levels[level].amplitude;
factor = 1.0;
if (height < detail * 0.25)
{
break;
}
else if (height < detail * 0.5)
{
factor = (detail * 0.5 - height) / 0.25;
}
result += _get1DLevelValue(levels + level, x) * factor;
}
return result + height_offset;
}
inline double NoiseGenerator::_get2DLevelValue(NoiseLevel* level, double x, double y)
{
return level->minvalue + _fixValue(_func_noise_2d(x / level->wavelength + level->xoffset, y / level->wavelength + level->yoffset), function.ridge_factor, function.curve_factor) * level->amplitude;
}
double NoiseGenerator::get2DLevel(int level, double x, double y)
{
if (level >= 0 && level < level_count)
{
return _get2DLevelValue(levels + level, x, y);
}
else
{
return 0.0;
}
}
double NoiseGenerator::get2DTotal(double x, double y)
{
int level;
double result;
result = 0.0;
for (level = 0; level < level_count; level++)
{
result += _get2DLevelValue(levels + level, x, y);
}
return result + height_offset;
}
double NoiseGenerator::get2DDetail(double x, double y, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < level_count; level++)
{
height = levels[level].amplitude;
factor = 1.0;
if (height < detail * 0.25)
{
break;
}
else if (height < detail * 0.5)
{
factor = (detail * 0.5 - height) / 0.25;
}
result += _get2DLevelValue(levels + level, x, y) * factor;
}
return result + height_offset;
}
inline double NoiseGenerator::_get3DLevelValue(NoiseLevel* level, double x, double y, double z)
{
return level->minvalue + _fixValue(_func_noise_3d(x / level->wavelength + level->xoffset, y / level->wavelength + level->yoffset, z / level->wavelength + level->zoffset), function.ridge_factor, function.curve_factor) * level->amplitude;
}
double NoiseGenerator::get3DLevel(int level, double x, double y, double z)
{
if (level >= 0 && level < level_count)
{
return _get3DLevelValue(levels + level, x, y, z);
}
else
{
return 0.0;
}
}
double NoiseGenerator::get3DTotal(double x, double y, double z)
{
int level;
double result;
result = 0.0;
for (level = 0; level < level_count; level++)
{
result += _get3DLevelValue(levels + level, x, y, z);
}
return result + height_offset;
}
double NoiseGenerator::get3DDetail(double x, double y, double z, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < level_count; level++)
{
height = levels[level].amplitude;
factor = 1.0;
if (height < detail * 0.25)
{
break;
}
else if (height < detail * 0.5)
{
factor = (detail * 0.5 - height) / 0.25;
}
result += _get3DLevelValue(levels + level, x, y, z) * factor;
}
return result + height_offset;
}

100
src/basics/NoiseGenerator.h Normal file
View file

@ -0,0 +1,100 @@
#ifndef NOISEGENERATOR_H
#define NOISEGENERATOR_H
#include "basics_global.h"
#define MAX_LEVEL_COUNT 30
typedef enum
{
NOISE_FUNCTION_SIMPLEX,
NOISE_FUNCTION_PERLIN,
NOISE_FUNCTION_NAIVE
} NoiseFunctionAlgorithm;
typedef struct
{
NoiseFunctionAlgorithm algorithm;
double ridge_factor; /* -0.5;0.5 */
double curve_factor; /* -1.0;1.0 */
} NoiseFunction;
typedef struct
{
double wavelength;
double amplitude;
double minvalue;
double xoffset;
double yoffset;
double zoffset;
} NoiseLevel;
namespace paysages {
namespace system {class PackStream;}
namespace basics {
class BASICSSHARED_EXPORT NoiseGenerator
{
public:
NoiseGenerator();
virtual ~NoiseGenerator();
virtual void save(PackStream* stream);
virtual void load(PackStream* stream);
virtual void copy(NoiseGenerator* destination);
virtual void validate();
void randomizeOffsets();
NoiseFunction getFunction();
void setCustomFunction(double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z));
void setFunction(NoiseFunction* function);
void setFunctionParams(NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor);
void forceValue(double value);
void getRange(double* minvalue, double* maxvalue);
int getLevelCount();
void clearLevels();
void addLevel(NoiseLevel level, int protect_offsets);
void addLevelSimple(double scaling, double minvalue, double maxvalue);
void addLevels(int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor);
void addLevelsSimple(int level_count, double scaling, double minvalue, double maxvalue, double center_factor);
void removeLevel(int level);
int getLevel(int level, NoiseLevel* params);
void setLevel(int index, NoiseLevel level, int protect_offsets);
void setLevelSimple(int index, double scaling, double minvalue, double maxvalue);
void normalizeAmplitude(double minvalue, double maxvalue, int adjust_scaling);
double get1DLevel(int level, double x);
double get1DTotal(double x);
double get1DDetail(double x, double detail);
double get2DLevel(int level, double x, double y);
double get2DTotal(double x, double y);
double get2DDetail(double x, double y, double detail);
double get3DLevel(int level, double x, double y, double z);
double get3DTotal(double x, double y, double z);
double get3DDetail(double x, double y, double z, double detail);
private:
double _get1DLevelValue(NoiseLevel* level, double x);
double _get2DLevelValue(NoiseLevel* level, double x, double y);
double _get3DLevelValue(NoiseLevel* level, double x, double y, double z);
NoiseFunction function;
double height_offset;
int level_count;
NoiseLevel levels[MAX_LEVEL_COUNT];
double _min_value;
double _max_value;
double (*_func_noise_1d)(double x);
double (*_func_noise_2d)(double x, double y);
double (*_func_noise_3d)(double x, double y, double z);
};
}
}
BASICSSHARED_EXPORT void noiseInit();
BASICSSHARED_EXPORT void noiseQuit();
BASICSSHARED_EXPORT void noiseSave(PackStream* stream);
BASICSSHARED_EXPORT void noiseLoad(PackStream* stream);
#endif // NOISEGENERATOR_H

View file

@ -12,11 +12,21 @@ TEMPLATE = lib
DEFINES += BASICS_LIBRARY DEFINES += BASICS_LIBRARY
SOURCES += \ SOURCES += \
ColorRGB.cpp ColorRGB.cpp \
NoiseGenerator.cpp \
NoiseFunctionNaive.cpp \
NoiseFunctionPerlin.cpp \
NoiseFunctionSimplex.cpp \
Interpolation.cpp
HEADERS +=\ HEADERS +=\
basics_global.h \ basics_global.h \
ColorRGB.h ColorRGB.h \
NoiseGenerator.h \
NoiseFunctionNaive.h \
NoiseFunctionPerlin.h \
NoiseFunctionSimplex.h \
Interpolation.h
unix:!symbian { unix:!symbian {
maemo5 { maemo5 {
@ -26,3 +36,9 @@ unix:!symbian {
} }
INSTALLS += target INSTALLS += target
} }
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../system/release/ -lpaysages_system
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../system/debug/ -lpaysages_system
else:unix: LIBS += -L$$OUT_PWD/../system/ -lpaysages_system
INCLUDEPATH += $$PWD/../system
DEPENDPATH += $$PWD/../system

View file

@ -2,17 +2,7 @@
#define BASICS_GLOBAL_H #define BASICS_GLOBAL_H
/* Shared object helpers */ /* Shared object helpers */
#ifdef __cplusplus #include <QtCore/qglobal.h>
# include <QtCore/qglobal.h>
#else
# if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# define Q_DECL_EXPORT __declspec(dllexport)
# define Q_DECL_IMPORT __declspec(dllimport)
# else
# define Q_DECL_EXPORT
# define Q_DECL_IMPORT
# endif
#endif
#if defined(DEFINITION_LIBRARY) #if defined(DEFINITION_LIBRARY)
# define BASICSSHARED_EXPORT Q_DECL_EXPORT # define BASICSSHARED_EXPORT Q_DECL_EXPORT
#else #else
@ -20,14 +10,12 @@
#endif #endif
/* Namespace using */ /* Namespace using */
#ifdef __cplusplus
namespace paysages namespace paysages
{ {
namespace system {}
namespace basics {} namespace basics {}
} }
using namespace paysages::system; using namespace paysages::system;
#endif using namespace paysages::basics;
/* Global imports */
#endif // BASICS_GLOBAL_H #endif // BASICS_GLOBAL_H

View file

@ -9,14 +9,23 @@
#include "rendering/shared/types.h" #include "rendering/shared/types.h"
#include "rendering/tools/curve.h" #include "rendering/tools/curve.h"
#include "rendering/tools/color.h" #include "rendering/tools/color.h"
#include "PackStream.h"
#include "rendering/tools/lighting.h" #include "rendering/tools/lighting.h"
#include "rendering/noise.h"
#include "Layers.h"
class QPushButton; class QPushButton;
class QComboBox; class QComboBox;
namespace paysages {
namespace system {
class PackStream;
}
namespace basics {
class NoiseGenerator;
}
namespace definition {
class Layers;
}
}
class BaseForm : public QWidget class BaseForm : public QWidget
{ {
Q_OBJECT Q_OBJECT

View file

@ -19,7 +19,7 @@ public:
PreviewLevel(QWidget* parent, NoiseGenerator* noise): BasePreview(parent) PreviewLevel(QWidget* parent, NoiseGenerator* noise): BasePreview(parent)
{ {
_noise_original = noise; _noise_original = noise;
_noise_preview = noiseCreateGenerator(); _noise_preview = new NoiseGenerator();
_level = -1; _level = -1;
configScaling(0.15, 6.0, 0.09, 6.0); configScaling(0.15, 6.0, 0.09, 6.0);
@ -33,11 +33,11 @@ public:
protected: protected:
void updateData() void updateData()
{ {
noiseCopy(_noise_original, _noise_preview); _noise_original->copy(_noise_preview);
} }
Color getColor(double x, double y) Color getColor(double x, double y)
{ {
if ((_level >= 0) && (-y > noiseGet1DLevel(_noise_preview, _level, x))) if ((_level >= 0) && (-y > _noise_preview->get1DLevel(_level, x)))
{ {
return COLOR_WHITE; return COLOR_WHITE;
} }
@ -58,18 +58,18 @@ public:
PreviewTotal(QWidget* parent, NoiseGenerator* noise): BasePreview(parent) PreviewTotal(QWidget* parent, NoiseGenerator* noise): BasePreview(parent)
{ {
_noise_original = noise; _noise_original = noise;
_noise_preview = noiseCreateGenerator(); _noise_preview = new NoiseGenerator();
configScaling(0.15, 6.0, 0.09, 6.0); configScaling(0.15, 6.0, 0.09, 6.0);
} }
protected: protected:
void updateData() void updateData()
{ {
noiseCopy(_noise_original, _noise_preview); _noise_original->copy(_noise_preview);
} }
Color getColor(double x, double y) Color getColor(double x, double y)
{ {
if (-y > noiseGet1DTotal(_noise_preview, x)) if (-y > _noise_preview->get1DTotal(x))
{ {
return COLOR_WHITE; return COLOR_WHITE;
} }
@ -95,7 +95,7 @@ DialogNoise::DialogNoise(QWidget *parent, NoiseGenerator* value):
QLabel* label; QLabel* label;
_base = value; _base = value;
_current = noiseCreateGenerator(); _current = new NoiseGenerator();
setLayout(new QHBoxLayout()); setLayout(new QHBoxLayout());
@ -208,7 +208,7 @@ DialogNoise::~DialogNoise()
delete previewLevel; delete previewLevel;
delete previewTotal; delete previewTotal;
noiseDeleteGenerator(_current); delete _current;
} }
bool DialogNoise::getNoise(QWidget* parent, NoiseGenerator* noise) bool DialogNoise::getNoise(QWidget* parent, NoiseGenerator* noise)
@ -230,13 +230,13 @@ void DialogNoise::closeEvent(QCloseEvent*)
void DialogNoise::accept() void DialogNoise::accept()
{ {
noiseCopy(_current, _base); _current->copy(_base);
QDialog::accept(); QDialog::accept();
} }
void DialogNoise::revert() void DialogNoise::revert()
{ {
noiseCopy(_base, _current); _base->copy(_current);
revertToCurrent(); revertToCurrent();
} }
@ -250,7 +250,7 @@ void DialogNoise::revertToCurrent()
selected = levels->currentRow(); selected = levels->currentRow();
levels->clear(); levels->clear();
n = noiseGetLevelCount(_current); n = _current->getLevelCount();
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
levels->addItem(QString(tr("Component %1")).arg(i + 1)); levels->addItem(QString(tr("Component %1")).arg(i + 1));
@ -269,7 +269,7 @@ void DialogNoise::revertToCurrent()
levels->setCurrentRow(selected); levels->setCurrentRow(selected);
} }
function = noiseGetFunction(_current); function = _current->getFunction();
function_algo->setCurrentIndex((int)function.algorithm); function_algo->setCurrentIndex((int)function.algorithm);
function_ridge->setValue(round(function.ridge_factor * 20.0)); function_ridge->setValue(round(function.ridge_factor * 20.0));
@ -284,7 +284,7 @@ void DialogNoise::addLevel()
level.amplitude = 0.1; level.amplitude = 0.1;
level.wavelength = 0.1; level.wavelength = 0.1;
noiseAddLevel(_current, level, 1); _current->addLevel(level, 1);
revertToCurrent(); revertToCurrent();
@ -297,7 +297,7 @@ void DialogNoise::removeLevel()
row = levels->currentRow(); row = levels->currentRow();
noiseRemoveLevel(_current, _current_level); _current->removeLevel(_current_level);
revertToCurrent(); revertToCurrent();
@ -315,8 +315,8 @@ void DialogNoise::functionChanged()
function.algorithm = (NoiseFunctionAlgorithm)function_algo->currentIndex(); function.algorithm = (NoiseFunctionAlgorithm)function_algo->currentIndex();
function.ridge_factor = (double)function_ridge->value() * 0.05; function.ridge_factor = (double)function_ridge->value() * 0.05;
noiseSetFunction(_current, &function); _current->setFunction(&function);
noiseValidate(_current); _current->validate();
previewLevel->redraw(); previewLevel->redraw();
previewTotal->redraw(); previewTotal->redraw();
@ -324,7 +324,7 @@ void DialogNoise::functionChanged()
void DialogNoise::levelChanged(int row) void DialogNoise::levelChanged(int row)
{ {
if (noiseGetLevel(_current, row, &_current_level_params)) if (_current->getLevel(row, &_current_level_params))
{ {
_current_level = row; _current_level = row;
((PreviewLevel*)previewLevel)->setLevel(row); ((PreviewLevel*)previewLevel)->setLevel(row);
@ -338,7 +338,7 @@ void DialogNoise::levelChanged(int row)
void DialogNoise::heightChanged(int value) void DialogNoise::heightChanged(int value)
{ {
_current_level_params.amplitude = ((double)value) / 1000.0; _current_level_params.amplitude = ((double)value) / 1000.0;
noiseSetLevel(_current, _current_level, _current_level_params, 1); _current->setLevel(_current_level, _current_level_params, 1);
previewLevel->redraw(); previewLevel->redraw();
previewTotal->redraw(); previewTotal->redraw();
} }
@ -346,7 +346,7 @@ void DialogNoise::heightChanged(int value)
void DialogNoise::scalingChanged(int value) void DialogNoise::scalingChanged(int value)
{ {
_current_level_params.wavelength = ((double)value) / 1000.0; _current_level_params.wavelength = ((double)value) / 1000.0;
noiseSetLevel(_current, _current_level, _current_level_params, 1); _current->setLevel(_current_level, _current_level_params, 1);
previewLevel->redraw(); previewLevel->redraw();
previewTotal->redraw(); previewTotal->redraw();
} }

View file

@ -5,8 +5,7 @@
#include <QComboBox> #include <QComboBox>
#include "basepreview.h" #include "basepreview.h"
#include "tools.h" #include "tools.h"
#include "NoiseGenerator.h"
#include "rendering/noise.h"
class DialogNoise : public DialogWithPreview class DialogNoise : public DialogWithPreview
{ {

View file

@ -68,7 +68,8 @@ HEADERS += \
textures/PreviewLayerCoverage.h \ textures/PreviewLayerCoverage.h \
textures/PreviewLayerLook.h \ textures/PreviewLayerLook.h \
textures/PreviewCumul.h \ textures/PreviewCumul.h \
textures/DialogTexturesLayer.h textures/DialogTexturesLayer.h \
editing_global.h
SOURCES += \ SOURCES += \
terrain/widgetheightmap.cpp \ terrain/widgetheightmap.cpp \
@ -150,6 +151,12 @@ else:unix: LIBS += -L$$OUT_PWD/../system/ -lpaysages_system
INCLUDEPATH += $$PWD/../system INCLUDEPATH += $$PWD/../system
DEPENDPATH += $$PWD/../system DEPENDPATH += $$PWD/../system
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../basics/release/ -lpaysages_basics
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../basics/debug/ -lpaysages_basics
else:unix: LIBS += -L$$OUT_PWD/../basics/ -lpaysages_basics
INCLUDEPATH += $$PWD/../basics
DEPENDPATH += $$PWD/../basics
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../exploring/release/ -lpaysages_exploring win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../exploring/release/ -lpaysages_exploring
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../exploring/debug/ -lpaysages_exploring else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../exploring/debug/ -lpaysages_exploring
else:unix: LIBS += -L$$OUT_PWD/../exploring/ -lpaysages_exploring else:unix: LIBS += -L$$OUT_PWD/../exploring/ -lpaysages_exploring

View file

@ -0,0 +1,14 @@
#ifndef EDITING_GLOBAL_H
#define EDITING_GLOBAL_H
namespace paysages
{
namespace system {}
namespace basics {}
namespace definition {}
}
using namespace paysages::system;
using namespace paysages::basics;
using namespace paysages::definition;
#endif // EDITING_GLOBAL_H

View file

@ -29,12 +29,12 @@ public:
double value, factor; double value, factor;
double minvalue, maxvalue; double minvalue, maxvalue;
noiseGetRange(noise, &minvalue, &maxvalue); noise->getRange(&minvalue, &maxvalue);
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
factor = ((double)(height / 2)) / maxvalue; factor = ((double)(height / 2)) / maxvalue;
value = -noiseGet1DTotal(noise, ((double)x) / factor) * factor; value = -noise->get1DTotal(((double)x) / factor) * factor;
painter.setPen(Qt::white); painter.setPen(Qt::white);
painter.drawLine(x, 0, x, height / 2 + value); painter.drawLine(x, 0, x, height / 2 + value);
painter.setPen(Qt::black); painter.setPen(Qt::black);

View file

@ -1,10 +1,16 @@
#ifndef _PAYSAGES_QT_INPUTNOISE_H_ #ifndef _PAYSAGES_QT_INPUTNOISE_H_
#define _PAYSAGES_QT_INPUTNOISE_H_ #define _PAYSAGES_QT_INPUTNOISE_H_
#include "editing_global.h"
#include <QWidget> #include <QWidget>
#include "baseinput.h" #include "baseinput.h"
#include "rendering/noise.h" namespace paysages {
namespace basics {
class NoiseGenerator;
}
}
class InputNoise:public BaseInput class InputNoise:public BaseInput
{ {

View file

@ -1,6 +1,8 @@
#include "dialogbaseterrainnoise.h" #include "dialogbaseterrainnoise.h"
#include "ui_dialogbaseterrainnoise.h" #include "ui_dialogbaseterrainnoise.h"
#include "NoiseGenerator.h"
DialogBaseTerrainNoise::DialogBaseTerrainNoise(QWidget *parent) : DialogBaseTerrainNoise::DialogBaseTerrainNoise(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::DialogBaseTerrainNoise) ui(new Ui::DialogBaseTerrainNoise)
@ -8,19 +10,19 @@ DialogBaseTerrainNoise::DialogBaseTerrainNoise(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
_original = 0; _original = 0;
_modified = noiseCreateGenerator(); _modified = new NoiseGenerator();
} }
DialogBaseTerrainNoise::~DialogBaseTerrainNoise() DialogBaseTerrainNoise::~DialogBaseTerrainNoise()
{ {
delete ui; delete ui;
noiseDeleteGenerator(_modified); delete _modified;
} }
void DialogBaseTerrainNoise::setNoise(NoiseGenerator* noise) void DialogBaseTerrainNoise::setNoise(NoiseGenerator* noise)
{ {
_original = noise; _original = noise;
noiseCopy(noise, _modified); noise->copy(_modified);
} }
int DialogBaseTerrainNoise::editNoise(QWidget* parent, NoiseGenerator* noise) int DialogBaseTerrainNoise::editNoise(QWidget* parent, NoiseGenerator* noise)

View file

@ -1,11 +1,17 @@
#ifndef DIALOGBASETERRAINNOISE_H #ifndef DIALOGBASETERRAINNOISE_H
#define DIALOGBASETERRAINNOISE_H #define DIALOGBASETERRAINNOISE_H
#include "editing_global.h"
#include <QDialog> #include <QDialog>
#include "rendering/noise.h"
namespace paysages {
namespace basics {
class NoiseGenerator;
}
}
namespace Ui { namespace Ui {
class DialogBaseTerrainNoise; class DialogBaseTerrainNoise;
} }
class DialogBaseTerrainNoise : public QDialog class DialogBaseTerrainNoise : public QDialog

View file

@ -1,6 +1,7 @@
#include "paintingbrush.h" #include "paintingbrush.h"
#include <cmath> #include <cmath>
#include "NoiseGenerator.h"
PaintingBrush::PaintingBrush() PaintingBrush::PaintingBrush()
{ {
@ -9,13 +10,13 @@ PaintingBrush::PaintingBrush()
_smoothing = 0.0; _smoothing = 0.0;
_strength = 0.0; _strength = 0.0;
_height = 0.0; _height = 0.0;
_noise = noiseCreateGenerator(); _noise = new NoiseGenerator();
noiseAddLevelsSimple(_noise, 10, 1.0, -0.5, 0.5, 0.5); _noise->addLevelsSimple(10, 1.0, -0.5, 0.5, 0.5);
} }
PaintingBrush::~PaintingBrush() PaintingBrush::~PaintingBrush()
{ {
noiseDeleteGenerator(_noise); delete _noise;
} }
void PaintingBrush::setMode(PaintingBrushMode mode) void PaintingBrush::setMode(PaintingBrushMode mode)
@ -56,7 +57,7 @@ void PaintingBrush::setStrength(QAbstractSlider* slider)
void PaintingBrush::randomizeNoise() void PaintingBrush::randomizeNoise()
{ {
noiseRandomizeOffsets(_noise); _noise->randomizeOffsets();
} }
double PaintingBrush::getInfluence(double relative_x, double relative_z) double PaintingBrush::getInfluence(double relative_x, double relative_z)

View file

@ -2,9 +2,14 @@
#define PAINTINGBRUSH_H #define PAINTINGBRUSH_H
#include <QAbstractSlider> #include <QAbstractSlider>
#include "rendering/noise.h"
#include "rendering/terrain/public.h" #include "rendering/terrain/public.h"
namespace paysages {
namespace basics {
class NoiseGenerator;
}
}
typedef enum typedef enum
{ {
PAINTING_BRUSH_RAISE = 0, PAINTING_BRUSH_RAISE = 0,

View file

@ -3,6 +3,7 @@
#include <QPainter> #include <QPainter>
#include <QPaintEvent> #include <QPaintEvent>
#include "tools.h" #include "tools.h"
#include "NoiseGenerator.h"
WidgetTerrainBaseNoisePreview::WidgetTerrainBaseNoisePreview(QWidget* parent) : WidgetTerrainBaseNoisePreview::WidgetTerrainBaseNoisePreview(QWidget* parent) :
DrawingWidget(parent) DrawingWidget(parent)
@ -30,12 +31,12 @@ void WidgetTerrainBaseNoisePreview::doDrawing(QPainter* painter)
double value, factor; double value, factor;
double minvalue, maxvalue; double minvalue, maxvalue;
noiseGetRange(_noise, &minvalue, &maxvalue); _noise->getRange(&minvalue, &maxvalue);
factor = ((double)height) / (maxvalue - minvalue); factor = ((double)height) / (maxvalue - minvalue);
for (int x = boundaries.left(); x <= boundaries.right(); x++) for (int x = boundaries.left(); x <= boundaries.right(); x++)
{ {
value = noiseGet1DTotal(_noise, 100.0 * ((double)x) / factor); value = _noise->get1DTotal(100.0 * ((double)x) / factor);
painter->drawLine(x, height - 1 - (value - minvalue) * factor, x, height - 1); painter->drawLine(x, height - 1 - (value - minvalue) * factor, x, height - 1);
} }

View file

@ -1,12 +1,17 @@
#ifndef _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_ #ifndef _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_
#define _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_ #define _PAYSAGES_EDITING_TERRAIN_WIDGETTERRAINBASENOISEPREVIEW_H_
#include "editing_global.h"
#include "editing/common/DrawingWidget.h" #include "editing/common/DrawingWidget.h"
#include "rendering/noise.h"
class QPainter; class QPainter;
namespace paysages {
namespace basics {
class NoiseGenerator;
}
}
class WidgetTerrainBaseNoisePreview : public DrawingWidget class WidgetTerrainBaseNoisePreview : public DrawingWidget
{ {
Q_OBJECT Q_OBJECT

View file

@ -2,7 +2,7 @@
#include <ctime> #include <ctime>
#include "rendering/noise.h" #include "NoiseGenerator.h"
#include "rendering/terrain/ter_raster.h" #include "rendering/terrain/ter_raster.h"
static Scenery* _main_scenery; static Scenery* _main_scenery;

View file

@ -25,9 +25,6 @@
//class PackStream; //class PackStream;
//class Renderer; //class Renderer;
namespace paysages {
namespace rendering {
/** /**
* @brief Global scenery management * @brief Global scenery management
* *
@ -79,9 +76,6 @@ private:
WaterDefinition* water; WaterDefinition* water;
}; };
}
}
extern "C" { extern "C" {
#endif #endif

View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "PackStream.h" #include "PackStream.h"
#include "NoiseGenerator.h"
/******************** Global definition ********************/ /******************** Global definition ********************/
static void _validateDefinition(CloudsDefinition* definition) static void _validateDefinition(CloudsDefinition* definition)
@ -62,23 +63,23 @@ void cloudsLayerValidateDefinition(CloudsLayerDefinition* definition)
} }
curveClear(definition->_coverage_by_altitude); curveClear(definition->_coverage_by_altitude);
noiseClearLevels(definition->_shape_noise); definition->_shape_noise->clearLevels();
noiseClearLevels(definition->_edge_noise); definition->_edge_noise->clearLevels();
noiseClearLevels(definition->_coverage_noise); definition->_coverage_noise->clearLevels();
noiseAddLevelsSimple(definition->_coverage_noise, 2, 10.0, 0.0, 1.0, 0.0); definition->_coverage_noise->addLevelsSimple(2, 10.0, 0.0, 1.0, 0.0);
noiseAddLevelsSimple(definition->_coverage_noise, 2, 1.0, 0.0, 1.0, 0.0); definition->_coverage_noise->addLevelsSimple(2, 1.0, 0.0, 1.0, 0.0);
noiseSetFunctionParams(definition->_coverage_noise, NOISE_FUNCTION_NAIVE, 0.0, 0.0); definition->_coverage_noise->setFunctionParams(NOISE_FUNCTION_NAIVE, 0.0, 0.0);
switch (definition->type) switch (definition->type)
{ {
case CLOUDS_TYPE_CIRRUS: case CLOUDS_TYPE_CIRRUS:
curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.5, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.5, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0);
noiseAddLevelsSimple(definition->_shape_noise, 3, 1.0, 0.0, 1.0, 0.5); definition->_shape_noise->addLevelsSimple(3, 1.0, 0.0, 1.0, 0.5);
noiseSetFunctionParams(definition->_shape_noise, NOISE_FUNCTION_SIMPLEX, 0.0, 0.0); definition->_shape_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.0, 0.0);
noiseAddLevelsSimple(definition->_edge_noise, 4, 1.0, -0.5, 0.5, 0.5); definition->_edge_noise->addLevelsSimple(4, 1.0, -0.5, 0.5, 0.5);
noiseSetFunctionParams(definition->_edge_noise, NOISE_FUNCTION_SIMPLEX, -0.2, 0.0); definition->_edge_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -0.2, 0.0);
break; break;
case CLOUDS_TYPE_CUMULUS: case CLOUDS_TYPE_CUMULUS:
curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0);
@ -86,38 +87,38 @@ void cloudsLayerValidateDefinition(CloudsLayerDefinition* definition)
curveQuickAddPoint(definition->_coverage_by_altitude, 0.4, 0.8); curveQuickAddPoint(definition->_coverage_by_altitude, 0.4, 0.8);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.7, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.7, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0);
noiseAddLevelsSimple(definition->_shape_noise, 7, 1.0, 0.0, 1.0, 0.5); definition->_shape_noise->addLevelsSimple(7, 1.0, 0.0, 1.0, 0.5);
noiseSetFunctionParams(definition->_shape_noise, NOISE_FUNCTION_SIMPLEX, 0.4, 0.0); definition->_shape_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.4, 0.0);
noiseAddLevelsSimple(definition->_edge_noise, 4, 1.0, -0.5, 0.5, 0.5); definition->_edge_noise->addLevelsSimple(4, 1.0, -0.5, 0.5, 0.5);
noiseSetFunctionParams(definition->_edge_noise, NOISE_FUNCTION_SIMPLEX, 0.8, 0.0); definition->_edge_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.8, 0.0);
break; break;
case CLOUDS_TYPE_STRATOCUMULUS: case CLOUDS_TYPE_STRATOCUMULUS:
curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.2, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.2, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.5, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.5, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0);
noiseAddLevelsSimple(definition->_shape_noise, 4, 1.0, 0.0, 1.0, 0.5); definition->_shape_noise->addLevelsSimple(4, 1.0, 0.0, 1.0, 0.5);
noiseSetFunctionParams(definition->_shape_noise, NOISE_FUNCTION_SIMPLEX, 0.3, 0.0); definition->_shape_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.3, 0.0);
noiseAddLevelsSimple(definition->_edge_noise, 6, 1.0, -0.5, 0.5, 0.5); definition->_edge_noise->addLevelsSimple(6, 1.0, -0.5, 0.5, 0.5);
noiseSetFunctionParams(definition->_edge_noise, NOISE_FUNCTION_SIMPLEX, 0.5, 0.0); definition->_edge_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.5, 0.0);
break; break;
case CLOUDS_TYPE_STRATUS: case CLOUDS_TYPE_STRATUS:
curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.0, 0.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.2, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.2, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 0.8, 1.0); curveQuickAddPoint(definition->_coverage_by_altitude, 0.8, 1.0);
curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0); curveQuickAddPoint(definition->_coverage_by_altitude, 1.0, 0.0);
noiseAddLevelsSimple(definition->_shape_noise, 3, 1.0, 0.0, 1.0, 0.5); definition->_shape_noise->addLevelsSimple(3, 1.0, 0.0, 1.0, 0.5);
noiseSetFunctionParams(definition->_shape_noise, NOISE_FUNCTION_SIMPLEX, -0.3, 0.0); definition->_shape_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -0.3, 0.0);
noiseAddLevelsSimple(definition->_edge_noise, 4, 1.0, -0.5, 0.5, 0.5); definition->_edge_noise->addLevelsSimple(4, 1.0, -0.5, 0.5, 0.5);
noiseSetFunctionParams(definition->_edge_noise, NOISE_FUNCTION_SIMPLEX, -0.5, 0.0); definition->_edge_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -0.5, 0.0);
break; break;
default: default:
break; break;
} }
noiseNormalizeAmplitude(definition->_coverage_noise, -1.0, 3.0, 0); definition->_coverage_noise->normalizeAmplitude(-1.0, 3.0, 0);
noiseNormalizeAmplitude(definition->_shape_noise, -0.5, 0.5, 0); definition->_shape_noise->normalizeAmplitude(-0.5, 0.5, 0);
noiseNormalizeAmplitude(definition->_edge_noise, -0.5, 0.5, 0); definition->_edge_noise->normalizeAmplitude(-0.5, 0.5, 0);
materialValidate(&definition->material); materialValidate(&definition->material);
} }
@ -128,9 +129,9 @@ CloudsLayerDefinition* cloudsLayerCreateDefinition()
result = new CloudsLayerDefinition; result = new CloudsLayerDefinition;
result->_coverage_by_altitude = curveCreate(); result->_coverage_by_altitude = curveCreate();
result->_coverage_noise = noiseCreateGenerator(); result->_coverage_noise = new NoiseGenerator();
result->_shape_noise = noiseCreateGenerator(); result->_shape_noise = new NoiseGenerator();
result->_edge_noise = noiseCreateGenerator(); result->_edge_noise = new NoiseGenerator();
cloudsLayerAutoPreset(result, CLOUDS_LAYER_PRESET_CIRRUS); cloudsLayerAutoPreset(result, CLOUDS_LAYER_PRESET_CIRRUS);
@ -140,9 +141,9 @@ CloudsLayerDefinition* cloudsLayerCreateDefinition()
void cloudsLayerDeleteDefinition(CloudsLayerDefinition* definition) void cloudsLayerDeleteDefinition(CloudsLayerDefinition* definition)
{ {
curveDelete(definition->_coverage_by_altitude); curveDelete(definition->_coverage_by_altitude);
noiseDeleteGenerator(definition->_coverage_noise); delete definition->_coverage_noise;
noiseDeleteGenerator(definition->_shape_noise); delete definition->_shape_noise;
noiseDeleteGenerator(definition->_edge_noise); delete definition->_edge_noise;
delete definition; delete definition;
} }
@ -157,13 +158,13 @@ void cloudsLayerCopyDefinition(CloudsLayerDefinition* source, CloudsLayerDefinit
curveCopy(source->_coverage_by_altitude, destination->_coverage_by_altitude); curveCopy(source->_coverage_by_altitude, destination->_coverage_by_altitude);
destination->_coverage_noise = temp._coverage_noise; destination->_coverage_noise = temp._coverage_noise;
noiseCopy(source->_coverage_noise, destination->_coverage_noise); source->_coverage_noise->copy(destination->_coverage_noise);
destination->_shape_noise = temp._shape_noise; destination->_shape_noise = temp._shape_noise;
noiseCopy(source->_shape_noise, destination->_shape_noise); source->_shape_noise->copy(destination->_shape_noise);
destination->_edge_noise = temp._edge_noise; destination->_edge_noise = temp._edge_noise;
noiseCopy(source->_edge_noise, destination->_edge_noise); source->_edge_noise->copy(destination->_edge_noise);
} }
void _cloudsLayerSave(PackStream* stream, CloudsLayerDefinition* layer) void _cloudsLayerSave(PackStream* stream, CloudsLayerDefinition* layer)
@ -174,9 +175,9 @@ void _cloudsLayerSave(PackStream* stream, CloudsLayerDefinition* layer)
stream->write(&layer->lower_altitude); stream->write(&layer->lower_altitude);
stream->write(&layer->thickness); stream->write(&layer->thickness);
curveSave(stream, layer->_coverage_by_altitude); curveSave(stream, layer->_coverage_by_altitude);
noiseSaveGenerator(stream, layer->_coverage_noise); layer->_coverage_noise->save(stream);
noiseSaveGenerator(stream, layer->_shape_noise); layer->_shape_noise->save(stream);
noiseSaveGenerator(stream, layer->_edge_noise); layer->_edge_noise->save(stream);
materialSave(stream, &layer->material); materialSave(stream, &layer->material);
stream->write(&layer->hardness); stream->write(&layer->hardness);
stream->write(&layer->transparencydepth); stream->write(&layer->transparencydepth);
@ -197,9 +198,9 @@ void _cloudsLayerLoad(PackStream* stream, CloudsLayerDefinition* layer)
stream->read(&layer->lower_altitude); stream->read(&layer->lower_altitude);
stream->read(&layer->thickness); stream->read(&layer->thickness);
curveLoad(stream, layer->_coverage_by_altitude); curveLoad(stream, layer->_coverage_by_altitude);
noiseLoadGenerator(stream, layer->_coverage_noise); layer->_coverage_noise->load(stream);
noiseLoadGenerator(stream, layer->_shape_noise); layer->_shape_noise->load(stream);
noiseLoadGenerator(stream, layer->_edge_noise); layer->_edge_noise->load(stream);
materialLoad(stream, &layer->material); materialLoad(stream, &layer->material);
stream->read(&layer->hardness); stream->read(&layer->hardness);
stream->read(&layer->transparencydepth); stream->read(&layer->transparencydepth);

View file

@ -1,6 +1,7 @@
#include "clo_density.h" #include "clo_density.h"
#include "../tools.h" #include "../tools.h"
#include "NoiseGenerator.h"
double cloudsGetLayerCoverage(CloudsLayerDefinition* layer, Vector3 location) double cloudsGetLayerCoverage(CloudsLayerDefinition* layer, Vector3 location)
{ {
@ -10,7 +11,7 @@ double cloudsGetLayerCoverage(CloudsLayerDefinition* layer, Vector3 location)
} }
else else
{ {
double coverage = 0.5 + noiseGet2DTotal(layer->_coverage_noise, location.x / layer->shape_scaling, location.z / layer->shape_scaling); double coverage = 0.5 + layer->_coverage_noise->get2DTotal(location.x / layer->shape_scaling, location.z / layer->shape_scaling);
coverage -= (1.0 - layer->base_coverage); coverage -= (1.0 - layer->base_coverage);
coverage *= curveGetValue(layer->_coverage_by_altitude, (location.y - layer->lower_altitude) / layer->thickness); coverage *= curveGetValue(layer->_coverage_by_altitude, (location.y - layer->lower_altitude) / layer->thickness);
@ -42,7 +43,7 @@ double cloudsGetLayerDensity(CloudsLayerDefinition* layer, Vector3 location, dou
} }
else else
{ {
double density = noiseGet3DTotal(layer->_shape_noise, location.x / layer->shape_scaling, location.y / layer->shape_scaling, location.z / layer->shape_scaling); double density = layer->_shape_noise->get3DTotal(location.x / layer->shape_scaling, location.y / layer->shape_scaling, location.z / layer->shape_scaling);
density -= (0.5 - coverage); density -= (0.5 - coverage);
return (density <= 0.0) ? 0.0 : density; return (density <= 0.0) ? 0.0 : density;
} }
@ -60,7 +61,7 @@ double cloudsGetEdgeDensity(CloudsLayerDefinition* layer, Vector3 location, doub
} }
else else
{ {
double density = noiseGet3DTotal(layer->_edge_noise, location.x / layer->edge_scaling, location.y / layer->edge_scaling, location.z / layer->edge_scaling); double density = layer->_edge_noise->get3DTotal(location.x / layer->edge_scaling, location.y / layer->edge_scaling, location.z / layer->edge_scaling);
density -= (0.5 - layer_density); density -= (0.5 - layer_density);
return (density <= 0.0) ? 0.0 : density; return (density <= 0.0) ? 0.0 : density;
} }

View file

@ -1,7 +1,8 @@
#include "private.h" #include "private.h"
#include <math.h> #include <cmath>
#include <stdlib.h> #include <cstdlib>
#include "NoiseGenerator.h"
/* /*
* Clouds presets. * Clouds presets.
@ -22,9 +23,9 @@ void cloudsAutoPreset(CloudsDefinition* definition, CloudsPreset preset)
void cloudsLayerAutoPreset(CloudsLayerDefinition* definition, CloudsLayerPreset preset) void cloudsLayerAutoPreset(CloudsLayerDefinition* definition, CloudsLayerPreset preset)
{ {
noiseRandomizeOffsets(definition->_coverage_noise); definition->_coverage_noise->randomizeOffsets();
noiseRandomizeOffsets(definition->_edge_noise); definition->_edge_noise->randomizeOffsets();
noiseRandomizeOffsets(definition->_shape_noise); definition->_shape_noise->randomizeOffsets();
definition->material.base = colorToHSL(colorFromValues(0.7, 0.7, 0.7, 1.0)); definition->material.base = colorToHSL(colorFromValues(0.7, 0.7, 0.7, 1.0));

View file

@ -6,12 +6,13 @@
#include "../tools/lighting.h" #include "../tools/lighting.h"
#include "../tools/curve.h" #include "../tools/curve.h"
#include "../tools/euclid.h" #include "../tools/euclid.h"
#include "../noise.h"
#include "Layers.h" #include "Layers.h"
#ifdef __cplusplus namespace paysages {
extern "C" { namespace basics {
#endif class NoiseGenerator;
}
}
typedef enum typedef enum
{ {
@ -82,8 +83,4 @@ RENDERINGSHARED_EXPORT LayerType cloudsGetLayerType();
RENDERINGSHARED_EXPORT void cloudsAutoPreset(CloudsDefinition* definition, CloudsPreset preset); RENDERINGSHARED_EXPORT void cloudsAutoPreset(CloudsDefinition* definition, CloudsPreset preset);
RENDERINGSHARED_EXPORT void cloudsLayerAutoPreset(CloudsLayerDefinition* definition, CloudsLayerPreset preset); RENDERINGSHARED_EXPORT void cloudsLayerAutoPreset(CloudsLayerDefinition* definition, CloudsLayerPreset preset);
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -1,621 +0,0 @@
#include "noise.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "tools.h"
#include "noisesimplex.h"
#include "noisenaive.h"
#include "noiseperlin.h"
#include "opencl.h"
#define MAX_LEVEL_COUNT 30
struct NoiseGenerator
{
NoiseFunction function;
double height_offset;
int level_count;
NoiseLevel levels[MAX_LEVEL_COUNT];
double _min_value;
double _max_value;
double (*_func_noise_1d)(double x);
double (*_func_noise_2d)(double x, double y);
double (*_func_noise_3d)(double x, double y, double z);
};
void noiseInit()
{
noiseSimplexInit();
noisePerlinInit();
noiseNaiveInit();
/* Noise stats */
/*NoiseGenerator* noise;
int x;
double min, max, value;
noise = noiseCreateGenerator();
noiseSetFunctionParams(noise, NOISE_FUNCTION_NAIVE, 0.0);
noiseAddLevelSimple(noise, 1.0, 0.0, 1.0);
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);*/
}
void noiseQuit()
{
noiseNaiveQuit();
}
void noiseSave(PackStream* stream)
{
noiseNaiveSave(stream);
}
void noiseLoad(PackStream* stream)
{
noiseNaiveLoad(stream);
}
NoiseGenerator* noiseCreateGenerator()
{
NoiseGenerator* result;
/* initialize */
result = new NoiseGenerator;
result->function.algorithm = NOISE_FUNCTION_SIMPLEX;
result->function.ridge_factor = 0.0;
result->function.curve_factor = 0.0;
result->level_count = 0;
result->height_offset = 0.0;
noiseRandomizeOffsets(result);
noiseValidate(result);
return result;
}
void noiseDeleteGenerator(NoiseGenerator* generator)
{
delete generator;
}
void noiseSaveGenerator(PackStream* stream, NoiseGenerator* generator)
{
int x;
x = (int)generator->function.algorithm;
stream->write(&x);
stream->write(&generator->function.ridge_factor);
stream->write(&generator->function.curve_factor);
stream->write(&generator->height_offset);
stream->write(&generator->level_count);
for (x = 0; x < generator->level_count; x++)
{
NoiseLevel* level = generator->levels + x;
stream->write(&level->wavelength);
stream->write(&level->amplitude);
stream->write(&level->minvalue);
stream->write(&level->xoffset);
stream->write(&level->yoffset);
stream->write(&level->zoffset);
}
}
void noiseLoadGenerator(PackStream* stream, NoiseGenerator* generator)
{
int x;
stream->read(&x);
generator->function.algorithm = (NoiseFunctionAlgorithm)x;
stream->read(&generator->function.ridge_factor);
stream->read(&generator->function.curve_factor);
stream->read(&generator->height_offset);
stream->read(&generator->level_count);
for (x = 0; x < generator->level_count; x++)
{
NoiseLevel* level = generator->levels + x;
stream->read(&level->wavelength);
stream->read(&level->amplitude);
stream->read(&level->minvalue);
stream->read(&level->xoffset);
stream->read(&level->yoffset);
stream->read(&level->zoffset);
}
noiseValidate(generator);
}
void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination)
{
destination->function = source->function;
destination->height_offset = source->height_offset;
destination->level_count = source->level_count;
memcpy(destination->levels, source->levels, sizeof(NoiseLevel) * destination->level_count);
noiseValidate(destination);
}
void noiseValidate(NoiseGenerator* generator)
{
int x;
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:
generator->_func_noise_1d = noisePerlinGet1DValue;
generator->_func_noise_2d = noisePerlinGet2DValue;
generator->_func_noise_3d = noisePerlinGet3DValue;
break;
case NOISE_FUNCTION_NAIVE:
generator->_func_noise_1d = noiseNaiveGet1DValue;
generator->_func_noise_2d = noiseNaiveGet2DValue;
generator->_func_noise_3d = noiseNaiveGet3DValue;
break;
}
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;
}
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;
}
generator->_min_value = generator->height_offset;
generator->_max_value = generator->height_offset;
for (x = 0; x < generator->level_count; x++)
{
generator->_min_value += generator->levels[x].minvalue;
generator->_max_value += generator->levels[x].minvalue + generator->levels[x].amplitude;
}
}
void noiseRandomizeOffsets(NoiseGenerator* generator)
{
int i;
for (i = 0; i < MAX_LEVEL_COUNT; i++)
{
generator->levels[i].xoffset = toolsRandom();
generator->levels[i].yoffset = toolsRandom();
generator->levels[i].zoffset = toolsRandom();
}
}
NoiseFunction noiseGetFunction(NoiseGenerator* generator)
{
return generator->function;
}
void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function)
{
generator->function = *function;
noiseValidate(generator);
}
void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z))
{
generator->_func_noise_1d = func1d;
generator->_func_noise_2d = func2d;
generator->_func_noise_3d = func3d;
}
void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor)
{
NoiseFunction function = {algorithm, ridge_factor, curve_factor};
noiseSetFunction(generator, &function);
}
void noiseForceValue(NoiseGenerator* generator, double value)
{
noiseClearLevels(generator);
generator->height_offset = value;
noiseAddLevelSimple(generator, 1.0, 0.0, 0.0); /* FIXME Should not be needed */
}
void noiseGetRange(NoiseGenerator* generator, double* minvalue, double* maxvalue)
{
*minvalue = generator->_min_value;
*maxvalue = generator->_max_value;
}
int noiseGetLevelCount(NoiseGenerator* generator)
{
return generator->level_count;
}
void noiseClearLevels(NoiseGenerator* generator)
{
generator->level_count = 0;
noiseValidate(generator);
}
void noiseAddLevel(NoiseGenerator* generator, NoiseLevel level, int protect_offsets)
{
if (generator->level_count < MAX_LEVEL_COUNT)
{
NoiseLevel baselevel = generator->levels[generator->level_count];
if (protect_offsets)
{
level.xoffset = baselevel.xoffset;
level.yoffset = baselevel.yoffset;
level.zoffset = baselevel.zoffset;
}
generator->levels[generator->level_count] = level;
generator->level_count++;
noiseValidate(generator);
}
}
void noiseAddLevelSimple(NoiseGenerator* generator, double scaling, double minvalue, double maxvalue)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
noiseAddLevel(generator, level, 1);
}
void noiseAddLevels(NoiseGenerator* generator, int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor)
{
int i;
for (i = 0; i < level_count; i++)
{
noiseAddLevel(generator, start_level, 1);
start_level.minvalue += start_level.amplitude * (1.0 - amplitude_factor) * center_factor;
start_level.wavelength *= scaling_factor;
start_level.amplitude *= amplitude_factor;
}
}
void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double scaling, double minvalue, double maxvalue, double center_factor)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
noiseAddLevels(generator, level_count, level, 0.5, 0.5, center_factor);
}
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--;
noiseValidate(generator);
}
}
int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params)
{
if (level >= 0 && level < generator->level_count)
{
*params = generator->levels[level];
return 1;
}
else
{
return 0;
}
}
void noiseSetLevel(NoiseGenerator* generator, int index, NoiseLevel level, int protect_offsets)
{
if (index >= 0 && index < generator->level_count)
{
NoiseLevel baselevel = generator->levels[index];
if (protect_offsets)
{
level.xoffset = baselevel.xoffset;
level.yoffset = baselevel.yoffset;
level.zoffset = baselevel.zoffset;
}
generator->levels[index] = level;
noiseValidate(generator);
}
}
void noiseSetLevelSimple(NoiseGenerator* generator, int index, double scaling, double minvalue, double maxvalue)
{
NoiseLevel level;
level.wavelength = scaling;
level.minvalue = minvalue;
level.amplitude = maxvalue - minvalue;
noiseSetLevel(generator, index, level, 1);
}
void noiseNormalizeAmplitude(NoiseGenerator* generator, double minvalue, double maxvalue, int adjust_scaling)
{
int level;
double current_minvalue, current_maxvalue, current_amplitude;
double target_amplitude, factor;
if (generator->level_count == 0)
{
return;
}
target_amplitude = maxvalue - minvalue;
noiseGetRange(generator, &current_minvalue, &current_maxvalue);
current_amplitude = current_maxvalue - current_minvalue;
factor = target_amplitude / current_amplitude;
for (level = 0; level < generator->level_count; level++)
{
generator->levels[level].minvalue *= factor;
generator->levels[level].amplitude *= factor;
if (adjust_scaling)
{
generator->levels[level].wavelength *= factor;
}
}
generator->height_offset = minvalue + (generator->height_offset - current_minvalue) * factor;
noiseValidate(generator);
}
static inline double _fixValue(double value, double ridge, double curve)
{
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;
}
if (ridge > 0.0)
{
return fabs(value - ridge) / (1.0 - ridge);
}
else if (ridge < 0.0)
{
return 1.0 - fabs(value - 1.0 - ridge) / (1.0 + ridge);
}
else
{
return value;
}
/*if (ridge > 0.0)
{
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;
}*/
}
static inline double _get1DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x)
{
return level->minvalue + _fixValue(generator->_func_noise_1d(x / level->wavelength + level->xoffset), generator->function.ridge_factor, generator->function.curve_factor) * level->amplitude;
}
double noiseGet1DLevel(NoiseGenerator* generator, int level, double x)
{
if (level >= 0 && level < generator->level_count)
{
return _get1DLevelValue(generator, generator->levels + level, x);
}
else
{
return 0.0;
}
}
double noiseGet1DTotal(NoiseGenerator* generator, double x)
{
int level;
double result;
result = 0.0;
for (level = 0; level < generator->level_count; level++)
{
result += _get1DLevelValue(generator, generator->levels + level, x);
}
return result + generator->height_offset;
}
double noiseGet1DDetail(NoiseGenerator* generator, double x, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < generator->level_count; level++)
{
height = generator->levels[level].amplitude;
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;
}
static inline double _get2DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x, double y)
{
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;
}
double noiseGet2DLevel(NoiseGenerator* generator, int level, double x, double y)
{
if (level >= 0 && level < generator->level_count)
{
return _get2DLevelValue(generator, generator->levels + level, x, y);
}
else
{
return 0.0;
}
}
double noiseGet2DTotal(NoiseGenerator* generator, double x, double y)
{
int level;
double result;
result = 0.0;
for (level = 0; level < generator->level_count; level++)
{
result += _get2DLevelValue(generator, generator->levels + level, x, y);
}
return result + generator->height_offset;
}
double noiseGet2DDetail(NoiseGenerator* generator, double x, double y, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < generator->level_count; level++)
{
height = generator->levels[level].amplitude;
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;
}
static inline double _get3DLevelValue(NoiseGenerator* generator, NoiseLevel* level, double x, double y, double z)
{
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;
}
double noiseGet3DLevel(NoiseGenerator* generator, int level, double x, double y, double z)
{
if (level >= 0 && level < generator->level_count)
{
return _get3DLevelValue(generator, generator->levels + level, x, y, z);
}
else
{
return 0.0;
}
}
double noiseGet3DTotal(NoiseGenerator* generator, double x, double y, double z)
{
int level;
double result;
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;
}
double noiseGet3DDetail(NoiseGenerator* generator, double x, double y, double z, double detail)
{
int level;
double result, height, factor;
result = 0.0;
for (level = 0; level < generator->level_count; level++)
{
height = generator->levels[level].amplitude;
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;
}

View file

@ -1,83 +0,0 @@
#ifndef _PAYSAGES_NOISE_H_
#define _PAYSAGES_NOISE_H_
#include "rendering_global.h"
namespace paysages {
namespace system {class PackStream;}
}
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
NOISE_FUNCTION_SIMPLEX,
NOISE_FUNCTION_PERLIN,
NOISE_FUNCTION_NAIVE
} NoiseFunctionAlgorithm;
typedef struct
{
NoiseFunctionAlgorithm algorithm;
double ridge_factor; /* -0.5;0.5 */
double curve_factor; /* -1.0;1.0 */
} NoiseFunction;
typedef struct
{
double wavelength;
double amplitude;
double minvalue;
double xoffset;
double yoffset;
double zoffset;
} NoiseLevel;
typedef struct NoiseGenerator NoiseGenerator;
RENDERINGSHARED_EXPORT void noiseInit();
RENDERINGSHARED_EXPORT void noiseQuit();
RENDERINGSHARED_EXPORT void noiseSave(PackStream* stream);
RENDERINGSHARED_EXPORT void noiseLoad(PackStream* stream);
RENDERINGSHARED_EXPORT NoiseGenerator* noiseCreateGenerator();
RENDERINGSHARED_EXPORT void noiseDeleteGenerator(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseSaveGenerator(PackStream* stream, NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseLoadGenerator(PackStream* stream, NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseCopy(NoiseGenerator* source, NoiseGenerator* destination);
RENDERINGSHARED_EXPORT void noiseValidate(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseRandomizeOffsets(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT NoiseFunction noiseGetFunction(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseSetCustomFunction(NoiseGenerator* generator, double (*func1d)(double x), double (*func2d)(double x, double y), double (*func3d)(double x, double y, double z));
RENDERINGSHARED_EXPORT void noiseSetFunction(NoiseGenerator* generator, NoiseFunction* function);
RENDERINGSHARED_EXPORT void noiseSetFunctionParams(NoiseGenerator* generator, NoiseFunctionAlgorithm algorithm, double ridge_factor, double curve_factor);
RENDERINGSHARED_EXPORT void noiseForceValue(NoiseGenerator* generator, double value);
RENDERINGSHARED_EXPORT void noiseGetRange(NoiseGenerator* generator, double* minvalue, double* maxvalue);
RENDERINGSHARED_EXPORT int noiseGetLevelCount(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseClearLevels(NoiseGenerator* generator);
RENDERINGSHARED_EXPORT void noiseAddLevel(NoiseGenerator* generator, NoiseLevel level, int protect_offsets);
RENDERINGSHARED_EXPORT void noiseAddLevelSimple(NoiseGenerator* generator, double scaling, double minvalue, double maxvalue);
RENDERINGSHARED_EXPORT void noiseAddLevels(NoiseGenerator* generator, int level_count, NoiseLevel start_level, double scaling_factor, double amplitude_factor, double center_factor);
RENDERINGSHARED_EXPORT void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double scaling, double minvalue, double maxvalue, double center_factor);
RENDERINGSHARED_EXPORT void noiseRemoveLevel(NoiseGenerator* generator, int level);
RENDERINGSHARED_EXPORT int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params);
RENDERINGSHARED_EXPORT void noiseSetLevel(NoiseGenerator* generator, int index, NoiseLevel level, int protect_offsets);
RENDERINGSHARED_EXPORT void noiseSetLevelSimple(NoiseGenerator* generator, int index, double scaling, double minvalue, double maxvalue);
RENDERINGSHARED_EXPORT void noiseNormalizeAmplitude(NoiseGenerator* generator, double minvalue, double maxvalue, int adjust_scaling);
RENDERINGSHARED_EXPORT double noiseGet1DLevel(NoiseGenerator* generator, int level, double x);
RENDERINGSHARED_EXPORT double noiseGet1DTotal(NoiseGenerator* generator, double x);
RENDERINGSHARED_EXPORT double noiseGet1DDetail(NoiseGenerator* generator, double x, double detail);
RENDERINGSHARED_EXPORT double noiseGet2DLevel(NoiseGenerator* generator, int level, double x, double y);
RENDERINGSHARED_EXPORT double noiseGet2DTotal(NoiseGenerator* generator, double x, double y);
RENDERINGSHARED_EXPORT double noiseGet2DDetail(NoiseGenerator* generator, double x, double y, double detail);
RENDERINGSHARED_EXPORT double noiseGet3DLevel(NoiseGenerator* generator, int level, double x, double y, double z);
RENDERINGSHARED_EXPORT double noiseGet3DTotal(NoiseGenerator* generator, double x, double y, double z);
RENDERINGSHARED_EXPORT double noiseGet3DDetail(NoiseGenerator* generator, double x, double y, double z, double detail);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,24 +0,0 @@
#ifndef _PAYSAGES_NOISENAIVE_H_
#define _PAYSAGES_NOISENAIVE_H_
#include "rendering_global.h"
#include "PackStream.h"
#ifdef __cplusplus
extern "C" {
#endif
RENDERINGSHARED_EXPORT void noiseNaiveInit();
RENDERINGSHARED_EXPORT void noiseNaiveQuit();
RENDERINGSHARED_EXPORT void noiseNaiveSave(PackStream* stream);
RENDERINGSHARED_EXPORT void noiseNaiveLoad(PackStream* stream);
RENDERINGSHARED_EXPORT double noiseNaiveGet1DValue(double x);
RENDERINGSHARED_EXPORT double noiseNaiveGet2DValue(double x, double y);
RENDERINGSHARED_EXPORT double noiseNaiveGet3DValue(double x, double y, double z);
/*double noiseNaiveGet4DValue(double x, double y, double z, double w);*/
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,186 +0,0 @@
#include "noiseperlin.h"
/*
* Perlin noise implementation.
*
* Based on Ken Perlin implementation.
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#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*2.0};
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) * 1.068 + 0.5;
}
double noisePerlinGet2DValue(double x, double y)
{
double vec[2] = {x*2.0, y*2.0};
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) * 0.709 + 0.5;
}
double noisePerlinGet3DValue(double x, double y, double z)
{
double vec[3] = {x*2.0, y*2.0, z*2.0};
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) * 0.661 + 0.5;
}
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)((rand() % (B + B)) - B) / B;
for (j = 0 ; j < 2 ; j++)
g2[i][j] = (double)((rand() % (B + B)) - B) / B;
_normalize2(g2[i]);
for (j = 0 ; j < 3 ; j++)
g3[i][j] = (double)((rand() % (B + B)) - B) / B;
_normalize3(g3[i]);
}
while (--i) {
k = p[i];
p[i] = p[j = rand() % 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];
}
}

View file

@ -1,20 +0,0 @@
#ifndef _PAYSAGES_NOISEPERLIN_H_
#define _PAYSAGES_NOISEPERLIN_H_
#include "rendering_global.h"
#ifdef __cplusplus
extern "C" {
#endif
RENDERINGSHARED_EXPORT void noisePerlinInit();
RENDERINGSHARED_EXPORT double noisePerlinGet1DValue(double x);
RENDERINGSHARED_EXPORT double noisePerlinGet2DValue(double x, double y);
RENDERINGSHARED_EXPORT double noisePerlinGet3DValue(double x, double y, double z);
/*double noiseSimplexGet4DValue(double x, double y, double z, double w);*/
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,20 +0,0 @@
#ifndef _PAYSAGES_NOISESIMPLEX_H_
#define _PAYSAGES_NOISESIMPLEX_H_
#include "rendering_global.h"
#ifdef __cplusplus
extern "C" {
#endif
RENDERINGSHARED_EXPORT void noiseSimplexInit();
RENDERINGSHARED_EXPORT double noiseSimplexGet1DValue(double x);
RENDERINGSHARED_EXPORT double noiseSimplexGet2DValue(double x, double y);
RENDERINGSHARED_EXPORT double noiseSimplexGet3DValue(double x, double y, double z);
RENDERINGSHARED_EXPORT double noiseSimplexGet4DValue(double x, double y, double z, double w);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -4,7 +4,6 @@
/* OpenCL usage */ /* OpenCL usage */
#include "rendering_global.h" #include "rendering_global.h"
#include "noise.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View file

@ -14,10 +14,6 @@ SOURCES += main.cpp \
renderer.cpp \ renderer.cpp \
render.cpp \ render.cpp \
opencl.cpp \ opencl.cpp \
noisesimplex.cpp \
noiseperlin.cpp \
noisenaive.cpp \
noise.cpp \
geoarea.cpp \ geoarea.cpp \
camera.cpp \ camera.cpp \
atmosphere/atm_render.cpp \ atmosphere/atm_render.cpp \
@ -68,10 +64,6 @@ HEADERS += \
renderer.h \ renderer.h \
render.h \ render.h \
opencl.h \ opencl.h \
noisesimplex.h \
noiseperlin.h \
noisenaive.h \
noise.h \
main.h \ main.h \
geoarea.h \ geoarea.h \
camera.h \ camera.h \

View file

@ -2,17 +2,7 @@
#define RENDERING_GLOBAL_H #define RENDERING_GLOBAL_H
/* Shared object helpers */ /* Shared object helpers */
#ifdef __cplusplus #include <QtCore/qglobal.h>
# include <QtCore/qglobal.h>
#else
# if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# define Q_DECL_EXPORT __declspec(dllexport)
# define Q_DECL_IMPORT __declspec(dllimport)
# else
# define Q_DECL_EXPORT
# define Q_DECL_IMPORT
# endif
#endif
#if defined(RENDERING_LIBRARY) #if defined(RENDERING_LIBRARY)
# define RENDERINGSHARED_EXPORT Q_DECL_EXPORT # define RENDERINGSHARED_EXPORT Q_DECL_EXPORT
#else #else
@ -20,20 +10,14 @@
#endif #endif
/* Namespace using */ /* Namespace using */
#ifdef __cplusplus
namespace paysages namespace paysages
{ {
namespace system {} namespace system {}
namespace basics {} namespace basics {}
namespace definition {} namespace definition {}
namespace rendering {}
} }
using namespace paysages::system; using namespace paysages::system;
using namespace paysages::basics; using namespace paysages::basics;
using namespace paysages::definition; using namespace paysages::definition;
using namespace paysages::rendering;
#endif
/* Global import */
#endif // RENDERING_GLOBAL_H #endif // RENDERING_GLOBAL_H

View file

@ -6,11 +6,12 @@
#include "../shared/types.h" #include "../shared/types.h"
#include "../tools/color.h" #include "../tools/color.h"
#include "../tools/euclid.h" #include "../tools/euclid.h"
#include "../noise.h"
#ifdef __cplusplus namespace paysages {
extern "C" { namespace basics {
#endif class NoiseGenerator;
}
}
typedef enum typedef enum
{ {
@ -91,8 +92,4 @@ RENDERINGSHARED_EXPORT void terrainBrushReset(TerrainHeightMap* heightmap, Terra
RENDERINGSHARED_EXPORT void terrainBrushFlatten(TerrainHeightMap* heightmap, TerrainBrush* brush, double height, double force); RENDERINGSHARED_EXPORT void terrainBrushFlatten(TerrainHeightMap* heightmap, TerrainBrush* brush, double height, double force);
RENDERINGSHARED_EXPORT void terrainEndBrushStroke(TerrainHeightMap* heightmap); RENDERINGSHARED_EXPORT void terrainEndBrushStroke(TerrainHeightMap* heightmap);
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -1,14 +1,13 @@
#include "private.h" #include "private.h"
#include <stdlib.h>
#include <math.h>
#include "../tools.h" #include "../tools.h"
#include "../renderer.h" #include "../renderer.h"
#include "NoiseGenerator.h"
/******************** Definition ********************/ /******************** Definition ********************/
static void _validateDefinition(TerrainDefinition* definition) static void _validateDefinition(TerrainDefinition* definition)
{ {
noiseValidate(definition->_height_noise); definition->_height_noise->validate();
if (definition->height < 1.0) if (definition->height < 1.0)
{ {
@ -16,7 +15,7 @@ static void _validateDefinition(TerrainDefinition* definition)
} }
/* Get minimal and maximal height */ /* Get minimal and maximal height */
noiseGetRange(definition->_height_noise, &definition->_min_height, &definition->_max_height); definition->_height_noise->getRange(&definition->_min_height, &definition->_max_height);
definition->_min_height *= definition->height * definition->scaling; definition->_min_height *= definition->height * definition->scaling;
definition->_max_height *= definition->height * definition->scaling; definition->_max_height *= definition->height * definition->scaling;
@ -35,7 +34,7 @@ static TerrainDefinition* _createDefinition()
definition->water_height = -0.3; definition->water_height = -0.3;
definition->_height_noise = noiseCreateGenerator(); definition->_height_noise = new NoiseGenerator();
terrainAutoPreset(definition, TERRAIN_PRESET_STANDARD); terrainAutoPreset(definition, TERRAIN_PRESET_STANDARD);
@ -45,7 +44,7 @@ static TerrainDefinition* _createDefinition()
static void _deleteDefinition(TerrainDefinition* definition) static void _deleteDefinition(TerrainDefinition* definition)
{ {
terrainHeightmapDelete(definition->height_map); terrainHeightmapDelete(definition->height_map);
noiseDeleteGenerator(definition->_height_noise); delete definition->_height_noise;
delete definition; delete definition;
} }
@ -59,7 +58,7 @@ static void _copyDefinition(TerrainDefinition* source, TerrainDefinition* destin
destination->water_height = source->water_height; destination->water_height = source->water_height;
noiseCopy(source->_height_noise, destination->_height_noise); source->_height_noise->copy(destination->_height_noise);
_validateDefinition(destination); _validateDefinition(destination);
} }
@ -71,7 +70,7 @@ static void _saveDefinition(PackStream* stream, TerrainDefinition* definition)
stream->write(&definition->shadow_smoothing); stream->write(&definition->shadow_smoothing);
terrainHeightmapSave(stream, definition->height_map); terrainHeightmapSave(stream, definition->height_map);
stream->write(&definition->water_height); stream->write(&definition->water_height);
noiseSaveGenerator(stream, definition->_height_noise); definition->_height_noise->save(stream);
} }
static void _loadDefinition(PackStream* stream, TerrainDefinition* definition) static void _loadDefinition(PackStream* stream, TerrainDefinition* definition)
@ -81,7 +80,7 @@ static void _loadDefinition(PackStream* stream, TerrainDefinition* definition)
stream->read(&definition->shadow_smoothing); stream->read(&definition->shadow_smoothing);
terrainHeightmapLoad(stream, definition->height_map); terrainHeightmapLoad(stream, definition->height_map);
stream->read(&definition->water_height); stream->read(&definition->water_height);
noiseLoadGenerator(stream, definition->_height_noise); definition->_height_noise->load(stream);
_validateDefinition(definition); _validateDefinition(definition);
} }
@ -102,7 +101,7 @@ double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int wit
if (!with_painting || !terrainHeightmapGetGridHeight(definition->height_map, x, z, &height)) if (!with_painting || !terrainHeightmapGetGridHeight(definition->height_map, x, z, &height))
{ {
height = noiseGet2DTotal(definition->_height_noise, (double)x, (double)z); height = definition->_height_noise->get2DTotal((double)x, (double)z);
} }
return height; return height;
@ -116,7 +115,7 @@ double terrainGetInterpolatedHeight(TerrainDefinition* definition, double x, dou
if (!with_painting || !terrainHeightmapGetInterpolatedHeight(definition->height_map, x, z, &height)) if (!with_painting || !terrainHeightmapGetInterpolatedHeight(definition->height_map, x, z, &height))
{ {
height = noiseGet2DTotal(definition->_height_noise, x, z); height = definition->_height_noise->get2DTotal(x, z);
} }
if (scaled) if (scaled)

View file

@ -12,6 +12,7 @@
#include "../tools.h" #include "../tools.h"
#include "../tools/array.h" #include "../tools/array.h"
#include "PackStream.h" #include "PackStream.h"
#include "NoiseGenerator.h"
typedef struct typedef struct
{ {
@ -556,11 +557,11 @@ void terrainBrushSmooth(TerrainHeightMap* heightmap, TerrainBrush* brush, double
_applyBrush(heightmap, brush, value, NULL, _applyBrushSmooth); _applyBrush(heightmap, brush, value, NULL, _applyBrushSmooth);
} }
static double _applyBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data) static double _applyBrushAddNoise(TerrainHeightMap*, TerrainBrush* brush, double x, double z, double basevalue, double influence, double force, void* data)
{ {
UNUSED(heightmap); NoiseGenerator* noise = (NoiseGenerator*)data;
return basevalue + noiseGet2DTotal((NoiseGenerator*)data, x / brush->total_radius, z / brush->total_radius) * influence * force * brush->total_radius; return basevalue + noise->get2DTotal(x / brush->total_radius, z / brush->total_radius) * influence * force * brush->total_radius;
} }
void terrainBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value) void terrainBrushAddNoise(TerrainHeightMap* heightmap, TerrainBrush* brush, NoiseGenerator* generator, double value)

View file

@ -1,6 +1,7 @@
#include "private.h" #include "private.h"
#include <math.h> #include <math.h>
#include "NoiseGenerator.h"
/* /*
* Terrain presets. * Terrain presets.
@ -12,12 +13,12 @@ void terrainAutoPreset(TerrainDefinition* definition, TerrainPreset preset)
switch (preset) switch (preset)
{ {
case TERRAIN_PRESET_STANDARD: case TERRAIN_PRESET_STANDARD:
noiseRandomizeOffsets(definition->_height_noise); definition->_height_noise->randomizeOffsets();
noiseClearLevels(definition->_height_noise); definition->_height_noise->clearLevels();
noiseAddLevelSimple(definition->_height_noise, pow(2.0, resolution + 1), -1.0, 1.0); definition->_height_noise->addLevelSimple(pow(2.0, resolution + 1), -1.0, 1.0);
noiseAddLevelsSimple(definition->_height_noise, resolution - 2, pow(2.0, resolution - 1), -0.7, 0.7, 0.5); definition->_height_noise->addLevelsSimple(resolution - 2, pow(2.0, resolution - 1), -0.7, 0.7, 0.5);
noiseNormalizeAmplitude(definition->_height_noise, -1.0, 1.0, 0); definition->_height_noise->normalizeAmplitude(-1.0, 1.0, 0);
noiseSetFunctionParams(definition->_height_noise, NOISE_FUNCTION_SIMPLEX, 0.0, 0.0); definition->_height_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, 0.0, 0.0);
definition->scaling = 1.0; definition->scaling = 1.0;
definition->height = 30.0; definition->height = 30.0;
definition->shadow_smoothing = 0.03; definition->shadow_smoothing = 0.03;

View file

@ -1,9 +1,9 @@
#include "public.h" #include "public.h"
#include <stdlib.h>
#include "../tools.h" #include "../tools.h"
#include "../tools/lighting.h" #include "../tools/lighting.h"
#include "../renderer.h" #include "../renderer.h"
#include "NoiseGenerator.h"
/* /*
* Terrain previews. * Terrain previews.
@ -67,7 +67,7 @@ Renderer* terrainCreatePreviewRenderer()
layer->material.reflection = 0.05; layer->material.reflection = 0.05;
layer->material.shininess = 2.0; layer->material.shininess = 2.0;
TexturesDefinitionClass.validate(textures); TexturesDefinitionClass.validate(textures);
noiseClearLevels(layer->_detail_noise); layer->_detail_noise->clearLevels();
TexturesRendererClass.bind(result, textures); TexturesRendererClass.bind(result, textures);

View file

@ -1,7 +1,7 @@
#include "private.h" #include "private.h"
#include "Scenery.h"
#include <stdlib.h> #include "Scenery.h"
#include "NoiseGenerator.h"
/******************** Global definition ********************/ /******************** Global definition ********************/
static void _validateDefinition(TexturesDefinition* definition) static void _validateDefinition(TexturesDefinition* definition)
@ -57,15 +57,15 @@ static void _layerValidateDefinition(TexturesLayerDefinition* definition)
definition->displacement_scaling = 0.000001; definition->displacement_scaling = 0.000001;
} }
noiseClearLevels(definition->_displacement_noise); definition->_displacement_noise->clearLevels();
noiseAddLevelsSimple(definition->_displacement_noise, 9, 1.0, -1.0, 1.0, 0.0); definition->_displacement_noise->addLevelsSimple(9, 1.0, -1.0, 1.0, 0.0);
noiseNormalizeAmplitude(definition->_displacement_noise, -1.0, 1.0, 0); definition->_displacement_noise->normalizeAmplitude(-1.0, 1.0, 0);
noiseValidate(definition->_displacement_noise); definition->_displacement_noise->validate();
noiseClearLevels(definition->_detail_noise); definition->_detail_noise->clearLevels();
noiseAddLevelsSimple(definition->_detail_noise, 7, 0.01, -1.0, 1.0, 0.0); definition->_detail_noise->addLevelsSimple(7, 0.01, -1.0, 1.0, 0.0);
noiseNormalizeAmplitude(definition->_detail_noise, -0.008, 0.008, 0); definition->_detail_noise->normalizeAmplitude(-0.008, 0.008, 0);
noiseValidate(definition->_detail_noise); definition->_detail_noise->validate();
materialValidate(&definition->material); materialValidate(&definition->material);
@ -85,8 +85,8 @@ static TexturesLayerDefinition* _layerCreateDefinition()
result = new TexturesLayerDefinition; result = new TexturesLayerDefinition;
result->terrain_zone = zoneCreate(); result->terrain_zone = zoneCreate();
result->_displacement_noise = noiseCreateGenerator(); result->_displacement_noise = new NoiseGenerator();
result->_detail_noise = noiseCreateGenerator(); result->_detail_noise = new NoiseGenerator();
texturesLayerAutoPreset(result, TEXTURES_LAYER_PRESET_ROCK); texturesLayerAutoPreset(result, TEXTURES_LAYER_PRESET_ROCK);
@ -98,8 +98,8 @@ static TexturesLayerDefinition* _layerCreateDefinition()
static void _layerDeleteDefinition(TexturesLayerDefinition* definition) static void _layerDeleteDefinition(TexturesLayerDefinition* definition)
{ {
zoneDelete(definition->terrain_zone); zoneDelete(definition->terrain_zone);
noiseDeleteGenerator(definition->_displacement_noise); delete definition->_displacement_noise;
noiseDeleteGenerator(definition->_detail_noise); delete definition->_detail_noise;
delete definition; delete definition;
} }
@ -111,8 +111,8 @@ static void _layerCopyDefinition(TexturesLayerDefinition* source, TexturesLayerD
destination->displacement_offset = source->displacement_offset; destination->displacement_offset = source->displacement_offset;
destination->material = source->material; destination->material = source->material;
noiseCopy(source->_displacement_noise, destination->_displacement_noise); source->_displacement_noise->copy(destination->_displacement_noise);
noiseCopy(source->_detail_noise, destination->_detail_noise); source->_detail_noise->copy(destination->_detail_noise);
} }
static void _layerSave(PackStream* stream, TexturesLayerDefinition* layer) static void _layerSave(PackStream* stream, TexturesLayerDefinition* layer)
@ -123,8 +123,8 @@ static void _layerSave(PackStream* stream, TexturesLayerDefinition* layer)
stream->write(&layer->displacement_offset); stream->write(&layer->displacement_offset);
materialSave(stream, &layer->material); materialSave(stream, &layer->material);
noiseSaveGenerator(stream, layer->_displacement_noise); layer->_displacement_noise->save(stream);
noiseSaveGenerator(stream, layer->_detail_noise); layer->_detail_noise->save(stream);
} }
static void _layerLoad(PackStream* stream, TexturesLayerDefinition* layer) static void _layerLoad(PackStream* stream, TexturesLayerDefinition* layer)
@ -135,8 +135,8 @@ static void _layerLoad(PackStream* stream, TexturesLayerDefinition* layer)
stream->read(&layer->displacement_offset); stream->read(&layer->displacement_offset);
materialLoad(stream, &layer->material); materialLoad(stream, &layer->material);
noiseLoadGenerator(stream, layer->_displacement_noise); layer->_displacement_noise->load(stream);
noiseLoadGenerator(stream, layer->_detail_noise); layer->_detail_noise->load(stream);
} }
LayerType texturesGetLayerType() LayerType texturesGetLayerType()

View file

@ -1,5 +1,7 @@
#include "private.h" #include "private.h"
#include "NoiseGenerator.h"
void texturesAutoPreset(TexturesDefinition* definition, TexturesPreset preset) void texturesAutoPreset(TexturesDefinition* definition, TexturesPreset preset)
{ {
TexturesLayerDefinition* layer; TexturesLayerDefinition* layer;
@ -55,8 +57,8 @@ void texturesAutoPreset(TexturesDefinition* definition, TexturesPreset preset)
void texturesLayerAutoPreset(TexturesLayerDefinition* definition, TexturesLayerPreset preset) void texturesLayerAutoPreset(TexturesLayerDefinition* definition, TexturesLayerPreset preset)
{ {
noiseRandomizeOffsets(definition->_displacement_noise); definition->_displacement_noise->randomizeOffsets();
noiseRandomizeOffsets(definition->_detail_noise); definition->_detail_noise->randomizeOffsets();
zoneClear(definition->terrain_zone); zoneClear(definition->terrain_zone);

View file

@ -1,7 +1,7 @@
#include "private.h" #include "private.h"
#include <assert.h> #include <cmath>
#include <math.h> #include "NoiseGenerator.h"
/* /*
* Get the base presence factor of a layer, not accounting for other layers. * Get the base presence factor of a layer, not accounting for other layers.
@ -16,11 +16,9 @@ double texturesGetLayerBasePresence(TexturesLayerDefinition* layer, TerrainResul
*/ */
double texturesGetTriplanarNoise(NoiseGenerator* noise, Vector3 location, Vector3 normal) double texturesGetTriplanarNoise(NoiseGenerator* noise, Vector3 location, Vector3 normal)
{ {
/*assert(v3Norm(normal) == 1.0);*/ double noiseXY = noise->get2DTotal(location.x, location.y);
double noiseXZ = noise->get2DTotal(location.x, location.z);
double noiseXY = noiseGet2DTotal(noise, location.x, location.y); double noiseYZ = noise->get2DTotal(location.y, location.z);
double noiseXZ = noiseGet2DTotal(noise, location.x, location.z);
double noiseYZ = noiseGet2DTotal(noise, location.y, location.z);
double mXY = fabs(normal.z); double mXY = fabs(normal.z);
double mXZ = fabs(normal.y); double mXZ = fabs(normal.y);

View file

@ -6,12 +6,12 @@
#include "../tools/lighting.h" #include "../tools/lighting.h"
#include "../tools/curve.h" #include "../tools/curve.h"
#include "../tools/euclid.h" #include "../tools/euclid.h"
#include "../noise.h"
#include "Layers.h"
#ifdef __cplusplus namespace paysages {
extern "C" { namespace basics {
#endif class NoiseGenerator;
}
}
typedef enum typedef enum
{ {
@ -75,8 +75,4 @@ RENDERINGSHARED_EXPORT Color waterGetPreviewCoverage(Renderer* renderer, double
RENDERINGSHARED_EXPORT Renderer* waterCreatePreviewColorRenderer(); RENDERINGSHARED_EXPORT Renderer* waterCreatePreviewColorRenderer();
RENDERINGSHARED_EXPORT Color waterGetPreviewColor(Renderer* renderer, double x, double y, double scaling); RENDERINGSHARED_EXPORT Color waterGetPreviewColor(Renderer* renderer, double x, double y, double scaling);
#ifdef __cplusplus
}
#endif
#endif #endif

View file

@ -1,22 +1,22 @@
#include "private.h" #include "private.h"
#include <stdlib.h>
#include "PackStream.h" #include "PackStream.h"
#include "NoiseGenerator.h"
static void _validateDefinition(WaterDefinition* definition) static void _validateDefinition(WaterDefinition* definition)
{ {
double scaling = definition->scaling * 0.3; double scaling = definition->scaling * 0.3;
noiseClearLevels(definition->_waves_noise); definition->_waves_noise->clearLevels();
if (definition->waves_height > 0.0) if (definition->waves_height > 0.0)
{ {
noiseAddLevelsSimple(definition->_waves_noise, 2, scaling, -definition->waves_height * scaling * 0.015, definition->waves_height * scaling * 0.015, 0.5); definition->_waves_noise->addLevelsSimple(2, scaling, -definition->waves_height * scaling * 0.015, definition->waves_height * scaling * 0.015, 0.5);
} }
if (definition->detail_height > 0.0) if (definition->detail_height > 0.0)
{ {
noiseAddLevelsSimple(definition->_waves_noise, 3, scaling * 0.1, -definition->detail_height * scaling * 0.015, definition->detail_height * scaling * 0.015, 0.5); definition->_waves_noise->addLevelsSimple(3, scaling * 0.1, -definition->detail_height * scaling * 0.015, definition->detail_height * scaling * 0.015, 0.5);
} }
noiseSetFunctionParams(definition->_waves_noise, NOISE_FUNCTION_SIMPLEX, -definition->turbulence, 0.0); definition->_waves_noise->setFunctionParams(NOISE_FUNCTION_SIMPLEX, -definition->turbulence, 0.0);
noiseValidate(definition->_waves_noise); definition->_waves_noise->validate();
materialValidate(&definition->material); materialValidate(&definition->material);
materialValidate(&definition->foam_material); materialValidate(&definition->foam_material);
@ -26,7 +26,7 @@ static WaterDefinition* _createDefinition()
{ {
WaterDefinition* definition = new WaterDefinition; WaterDefinition* definition = new WaterDefinition;
definition->_waves_noise = noiseCreateGenerator(); definition->_waves_noise = new NoiseGenerator();
waterAutoPreset(definition, WATER_PRESET_LAKE); waterAutoPreset(definition, WATER_PRESET_LAKE);
@ -35,7 +35,7 @@ static WaterDefinition* _createDefinition()
static void _deleteDefinition(WaterDefinition* definition) static void _deleteDefinition(WaterDefinition* definition)
{ {
noiseDeleteGenerator(definition->_waves_noise); delete definition->_waves_noise;
delete definition; delete definition;
} }
@ -46,7 +46,7 @@ static void _copyDefinition(WaterDefinition* source, WaterDefinition* destinatio
noise = destination->_waves_noise; noise = destination->_waves_noise;
*destination = *source; *destination = *source;
destination->_waves_noise = noise; destination->_waves_noise = noise;
noiseCopy(source->_waves_noise, destination->_waves_noise); source->_waves_noise->copy(destination->_waves_noise);
} }
static void _saveDefinition(PackStream* stream, WaterDefinition* definition) static void _saveDefinition(PackStream* stream, WaterDefinition* definition)
@ -66,7 +66,7 @@ static void _saveDefinition(PackStream* stream, WaterDefinition* definition)
stream->write(&definition->foam_coverage); stream->write(&definition->foam_coverage);
materialSave(stream, &definition->foam_material); materialSave(stream, &definition->foam_material);
noiseSaveGenerator(stream, definition->_waves_noise); definition->_waves_noise->save(stream);
} }
static void _loadDefinition(PackStream* stream, WaterDefinition* definition) static void _loadDefinition(PackStream* stream, WaterDefinition* definition)
@ -86,7 +86,7 @@ static void _loadDefinition(PackStream* stream, WaterDefinition* definition)
stream->read(&definition->foam_coverage); stream->read(&definition->foam_coverage);
materialLoad(stream, &definition->foam_material); materialLoad(stream, &definition->foam_material);
noiseLoadGenerator(stream, definition->_waves_noise); definition->_waves_noise->load(stream);
_validateDefinition(definition); _validateDefinition(definition);
} }

View file

@ -1,8 +1,10 @@
#include "private.h" #include "private.h"
#include "NoiseGenerator.h"
void waterAutoPreset(WaterDefinition* definition, WaterPreset preset) void waterAutoPreset(WaterDefinition* definition, WaterPreset preset)
{ {
noiseRandomizeOffsets(definition->_waves_noise); definition->_waves_noise->randomizeOffsets();
if (preset == WATER_PRESET_LAKE) if (preset == WATER_PRESET_LAKE)
{ {

View file

@ -1,9 +1,9 @@
#include "private.h" #include "private.h"
#include <stdlib.h> #include <cmath>
#include <math.h>
#include "../tools.h" #include "../tools.h"
#include "../renderer.h" #include "../renderer.h"
#include "NoiseGenerator.h"
static HeightInfo _FAKE_HEIGHT_INFO = {0.0, 0.0, 0.0}; static HeightInfo _FAKE_HEIGHT_INFO = {0.0, 0.0, 0.0};
@ -44,7 +44,7 @@ static WaterResult _fakeGetResult(Renderer* renderer, double x, double z)
/******************** Helpers ********************/ /******************** Helpers ********************/
static inline double _getHeight(WaterDefinition* definition, double base_height, double x, double z) static inline double _getHeight(WaterDefinition* definition, double base_height, double x, double z)
{ {
return base_height + noiseGet2DTotal(definition->_waves_noise, x, z); return base_height + definition->_waves_noise->get2DTotal(x, z);
} }
static inline Vector3 _getNormal(WaterDefinition* definition, double base_height, Vector3 base, double detail) static inline Vector3 _getNormal(WaterDefinition* definition, double base_height, Vector3 base, double detail)
@ -205,7 +205,7 @@ static HeightInfo _realGetHeightInfo(Renderer* renderer)
double noise_minvalue, noise_maxvalue; double noise_minvalue, noise_maxvalue;
info.base_height = renderer->terrain->getWaterHeight(renderer); info.base_height = renderer->terrain->getWaterHeight(renderer);
noiseGetRange(definition->_waves_noise, &noise_minvalue, &noise_maxvalue); definition->_waves_noise->getRange(&noise_minvalue, &noise_maxvalue);
info.min_height = info.base_height + noise_minvalue; info.min_height = info.base_height + noise_minvalue;
info.max_height = info.base_height + noise_maxvalue; info.max_height = info.base_height + noise_maxvalue;

View file

@ -0,0 +1,5 @@
#include "RandomGenerator.h"
RandomGenerator::RandomGenerator()
{
}

View file

@ -0,0 +1,24 @@
#ifndef RANDOMGENERATOR_H
#define RANDOMGENERATOR_H
#include "system_global.h"
#include <cstdlib>
namespace paysages {
namespace system {
class RandomGenerator
{
public:
RandomGenerator();
static inline double random()
{
return ((double)rand()) / (double)RAND_MAX;
}
};
}
}
#endif // RANDOMGENERATOR_H

View file

@ -17,7 +17,8 @@ SOURCES += \
Thread.cpp \ Thread.cpp \
Mutex.cpp \ Mutex.cpp \
System.cpp \ System.cpp \
PackStream.cpp PackStream.cpp \
RandomGenerator.cpp
HEADERS += \ HEADERS += \
system_global.h \ system_global.h \
@ -25,7 +26,8 @@ HEADERS += \
Thread.h \ Thread.h \
Mutex.h \ Mutex.h \
System.h \ System.h \
PackStream.h PackStream.h \
RandomGenerator.h
unix:!symbian { unix:!symbian {
maemo5 { maemo5 {