[Change] Moving things around a litting in perling.* I made a little mess.

This commit is contained in:
Allanis 2013-07-11 16:16:38 +01:00
parent 2d1040b703
commit 4fd8308706
2 changed files with 44 additions and 40 deletions

View File

@ -7,10 +7,18 @@
#include "perlin.h" #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 LERP(a, b, x) (a + x * (b - a))
#define ABS(a) ((a)<0?-(a):(a)) #define ABS(a) ((a)<0?-(a):(a))
#define CLAMP(a, b, x) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) #define CLAMP(a, b, x) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
typedef void* noise_t;
/* Used internally. */
typedef struct { typedef struct {
int ndim; int ndim;
unsigned char map[256]; /* Randomized map of indexes into buffer. */ unsigned char map[256]; /* Randomized map of indexes into buffer. */
@ -21,7 +29,15 @@ typedef struct {
float exponent[NOISE_MAX_OCTAVES]; float exponent[NOISE_MAX_OCTAVES];
} perlin_data_t; } 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, static float lattice(perlin_data_t* data, int ix, float fx, int iy,
float fy, int iz, float fz, int iw, float fw) { 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; 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); perlin_data_t* data=(perlin_data_t*)calloc(sizeof(perlin_data_t), 1);
int i, j; int i, j;
unsigned char tmp; unsigned char tmp;
@ -85,7 +101,7 @@ noise_t noise_new(int ndim, float hurst, float lacunarity) {
return (noise_t)data; 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; perlin_data_t* data = (perlin_data_t*) noise;
int n[NOISE_MAX_DIMENSIONS]; /* Indexes to pass to lattice function */ 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); return CLAMP(-0.99999f, 0.99999f, value);
} }
#if 0
float noise_fbm(noise_t noise, float* f, float octaves) { float noise_fbm(noise_t noise, float* f, float octaves) {
float tf[NOISE_MAX_DIMENSIONS]; float tf[NOISE_MAX_DIMENSIONS];
perlin_data_t* data = (perlin_data_t*) noise; 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]; value += octaves * noise_get(noise, tf) * data->exponent[i];
return CLAMP(-0.99999f, 0.99999f, value); 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]; float tf[NOISE_MAX_DIMENSIONS];
perlin_data_t* data = (perlin_data_t*) noise; perlin_data_t* data = (perlin_data_t*) noise;
/* Init locals. */ /* Init locals. */
@ -216,14 +234,14 @@ void noise_delete(noise_t noise) {
free((perlin_data_t*)noise); free((perlin_data_t*)noise);
} }
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) {
int x, y; int x, y, z;
float f[2]; float f[3];
float octaves; float octaves;
float hurst; float hurst;
float lacunarity; float lacunarity;
noise_t noise; noise_t noise;
float* map; float* nebulae;;
float value; float value;
octaves = 3.; octaves = 3.;
@ -232,25 +250,31 @@ static float* noise_genMap(const int w, const int h, float rug) {
noise = noise_new(2, hurst, lacunarity); 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(z = 0; z < n; z++) {
for(y = 0; y < h; y++) { for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) { for(x = 0; x < w; x++) {
f[0] = rug * (float)x / (float)w; f[0] = rug * (float)x / (float)w;
f[1] = rug * (float)y / (float)h; 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; value = value + 0.3;
map[y*w+x] = (value < 1.) ? value : 1.; nebulae[z*w*h + y*w+x] = (value < 1.) ? value : 1.;
}
} }
} }
noise_delete(noise); noise_delete(noise);
return map; return nebulae;
} }
glTexture* noise_genCloud(const int w, const int h, double rug) { 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; glTexture* tex;
double c; double c;
map = noise_genMap(w, h, rug); map = noise_genNebulae(w, h, 1, rug);
sur = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RGBMASK); sur = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RGBMASK);
pix = sur->pixels; pix = sur->pixels;

View File

@ -1,25 +1,5 @@
#pragma once #pragma once
#include "opengl.h" #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); glTexture* noise_genCloud(const int w, const int h, double rug);