LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_perlin_noise.c File Reference

Functions

fnl_state fnlCreateState ()
 
float fnlGetNoise2D (fnl_state *state, FNLfloat x, FNLfloat y)
 
float fnlGetNoise3D (fnl_state *state, FNLfloat x, FNLfloat y, FNLfloat z)
 
void fnlDomainWarp2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
void fnlDomainWarp3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 
static float _fnlFastMin (float x, float y)
 
static float _fnlFastMax (float x, float y)
 
static float _fnlFastAbs (float f)
 
static float _fnlCasti32Tof32 (int i)
 
static int _fnlCastf32Toi32 (float f)
 
static float _fnlInvSqrt (float a)
 
static float _fnlFastSqrt (float a)
 
static int _fnlFastFloor (FNLfloat f)
 
static int _fnlFastRound (FNLfloat f)
 
static float _fnlLerp (float a, float b, float t)
 
static float _fnlInterpHermite (float t)
 
static float _fnlInterpQuintic (float t)
 
static float _fnlCubicLerp (float a, float b, float c, float d, float t)
 
static float _fnlPingPong (float t)
 
static float _fnlCalculateFractalBounding (fnl_state *state)
 
static int _fnlHash2D (int seed, int xPrimed, int yPrimed)
 
static int _fnlHash3D (int seed, int xPrimed, int yPrimed, int zPrimed)
 
static float _fnlValCoord2D (int seed, int xPrimed, int yPrimed)
 
static float _fnlValCoord3D (int seed, int xPrimed, int yPrimed, int zPrimed)
 
static float _fnlGradCoord2D (int seed, int xPrimed, int yPrimed, float xd, float yd)
 
static float _fnlGradCoord3D (int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd)
 
static void _fnlGradCoordOut2D (int seed, int xPrimed, int yPrimed, float *xo, float *yo)
 
static void _fnlGradCoordOut3D (int seed, int xPrimed, int yPrimed, int zPrimed, float *xo, float *yo, float *zo)
 
static void _fnlGradCoordDual2D (int seed, int xPrimed, int yPrimed, float xd, float yd, float *xo, float *yo)
 
static void _fnlGradCoordDual3D (int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd, float *xo, float *yo, float *zo)
 
static float _fnlSingleSimplex2D (int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSingleOpenSimplex23D (int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlSingleOpenSimplex2S2D (int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSingleOpenSimplex2S3D (int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlSingleCellular2D (fnl_state *state, int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSingleCellular3D (fnl_state *state, int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlSinglePerlin2D (int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSinglePerlin3D (int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlSingleValueCubic2D (int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSingleValueCubic3D (int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlSingleValue2D (int seed, FNLfloat x, FNLfloat y)
 
static float _fnlSingleValue3D (int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlGenNoiseSingle2D (fnl_state *state, int seed, FNLfloat x, FNLfloat y)
 
static float _fnlGenNoiseSingle3D (fnl_state *state, int seed, FNLfloat x, FNLfloat y, FNLfloat z)
 
static void _fnlTransformNoiseCoordinate2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
static void _fnlTransformNoiseCoordinate3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 
static void _fnlTransformDomainWarpCoordinate2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
static void _fnlTransformDomainWarpCoordinate3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 
static float _fnlGenFractalFBM2D (fnl_state *state, FNLfloat x, FNLfloat y)
 
static float _fnlGenFractalFBM3D (fnl_state *state, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlGenFractalRidged2D (fnl_state *state, FNLfloat x, FNLfloat y)
 
static float _fnlGenFractalRidged3D (fnl_state *state, FNLfloat x, FNLfloat y, FNLfloat z)
 
static float _fnlGenFractalPingPong2D (fnl_state *state, FNLfloat x, FNLfloat y)
 
static float _fnlGenFractalPingPong3D (fnl_state *state, FNLfloat x, FNLfloat y, FNLfloat z)
 
static void _fnlSingleDomainWarpBasicGrid2D (int seed, float warpAmp, float frequency, FNLfloat x, FNLfloat y, FNLfloat *xp, FNLfloat *yp)
 
static void _fnlSingleDomainWarpBasicGrid3D (int seed, float warpAmp, float frequency, FNLfloat x, FNLfloat y, FNLfloat z, FNLfloat *xp, FNLfloat *yp, FNLfloat *zp)
 
static void _fnlSingleDomainWarpSimplexGradient (int seed, float warpAmp, float frequency, FNLfloat x, FNLfloat y, FNLfloat *xr, FNLfloat *yr, bool outGradOnly)
 
static void _fnlSingleDomainWarpOpenSimplex2Gradient (int seed, float warpAmp, float frequency, FNLfloat x, FNLfloat y, FNLfloat z, FNLfloat *xr, FNLfloat *yr, FNLfloat *zr, bool outGradOnly)
 
static void _fnlDoSingleDomainWarp2D (fnl_state *state, int seed, float amp, float freq, FNLfloat x, FNLfloat y, FNLfloat *xp, FNLfloat *yp)
 
static void _fnlDoSingleDomainWarp3D (fnl_state *state, int seed, float amp, float freq, FNLfloat x, FNLfloat y, FNLfloat z, FNLfloat *xp, FNLfloat *yp, FNLfloat *zp)
 
static void _fnlDomainWarpSingle2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
static void _fnlDomainWarpSingle3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 
static void _fnlDomainWarpFractalProgressive2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
static void _fnlDomainWarpFractalProgressive3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 
static void _fnlDomainWarpFractalIndependent2D (fnl_state *state, FNLfloat *x, FNLfloat *y)
 
static void _fnlDomainWarpFractalIndependent3D (fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
 

Variables

static const float GRADIENTS_2D []
 
static const float RAND_VECS_2D []
 
static const float GRADIENTS_3D []
 
static const float RAND_VECS_3D []
 
static const int PRIME_X = 501125321
 
static const int PRIME_Y = 1136930381
 
static const int PRIME_Z = 1720413743
 

Detailed Description

=== Perlin noise funcs ===

  • Range = [-sqrt(n / 4), sqrt(n / 4)] with n = dim
  • n = 2 -> [-0.7, 0.7]
  • Often and typically normalized to [-1.0, 1.0] in implementations

Now using FastNoiseLite lib by Jordan Peck - seems pretty good

NOTE: FOLLOWING CODE IS COPYRIGHT(C) 2023 JORDAN PECK

=== Fast Noise Lite ===

Function Documentation

◆ fnlCreateState()

fnl_state fnlCreateState ( )

Creates a noise state with default values.

Parameters
seedOptionally set the state seed.

◆ fnlGetNoise2D()

float fnlGetNoise2D ( fnl_state state,
FNLfloat  x,
FNLfloat  y 
)

2D noise at given position using the state settings

Returns
Noise output bounded between -1 and 1.

◆ fnlGetNoise3D()

float fnlGetNoise3D ( fnl_state state,
FNLfloat  x,
FNLfloat  y,
FNLfloat  z 
)

3D noise at given position using the state settings

Returns
Noise output bounded between -1 and 1.

◆ fnlDomainWarp2D()

void fnlDomainWarp2D ( fnl_state state,
FNLfloat *  x,
FNLfloat *  y 
)

2D warps the input position using current domain warp settings.

Example usage with fnlGetNoise2D:

fnlDomainWarp2D(&state, &x, &y);
noise = fnlGetNoise2D(&state, x, y);
void fnlDomainWarp2D(fnl_state *state, FNLfloat *x, FNLfloat *y)
Definition lg_perlin_noise.c:2121
float fnlGetNoise2D(fnl_state *state, FNLfloat x, FNLfloat y)
Definition lg_perlin_noise.c:2088

◆ fnlDomainWarp3D()

void fnlDomainWarp3D ( fnl_state state,
FNLfloat *  x,
FNLfloat *  y,
FNLfloat *  z 
)

3D warps the input position using current domain warp settings.

Example usage with fnlGetNoise3D:

fnlDomainWarp3D(&state, &x, &y, &z);
noise = fnlGetNoise3D(&state, x, y, z);
void fnlDomainWarp3D(fnl_state *state, FNLfloat *x, FNLfloat *y, FNLfloat *z)
Definition lg_perlin_noise.c:2136
float fnlGetNoise3D(fnl_state *state, FNLfloat x, FNLfloat y, FNLfloat z)
Definition lg_perlin_noise.c:2104

Variable Documentation

◆ GRADIENTS_3D

const float GRADIENTS_3D[]
static
Initial value:
=
{
0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
1, 1, 0, 0, 0,-1, 1, 0, -1, 1, 0, 0, 0,-1,-1, 0
}