Added love.math.noise(x, [y, z, w]). Calculates the 1D, 2D, 3D or 4D Simplex noise value at a particular coordinate

This commit is contained in:
Alex Szpakowski
2013-05-01 18:26:40 -07:00
parent 1edfbb766f
commit 9ccf013570
6 changed files with 618 additions and 0 deletions
+34
View File
@@ -27,6 +27,10 @@
#include "common/Module.h"
#include "common/math.h"
#include "common/int.h"
#include "common/StringMap.h"
// Noise
#include "libraries/noise1234/simplexnoise1234.h"
// STL
#include <limits>
@@ -106,6 +110,16 @@ public:
**/
std::vector<Triangle> triangulate(const std::vector<vertex> &polygon);
/**
* Calculate Simplex noise for the specified coordinate(s).
*
* @return Noise value in the range of [0,1].
**/
float simplexNoise1(float x) const;
float simplexNoise2(float x, float y) const;
float simplexNoise3(float x, float y, float z) const;
float simplexNoise4(float x, float y, float z, float w) const;
static Math instance;
private:
@@ -114,6 +128,26 @@ private:
}; // Math
inline float Math::simplexNoise1(float x) const
{
return SimplexNoise1234::noise(x);
}
inline float Math::simplexNoise2(float x, float y) const
{
return SimplexNoise1234::noise(x, y);
}
inline float Math::simplexNoise3(float x, float y, float z) const
{
return SimplexNoise1234::noise(x, y, z);
}
inline float Math::simplexNoise4(float x, float y, float z, float w) const
{
return SimplexNoise1234::noise(x, y, z, w);
}
} // math
} // love
+37
View File
@@ -148,6 +148,42 @@ int w_triangulate(lua_State *L)
return 1;
}
int w_noise(lua_State *L)
{
float w, x, y, z;
float val;
switch (lua_gettop(L))
{
case 1:
x = luaL_checknumber(L, 1);
val = Math::instance.simplexNoise1(x);
break;
case 2:
x = luaL_checknumber(L, 1);
y = luaL_checknumber(L, 2);
val = Math::instance.simplexNoise2(x, y);
break;
case 3:
x = luaL_checknumber(L, 1);
y = luaL_checknumber(L, 2);
z = luaL_checknumber(L, 3);
val = Math::instance.simplexNoise3(x, y, z);
break;
case 4:
default:
x = luaL_checknumber(L, 1);
y = luaL_checknumber(L, 2);
z = luaL_checknumber(L, 3);
w = luaL_checknumber(L, 4);
val = Math::instance.simplexNoise4(x, y, z, w);
break;
}
lua_pushnumber(L, (lua_Number) val);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
@@ -156,6 +192,7 @@ static const luaL_Reg functions[] =
{ "randomnormal", w_randomnormal },
{ "newRandomGenerator", w_newRandomGenerator },
{ "triangulate", w_triangulate },
{ "noise", w_noise },
{ 0, 0 }
};
+1
View File
@@ -35,6 +35,7 @@ int w_random(lua_State *L);
int w_randomnormal(lua_State *L);
int w_newRandomGenerator(lua_State *L);
int w_triangulate(lua_State *L);
int w_noise(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_math(lua_State *L);
} // random