From f31df7521a9e59f7a1af3f923630900ad74eb1bc Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 21 May 2023 00:14:09 +0200 Subject: [PATCH] REVIEWED: `GenImagePerlinNoise()`, no change --- src/rtextures.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/rtextures.c b/src/rtextures.c index 6bca43d15..2d8056d64 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -833,18 +833,23 @@ Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float { for (int x = 0; x < width; x++) { - float nx = (float)(x + offsetX)*scale/(float)width; - float ny = (float)(y + offsetY)*scale/(float)height; - + float nx = (float)(x + offsetX)*(scale/(float)width); + float ny = (float)(y + offsetY)*(scale/(float)height); + + // Basic perlin noise implementation (not used) + //float p = (stb_perlin_noise3(nx, ny, 0.0f, 0, 0, 0); + + // Calculate a better perlin noise using fbm (fractal brownian motion) // Typical values to start playing with: // lacunarity = ~2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output) // gain = 0.5 -- relative weighting applied to each successive octave // octaves = 6 -- number of "octaves" of noise3() to sum + float p = stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6); + + // We need to normalize the data from [-1..1] to [0..1] + float np = (p + 1.0f)/2.0f; - // NOTE: We need to translate the data from [-1..1] to [0..1] - float p = (stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6) + 1.0f)/2.0f; - - int intensity = (int)(p*255.0f); + int intensity = (int)(np*255.0f); pixels[y*width + x] = (Color){ intensity, intensity, intensity, 255 }; } }