Skip to content

Commit

Permalink
Improve the API for the public-facing noise function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Rideout committed Aug 14, 2015
1 parent cc20479 commit 6413565
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
8 changes: 6 additions & 2 deletions include/heman.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ heman_image* heman_color_apply_gradient(heman_image* heightmap, float minheight,
// distance field to generate an interesting height map.
heman_image* heman_generate_island_heightmap(int width, int height, int seed);

// High-level function that computes several octaves of noise for demo purposes.
heman_image* heman_generate_island_noise(int width, int height, int seed);
// High-level function that sums up a number of noise octaves, also known as
// Fractional Brownian Motion. Taken alone, Perlin / Simplex noise are not
// fractals; this makes them more fractal-like. A good starting point is to use
// a lacunarity of 2.0 and a gain of 0.5, with only 2 or 3 octaves.
heman_image* heman_generate_simplex_fbm(int width, int height, float frequency,
float amplitude, int octaves, float lacunarity, float gain, int seed);

// Apply ambient occlusion and diffuse lighting to the given heightmap.
heman_image* heman_lighting_apply(heman_image* heightmap,
Expand Down
36 changes: 34 additions & 2 deletions src/generate.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "image.h"
#include "noise.h"
#include <math.h>
#include <memory.h>

static const float SEALEVEL = 0.5f;

#define NOISE(U, V) open_simplex_noise2(ctx, U, V)
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a > b ? b : a)

heman_image* heman_generate_island_noise(int width, int height, int seed)
static heman_image* generate_island_noise(int width, int height, int seed)
{
struct osn_context* ctx;
open_simplex_noise(seed, &ctx);
Expand Down Expand Up @@ -42,7 +43,7 @@ heman_image* heman_generate_island_noise(int width, int height, int seed)

heman_image* heman_generate_island_heightmap(int width, int height, int seed)
{
heman_image* noisetex = heman_generate_island_noise(width, height, seed);
heman_image* noisetex = generate_island_noise(width, height, seed);
heman_image* coastmask = heman_image_create(width, height, 1);
float* data = coastmask->data;
float invh = 1.0f / height;
Expand Down Expand Up @@ -100,3 +101,34 @@ heman_image* heman_generate_island_heightmap(int width, int height, int seed)
heman_image_destroy(heightmap);
return result;
}

heman_image* heman_generate_simplex_fbm(int width, int height, float frequency,
float amplitude, int octaves, float lacunarity, float gain, int seed)
{
struct osn_context* ctx;
open_simplex_noise(seed, &ctx);
heman_image* img = heman_image_create(width, height, 1);
float* data = img->data;
float invh = 1.0f / height;
float invw = 1.0f / width;
float ampl = amplitude;
float freq = frequency;
memset(data, 0, sizeof(float) * width * height);

while (octaves--) {
#pragma omp parallel for
for (int y = 0; y < height; ++y) {
float v = y * invh;
float* dst = data + y * width;
for (int x = 0; x < width; ++x) {
float u = x * invw;
*dst++ += ampl* NOISE(u * freq, v * freq);
}
}
ampl *= gain;
freq *= lacunarity;
}

open_simplex_noise_free(ctx);
return img;
}
10 changes: 9 additions & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ static void test_noise()
{
printf("Generating noise.\n");
double begin = omp_get_wtime();
heman_image* img = heman_generate_island_noise(SIZE, SIZE, 7000);
float frequency = 4;
float amplitude = 1;
int octaves = 10;
float lacunarity = 2;
float gain = 0.65f;
int seed = 5000;
heman_image* img = heman_generate_simplex_fbm(
SIZE, SIZE, frequency, amplitude, octaves, lacunarity, gain, seed);
double duration = omp_get_wtime() - begin;
printf("Noise generated in %.3f seconds.\n", duration);
write_image(OUTFOLDER "noise.png", img);
heman_image_destroy(img);
}

Expand Down

0 comments on commit 6413565

Please sign in to comment.