Browse Source

Use double-precision on all noise-related functions.

Miku AuahDark 2 years ago
parent
commit
d082e91435

+ 16 - 16
src/libraries/noise1234/noise1234.cpp

@@ -140,7 +140,7 @@ double  Noise1234::grad( int hash, double x, double y, double z, double t ) {
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 1D float Perlin noise, SL "noise()"
 /** 1D float Perlin noise, SL "noise()"
  */
  */
-float Noise1234::noise( double x )
+double Noise1234::noise( double x )
 {
 {
     int ix0, ix1;
     int ix0, ix1;
     double fx0, fx1;
     double fx0, fx1;
@@ -156,13 +156,13 @@ float Noise1234::noise( double x )
 
 
     n0 = grad( perm[ ix0 ], fx0 );
     n0 = grad( perm[ ix0 ], fx0 );
     n1 = grad( perm[ ix1 ], fx1 );
     n1 = grad( perm[ ix1 ], fx1 );
-    return 0.188f * ( LERP( s, n0, n1 ) );
+    return 0.188 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 1D float Perlin periodic noise, SL "pnoise()"
 /** 1D float Perlin periodic noise, SL "pnoise()"
  */
  */
-float Noise1234::pnoise( double x, int px )
+double Noise1234::pnoise( double x, int px )
 {
 {
     int ix0, ix1;
     int ix0, ix1;
     double fx0, fx1;
     double fx0, fx1;
@@ -178,14 +178,14 @@ float Noise1234::pnoise( double x, int px )
 
 
     n0 = grad( perm[ ix0 ], fx0 );
     n0 = grad( perm[ ix0 ], fx0 );
     n1 = grad( perm[ ix1 ], fx1 );
     n1 = grad( perm[ ix1 ], fx1 );
-    return 0.188f * ( LERP( s, n0, n1 ) );
+    return 0.188 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 2D float Perlin noise.
 /** 2D float Perlin noise.
  */
  */
-float Noise1234::noise( double x, double y )
+double Noise1234::noise( double x, double y )
 {
 {
     int ix0, iy0, ix1, iy1;
     int ix0, iy0, ix1, iy1;
     double fx0, fy0, fx1, fy1;
     double fx0, fy0, fx1, fy1;
@@ -213,13 +213,13 @@ float Noise1234::noise( double x, double y )
     nx1 = grad(perm[ix1 + perm[iy1]], fx1, fy1);
     nx1 = grad(perm[ix1 + perm[iy1]], fx1, fy1);
     n1 = LERP(t, nx0, nx1);
     n1 = LERP(t, nx0, nx1);
 
 
-    return 0.507f * ( LERP( s, n0, n1 ) );
+    return 0.507 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 2D float Perlin periodic noise.
 /** 2D float Perlin periodic noise.
  */
  */
-float Noise1234::pnoise( double x, double y, int px, int py )
+double Noise1234::pnoise( double x, double y, int px, int py )
 {
 {
     int ix0, iy0, ix1, iy1;
     int ix0, iy0, ix1, iy1;
     double fx0, fy0, fx1, fy1;
     double fx0, fy0, fx1, fy1;
@@ -247,14 +247,14 @@ float Noise1234::pnoise( double x, double y, int px, int py )
     nx1 = grad(perm[ix1 + perm[iy1]], fx1, fy1);
     nx1 = grad(perm[ix1 + perm[iy1]], fx1, fy1);
     n1 = LERP(t, nx0, nx1);
     n1 = LERP(t, nx0, nx1);
 
 
-    return 0.507f * ( LERP( s, n0, n1 ) );
+    return 0.507 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 3D float Perlin noise.
 /** 3D float Perlin noise.
  */
  */
-float Noise1234::noise( double x, double y, double z )
+double Noise1234::noise( double x, double y, double z )
 {
 {
     int ix0, iy0, ix1, iy1, iz0, iz1;
     int ix0, iy0, ix1, iy1, iz0, iz1;
     double fx0, fy0, fz0, fx1, fy1, fz1;
     double fx0, fy0, fz0, fx1, fy1, fz1;
@@ -301,13 +301,13 @@ float Noise1234::noise( double x, double y, double z )
 
 
     n1 = LERP( t, nx0, nx1 );
     n1 = LERP( t, nx0, nx1 );
 
 
-    return 0.936f * ( LERP( s, n0, n1 ) );
+    return 0.936 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 3D float Perlin periodic noise.
 /** 3D float Perlin periodic noise.
  */
  */
-float Noise1234::pnoise( double x, double y, double z, int px, int py, int pz )
+double Noise1234::pnoise( double x, double y, double z, int px, int py, int pz )
 {
 {
     int ix0, iy0, ix1, iy1, iz0, iz1;
     int ix0, iy0, ix1, iy1, iz0, iz1;
     double fx0, fy0, fz0, fx1, fy1, fz1;
     double fx0, fy0, fz0, fx1, fy1, fz1;
@@ -354,7 +354,7 @@ float Noise1234::pnoise( double x, double y, double z, int px, int py, int pz )
 
 
     n1 = LERP( t, nx0, nx1 );
     n1 = LERP( t, nx0, nx1 );
 
 
-    return 0.936f * ( LERP( s, n0, n1 ) );
+    return 0.936 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 
 
@@ -362,7 +362,7 @@ float Noise1234::pnoise( double x, double y, double z, int px, int py, int pz )
 /** 4D float Perlin noise.
 /** 4D float Perlin noise.
  */
  */
 
 
-float Noise1234::noise( double x, double y, double z, double w )
+double Noise1234::noise( double x, double y, double z, double w )
 {
 {
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
     double fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
     double fx0, fy0, fz0, fw0, fx1, fy1, fz1, fw1;
@@ -439,14 +439,14 @@ float Noise1234::noise( double x, double y, double z, double w )
 
 
     n1 = LERP( t, nx0, nx1 );
     n1 = LERP( t, nx0, nx1 );
 
 
-    return 0.87f * ( LERP( s, n0, n1 ) );
+    return 0.87 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
 /** 4D float Perlin periodic noise.
 /** 4D float Perlin periodic noise.
  */
  */
 
 
-float Noise1234::pnoise( double x, double y, double z, double w,
+double Noise1234::pnoise( double x, double y, double z, double w,
                             int px, int py, int pz, int pw )
                             int px, int py, int pz, int pw )
 {
 {
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
     int ix0, iy0, iz0, iw0, ix1, iy1, iz1, iw1;
@@ -524,7 +524,7 @@ float Noise1234::pnoise( double x, double y, double z, double w,
 
 
     n1 = LERP( t, nx0, nx1 );
     n1 = LERP( t, nx0, nx1 );
 
 
-    return 0.87f * ( LERP( s, n0, n1 ) );
+    return 0.87 * ( LERP( s, n0, n1 ) );
 }
 }
 
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------

+ 8 - 8
src/libraries/noise1234/noise1234.h

@@ -32,17 +32,17 @@ class Noise1234 {
 
 
 /** 1D, 2D, 3D and 4D float Perlin noise, SL "noise()"
 /** 1D, 2D, 3D and 4D float Perlin noise, SL "noise()"
  */
  */
-    static float noise( double x );
-    static float noise( double x, double y );
-    static float noise( double x, double y, double z );
-    static float noise( double x, double y, double z, double w );
+    static double noise( double x );
+    static double noise( double x, double y );
+    static double noise( double x, double y, double z );
+    static double noise( double x, double y, double z, double w );
 
 
 /** 1D, 2D, 3D and 4D float Perlin periodic noise, SL "pnoise()"
 /** 1D, 2D, 3D and 4D float Perlin periodic noise, SL "pnoise()"
  */
  */
-    static float pnoise( double x, int px );
-    static float pnoise( double x, double y, int px, int py );
-    static float pnoise( double x, double y, double z, int px, int py, int pz );
-    static float pnoise( double x, double y, double z, double w,
+    static double pnoise( double x, int px );
+    static double pnoise( double x, double y, int px, int py );
+    static double pnoise( double x, double y, double z, int px, int py, int pz );
+    static double pnoise( double x, double y, double z, double w,
                               int px, int py, int pz, int pw );
                               int px, int py, int pz, int pw );
 
 
   private:
   private:

+ 8 - 8
src/libraries/noise1234/simplexnoise1234.cpp

@@ -145,7 +145,7 @@ static unsigned char simplex[64][4] = {
     {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}};
     {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}};
 
 
 // 1D simplex noise
 // 1D simplex noise
-float SimplexNoise1234::noise(double x) {
+double SimplexNoise1234::noise(double x) {
 
 
   int i0 = FASTFLOOR(x);
   int i0 = FASTFLOOR(x);
   int i1 = i0 + 1;
   int i1 = i0 + 1;
@@ -163,12 +163,12 @@ float SimplexNoise1234::noise(double x) {
   n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
   n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
   // The maximum value of this noise is 8*(3/4)^4 = 2.53125
   // The maximum value of this noise is 8*(3/4)^4 = 2.53125
   // A factor of 0.395 will scale to fit exactly within [-1,1]
   // A factor of 0.395 will scale to fit exactly within [-1,1]
-  return 0.395f * (n0 + n1);
+  return 0.395 * (n0 + n1);
 
 
 }
 }
 
 
 // 2D simplex noise
 // 2D simplex noise
-float SimplexNoise1234::noise(double x, double y) {
+double SimplexNoise1234::noise(double x, double y) {
 
 
 #define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
 #define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
 #define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0
 #define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0
@@ -231,11 +231,11 @@ float SimplexNoise1234::noise(double x, double y) {
 
 
     // Add contributions from each corner to get the final noise value.
     // Add contributions from each corner to get the final noise value.
     // The result is scaled to return values in the interval [-1,1].
     // The result is scaled to return values in the interval [-1,1].
-    return 45.23f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
+    return 45.23 * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
   }
   }
 
 
 // 3D simplex noise
 // 3D simplex noise
-float SimplexNoise1234::noise(double x, double y, double z) {
+double SimplexNoise1234::noise(double x, double y, double z) {
 
 
     // Simple skewing factors for the 3D case
     // Simple skewing factors for the 3D case
 #define F3 0.333333333
 #define F3 0.333333333
@@ -329,12 +329,12 @@ float SimplexNoise1234::noise(double x, double y, double z) {
 
 
     // Add contributions from each corner to get the final noise value.
     // Add contributions from each corner to get the final noise value.
     // The result is scaled to stay just inside [-1,1]
     // The result is scaled to stay just inside [-1,1]
-    return 32.74f * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
+    return 32.74 * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
 }
 }
 
 
 
 
 // 4D simplex noise
 // 4D simplex noise
-float SimplexNoise1234::noise(double x, double y, double z, double w) {
+double SimplexNoise1234::noise(double x, double y, double z, double w) {
 
 
     // The skewing and unskewing factors are hairy again for the 4D case
     // The skewing and unskewing factors are hairy again for the 4D case
 #define F4 0.309016994 // F4 = (Math.sqrt(5.0)-1.0)/4.0
 #define F4 0.309016994 // F4 = (Math.sqrt(5.0)-1.0)/4.0
@@ -465,6 +465,6 @@ float SimplexNoise1234::noise(double x, double y, double z, double w) {
     }
     }
 
 
     // Sum up and scale the result to cover the range [-1,1]
     // Sum up and scale the result to cover the range [-1,1]
-    return 27.3f * (n0 + n1 + n2 + n3 + n4); // TODO: The scale factor is preliminary!
+    return 27.3 * (n0 + n1 + n2 + n3 + n4); // TODO: The scale factor is preliminary!
 }
 }
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------

+ 4 - 4
src/libraries/noise1234/simplexnoise1234.h

@@ -34,10 +34,10 @@ class SimplexNoise1234 {
 
 
 /** 1D and 2D float Perlin noise
 /** 1D and 2D float Perlin noise
  */
  */
-    static float noise( double x );
-    static float noise( double x, double y );
-    static float noise( double x, double y, double z );
-    static float noise( double x, double y, double z, double w);
+    static double noise( double x );
+    static double noise( double x, double y );
+    static double noise( double x, double y, double z );
+    static double noise( double x, double y, double z, double w);
 
 
   private:
   private:
     static unsigned char perm[];
     static unsigned char perm[];

+ 24 - 24
src/modules/math/MathModule.h

@@ -83,14 +83,14 @@ float linearToGamma(float c);
  *
  *
  * @return Noise value in the range of [0, 1].
  * @return Noise value in the range of [0, 1].
  **/
  **/
-static float simplexNoise1(double x);
-static float simplexNoise2(double x, double y);
-static float simplexNoise3(double x, double y, double z);
-static float simplexNoise4(double x, double y, double z, double w);
-static float perlinNoise1(double x);
-static float perlinNoise2(double x, double y);
-static float perlinNoise3(double x, double y, double z);
-static float perlinNoise4(double x, double y, double z, double w);
+static double simplexNoise1(double x);
+static double simplexNoise2(double x, double y);
+static double simplexNoise3(double x, double y, double z);
+static double simplexNoise4(double x, double y, double z, double w);
+static double perlinNoise1(double x);
+static double perlinNoise2(double x, double y);
+static double perlinNoise3(double x, double y, double z);
+static double perlinNoise4(double x, double y, double z, double w);
 
 
 
 
 class Math : public Module
 class Math : public Module
@@ -136,44 +136,44 @@ private:
 }; // Math
 }; // Math
 
 
 
 
-static inline float simplexNoise1(double x)
+static inline double simplexNoise1(double x)
 {
 {
-	return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
+	return SimplexNoise1234::noise(x) * 0.5 + 0.5;
 }
 }
 
 
-static inline float simplexNoise2(double x, double y)
+static inline double simplexNoise2(double x, double y)
 {
 {
-	return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
+	return SimplexNoise1234::noise(x, y) * 0.5 + 0.5;
 }
 }
 
 
-static inline float simplexNoise3(double x, double y, double z)
+static inline double simplexNoise3(double x, double y, double z)
 {
 {
-	return SimplexNoise1234::noise(x, y, z) * 0.5f + 0.5f;
+	return SimplexNoise1234::noise(x, y, z) * 0.5 + 0.5;
 }
 }
 
 
-static inline float simplexNoise4(double x, double y, double z, double w)
+static inline double simplexNoise4(double x, double y, double z, double w)
 {
 {
-	return SimplexNoise1234::noise(x, y, z, w) * 0.5f + 0.5f;
+	return SimplexNoise1234::noise(x, y, z, w) * 0.5 + 0.5;
 }
 }
 
 
-static inline float perlinNoise1(double x)
+static inline double perlinNoise1(double x)
 {
 {
-	return Noise1234::noise(x) * 0.5f + 0.5f;
+	return Noise1234::noise(x) * 0.5 + 0.5;
 }
 }
 
 
-static inline float perlinNoise2(double x, double y)
+static inline double perlinNoise2(double x, double y)
 {
 {
-	return Noise1234::noise(x, y) * 0.5f + 0.5f;
+	return Noise1234::noise(x, y) * 0.5 + 0.5;
 }
 }
 
 
-static inline float perlinNoise3(double x, double y, double z)
+static inline double perlinNoise3(double x, double y, double z)
 {
 {
-	return Noise1234::noise(x, y, z) * 0.5f + 0.5f;
+	return Noise1234::noise(x, y, z) * 0.5 + 0.5;
 }
 }
 
 
-static inline float perlinNoise4(double x, double y, double z, double w)
+static inline double perlinNoise4(double x, double y, double z, double w)
 {
 {
-	return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f;
+	return Noise1234::noise(x, y, z, w) * 0.5 + 0.5;
 }
 }
 
 
 } // math
 } // math

+ 11 - 11
src/modules/math/wrap_Math.cpp

@@ -326,7 +326,7 @@ int w_noise(lua_State *L)
 	for (int i = 0; i < nargs; i++)
 	for (int i = 0; i < nargs; i++)
 		args[i] = luaL_checknumber(L, i + 1);
 		args[i] = luaL_checknumber(L, i + 1);
 
 
-	float val = 0.0f;
+	double val = 0.0;
 
 
 	switch (nargs)
 	switch (nargs)
 	{
 	{
@@ -356,7 +356,7 @@ int w_perlinNoise(lua_State* L)
 	for (int i = 0; i < nargs; i++)
 	for (int i = 0; i < nargs; i++)
 		args[i] = luaL_checknumber(L, i + 1);
 		args[i] = luaL_checknumber(L, i + 1);
 
 
-	float val = 0.0f;
+	double val = 0.0;
 
 
 	switch (nargs)
 	switch (nargs)
 	{
 	{
@@ -386,7 +386,7 @@ int w_simplexNoise(lua_State* L)
 	for (int i = 0; i < nargs; i++)
 	for (int i = 0; i < nargs; i++)
 		args[i] = luaL_checknumber(L, i + 1);
 		args[i] = luaL_checknumber(L, i + 1);
 
 
-	float val = 0.0f;
+	double val = 0.0;
 
 
 	switch (nargs)
 	switch (nargs)
 	{
 	{
@@ -411,14 +411,14 @@ int w_simplexNoise(lua_State* L)
 // C functions in a struct, necessary for the FFI versions of math functions.
 // C functions in a struct, necessary for the FFI versions of math functions.
 struct FFI_Math
 struct FFI_Math
 {
 {
-	float (*snoise1)(double x);
-	float (*snoise2)(double x, double y);
-	float (*snoise3)(double x, double y, double z);
-	float (*snoise4)(double x, double y, double z, double w);
-	float (*pnoise1)(double x);
-	float (*pnoise2)(double x, double y);
-	float (*pnoise3)(double x, double y, double z);
-	float (*pnoise4)(double x, double y, double z, double w);
+	double (*snoise1)(double x);
+	double (*snoise2)(double x, double y);
+	double (*snoise3)(double x, double y, double z);
+	double (*snoise4)(double x, double y, double z, double w);
+	double (*pnoise1)(double x);
+	double (*pnoise2)(double x, double y);
+	double (*pnoise3)(double x, double y, double z);
+	double (*pnoise4)(double x, double y, double z, double w);
 
 
 	float (*gammaToLinear)(float c);
 	float (*gammaToLinear)(float c);
 	float (*linearToGamma)(float c);
 	float (*linearToGamma)(float c);

+ 9 - 9
src/modules/math/wrap_Math.lua

@@ -93,14 +93,14 @@ if not status then return end
 pcall(ffi.cdef, [[
 pcall(ffi.cdef, [[
 typedef struct FFI_Math
 typedef struct FFI_Math
 {
 {
-	float (*snoise1)(double x);
-	float (*snoise2)(double x, double y);
-	float (*snoise3)(double x, double y, double z);
-	float (*snoise4)(double x, double y, double z, double w);
-	float (*pnoise1)(double x);
-	float (*pnoise2)(double x, double y);
-	float (*pnoise3)(double x, double y, double z);
-	float (*pnoise4)(double x, double y, double z, double w);
+	double (*snoise1)(double x);
+	double (*snoise2)(double x, double y);
+	double (*snoise3)(double x, double y, double z);
+	double (*snoise4)(double x, double y, double z, double w);
+	double (*pnoise1)(double x);
+	double (*pnoise2)(double x, double y);
+	double (*pnoise3)(double x, double y, double z);
+	double (*pnoise4)(double x, double y, double z, double w);
 
 
 	float (*gammaToLinear)(float c);
 	float (*gammaToLinear)(float c);
 	float (*linearToGamma)(float c);
 	float (*linearToGamma)(float c);
@@ -108,7 +108,7 @@ typedef struct FFI_Math
 ]])
 ]])
 
 
 local ffifuncs = ffi.cast("FFI_Math **", ffifuncspointer_str)[0]
 local ffifuncs = ffi.cast("FFI_Math **", ffifuncspointer_str)[0]
-
+local love = require("love")
 
 
 -- Overwrite some regular love.math functions with FFI implementations.
 -- Overwrite some regular love.math functions with FFI implementations.