From 4fd83087060230361e9174a6f560ab6011f50c17 Mon Sep 17 00:00:00 2001 From: Allanis Date: Thu, 11 Jul 2013 16:16:38 +0100 Subject: [PATCH] [Change] Moving things around a litting in perling.* I made a little mess. --- src/perlin.c | 64 ++++++++++++++++++++++++++++++++++++---------------- src/perlin.h | 20 ---------------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/perlin.c b/src/perlin.c index 7b26b9d..7955e9d 100644 --- a/src/perlin.c +++ b/src/perlin.c @@ -7,10 +7,18 @@ #include "perlin.h" +#define NOISE_MAX_OCTAVES 128 +#define NOISE_MAX_DIMENSIONS 4 +#define NOISE_DEFAULT_HURST 0.5 +#define NOISE_DEFAULT_LACUNARITY 2. + #define LERP(a, b, x) (a + x * (b - a)) #define ABS(a) ((a)<0?-(a):(a)) #define CLAMP(a, b, x) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) +typedef void* noise_t; + +/* Used internally. */ typedef struct { int ndim; unsigned char map[256]; /* Randomized map of indexes into buffer. */ @@ -21,7 +29,15 @@ typedef struct { float exponent[NOISE_MAX_OCTAVES]; } perlin_data_t; -static float* noise_genMap(const int w, const int h, float rug); +static float* noise_genNebulae(const int w, const int h, const int n, float rug); +static noise_t noise_new(int dimensions, float hurst, float lacunarity); +/* Basic perlin noise. */ +static float noise_get(noise_t noise, float* f); +/* Fractional brownian motion. */ +/*static float noise_fbm(noise_t noise, float* f, float octaves);*/ +/* Turbulence. */ +static float noise_turbulence(noise_t noise, float* f, float octaves); +static void noise_delete(noise_t noise); static float lattice(perlin_data_t* data, int ix, float fx, int iy, float fy, int iz, float fz, int iw, float fw) { @@ -57,7 +73,7 @@ static void normalize(perlin_data_t* data, float* f) { f[i] *= magnitude; } -noise_t noise_new(int ndim, float hurst, float lacunarity) { +static noise_t noise_new(int ndim, float hurst, float lacunarity) { perlin_data_t* data=(perlin_data_t*)calloc(sizeof(perlin_data_t), 1); int i, j; unsigned char tmp; @@ -85,7 +101,7 @@ noise_t noise_new(int ndim, float hurst, float lacunarity) { return (noise_t)data; } -float noise_get(noise_t noise, float *f ) +static float noise_get(noise_t noise, float *f ) { perlin_data_t* data = (perlin_data_t*) noise; int n[NOISE_MAX_DIMENSIONS]; /* Indexes to pass to lattice function */ @@ -170,6 +186,7 @@ float noise_get(noise_t noise, float *f ) return CLAMP(-0.99999f, 0.99999f, value); } +#if 0 float noise_fbm(noise_t noise, float* f, float octaves) { float tf[NOISE_MAX_DIMENSIONS]; perlin_data_t* data = (perlin_data_t*) noise; @@ -190,8 +207,9 @@ float noise_fbm(noise_t noise, float* f, float octaves) { value += octaves * noise_get(noise, tf) * data->exponent[i]; return CLAMP(-0.99999f, 0.99999f, value); } +#endif -float noise_turbulence(noise_t noise, float* f, float octaves) { +static float noise_turbulence(noise_t noise, float* f, float octaves) { float tf[NOISE_MAX_DIMENSIONS]; perlin_data_t* data = (perlin_data_t*) noise; /* Init locals. */ @@ -216,14 +234,14 @@ void noise_delete(noise_t noise) { free((perlin_data_t*)noise); } -static float* noise_genMap(const int w, const int h, float rug) { - int x, y; - float f[2]; +static float* noise_genNebulae(const int w, const int h, const int n, float rug) { + int x, y, z; + float f[3]; float octaves; float hurst; float lacunarity; noise_t noise; - float* map; + float* nebulae;; float value; octaves = 3.; @@ -232,25 +250,31 @@ static float* noise_genMap(const int w, const int h, float rug) { noise = noise_new(2, hurst, lacunarity); - map = malloc(sizeof(float)*w*h); + nebulae = malloc(sizeof(float)*w*h*n); + if(nebulae == NULL) { + WARN("Out of memory!"); + return NULL; + } - for(y = 0; y < h; y++) { - for(x = 0; x < w; x++) { - f[0] = rug * (float)x / (float)w; - f[1] = rug * (float)y / (float)h; + for(z = 0; z < n; z++) { + for(y = 0; y < h; y++) { + for(x = 0; x < w; x++) { + + f[0] = rug * (float)x / (float)w; + f[1] = rug * (float)y / (float)h; + f[2] = rug * (float)z / (float)n; - /*value = noise_get(noise, f);*/ - /*value = noise_fbm(noise, f, octaves);*/ - value = noise_turbulence(noise, f, octaves); + value = noise_turbulence(noise, f, octaves); - value = value + 0.3; - map[y*w+x] = (value < 1.) ? value : 1.; + value = value + 0.3; + nebulae[z*w*h + y*w+x] = (value < 1.) ? value : 1.; + } } } noise_delete(noise); - return map; + return nebulae; } glTexture* noise_genCloud(const int w, const int h, double rug) { @@ -261,7 +285,7 @@ glTexture* noise_genCloud(const int w, const int h, double rug) { glTexture* tex; double c; - map = noise_genMap(w, h, rug); + map = noise_genNebulae(w, h, 1, rug); sur = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RGBMASK); pix = sur->pixels; diff --git a/src/perlin.h b/src/perlin.h index ba7748e..780d6f0 100644 --- a/src/perlin.h +++ b/src/perlin.h @@ -1,25 +1,5 @@ #pragma once #include "opengl.h" -typedef void* noise_t; - -#define NOISE_MAX_OCTAVES 128 -#define NOISE_MAX_DIMENSIONS 4 -#define NOISE_DEFAULT_HURST 0.5f -#define NOISE_DEFAULT_LACUNARITY 2.0f - -noise_t noise_new(int dimensions, float hurst, float lacunarity); - -/* Basic perlin noise. */ -float noise_get(noise_t noise, float *f); - -/* Fractional brownian motion. */ -float noise_fbm(noise_t noise, float* f, float octaves); - -/* Turbulence. */ -float noise_turbulence(noise_t noise, float* f, float octaves); - -void noise_delete(noise_t noise); - glTexture* noise_genCloud(const int w, const int h, double rug);