Remove Lua and Noise modules

This commit is contained in:
Lynix
2020-02-24 17:52:06 +01:00
parent 7c1857ba1e
commit eb8800f812
84 changed files with 4 additions and 10261 deletions

View File

@@ -1,31 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/Config.hpp>
#if NAZARA_LUA_MANAGE_MEMORY
#include <Nazara/Core/MemoryManager.hpp>
#include <new> // Nécessaire ?
void* operator new(std::size_t size)
{
return Nz::MemoryManager::Allocate(size, false);
}
void* operator new[](std::size_t size)
{
return Nz::MemoryManager::Allocate(size, true);
}
void operator delete(void* pointer) noexcept
{
Nz::MemoryManager::Free(pointer, false);
}
void operator delete[](void* pointer) noexcept
{
Nz::MemoryManager::Free(pointer, true);
}
#endif // NAZARA_LUA_MANAGE_MEMORY

View File

@@ -1,63 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/Lua.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Lua/Config.hpp>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
{
bool Lua::Initialize()
{
if (s_moduleReferenceCounter > 0)
{
s_moduleReferenceCounter++;
return true; // Déjà initialisé
}
// Initialisation des dépendances
if (!Core::Initialize())
{
NazaraError("Failed to initialize core module");
return false;
}
s_moduleReferenceCounter++;
// Initialisation du module
NazaraNotice("Initialized: Lua module");
return true;
}
bool Lua::IsInitialized()
{
return s_moduleReferenceCounter != 0;
}
void Lua::Uninitialize()
{
if (s_moduleReferenceCounter != 1)
{
// Le module est soit encore utilisé, soit pas initialisé
if (s_moduleReferenceCounter > 1)
s_moduleReferenceCounter--;
return;
}
// Libération du module
s_moduleReferenceCounter = 0;
NazaraNotice("Uninitialized: Lua module");
// Libération des dépendances
Core::Uninitialize();
}
unsigned int Lua::s_moduleReferenceCounter = 0;
}

View File

@@ -1,54 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaCoroutine.hpp>
#include <Lua/lua.h>
#include <Nazara/Lua/LuaInstance.hpp>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
{
LuaCoroutine::LuaCoroutine(lua_State* internalState, int refIndex) :
LuaState(internalState),
m_ref(refIndex)
{
}
LuaCoroutine::~LuaCoroutine()
{
if (m_ref >= 0)
DestroyReference(m_ref);
}
bool LuaCoroutine::CanResume() const
{
return lua_status(m_state) == LUA_YIELD;
}
Ternary LuaCoroutine::Resume(unsigned int argCount)
{
LuaInstance& instance = GetInstance(m_state);
if (instance.m_level++ == 0)
instance.m_clock.Restart();
int status = lua_resume(m_state, nullptr, argCount);
instance.m_level--;
if (status == LUA_OK)
return Ternary_True;
else if (status == LUA_YIELD)
return Ternary_Unknown;
else
{
m_lastError = ToString(-1);
Pop();
return Ternary_False;
}
}
bool LuaCoroutine::Run(int argCount, int /*resultCount*/, int /*errHandler*/)
{
return Resume(argCount) != Ternary_False;
}
}

View File

@@ -1,167 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaInstance.hpp>
#include <Lua/lauxlib.h>
#include <Lua/lua.h>
#include <Lua/lualib.h>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Error.hpp>
#include <array>
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
{
namespace
{
int AtPanic(lua_State* internalState)
{
String lastError(lua_tostring(internalState, -1));
throw std::runtime_error("Lua panic: " + lastError.ToStdString());
}
}
LuaInstance::LuaInstance() :
LuaState(nullptr),
m_memoryLimit(0),
m_memoryUsage(0),
m_timeLimit(1000),
m_level(0)
{
m_state = lua_newstate(MemoryAllocator, this);
lua_atpanic(m_state, AtPanic);
lua_sethook(m_state, TimeLimiter, LUA_MASKCOUNT, 1000);
}
LuaInstance::LuaInstance(LuaInstance&& instance) :
LuaState(std::move(instance))
{
std::swap(m_memoryLimit, instance.m_memoryLimit);
std::swap(m_memoryUsage, instance.m_memoryUsage);
std::swap(m_timeLimit, instance.m_timeLimit);
std::swap(m_clock, instance.m_clock);
std::swap(m_level, instance.m_level);
if (m_state)
lua_setallocf(m_state, MemoryAllocator, this);
if (instance.m_state)
lua_setallocf(instance.m_state, MemoryAllocator, &instance);
}
LuaInstance::~LuaInstance()
{
if (m_state)
lua_close(m_state);
}
void LuaInstance::LoadLibraries(LuaLibFlags libFlags)
{
// From luaL_openlibs
std::array<luaL_Reg, LuaLib_Max + 1 + 1> libs;
std::size_t libCount = 0;
libs[libCount++] = { "_G", luaopen_base };
if (libFlags & LuaLib_Coroutine)
libs[libCount++] = { LUA_COLIBNAME, luaopen_coroutine };
if (libFlags & LuaLib_Debug)
libs[libCount++] = { LUA_DBLIBNAME, luaopen_debug };
if (libFlags & LuaLib_Io)
libs[libCount++] = { LUA_IOLIBNAME, luaopen_io };
if (libFlags & LuaLib_Math)
libs[libCount++] = { LUA_MATHLIBNAME, luaopen_math };
if (libFlags & LuaLib_Os)
libs[libCount++] = { LUA_OSLIBNAME, luaopen_os };
if (libFlags & LuaLib_Package)
libs[libCount++] = { LUA_LOADLIBNAME, luaopen_package };
if (libFlags & LuaLib_String)
libs[libCount++] = { LUA_STRLIBNAME, luaopen_string };
if (libFlags & LuaLib_Table)
libs[libCount++] = { LUA_TABLIBNAME, luaopen_table };
if (libFlags & LuaLib_Utf8)
libs[libCount++] = { LUA_UTF8LIBNAME, luaopen_utf8 };
for (std::size_t i = 0; i < libCount; ++i)
{
luaL_requiref(m_state, libs[i].name, libs[i].func, 1);
lua_pop(m_state, 1); /* remove lib */
}
}
LuaInstance& LuaInstance::operator=(LuaInstance&& instance)
{
LuaState::operator=(std::move(instance));
std::swap(m_memoryLimit, instance.m_memoryLimit);
std::swap(m_memoryUsage, instance.m_memoryUsage);
std::swap(m_timeLimit, instance.m_timeLimit);
std::swap(m_clock, instance.m_clock);
std::swap(m_level, instance.m_level);
if (m_state)
lua_setallocf(m_state, MemoryAllocator, this);
if (instance.m_state)
lua_setallocf(instance.m_state, MemoryAllocator, &instance);
return *this;
}
void* LuaInstance::MemoryAllocator(void* ud, void* ptr, std::size_t osize, std::size_t nsize)
{
LuaInstance* instance = static_cast<LuaInstance*>(ud);
std::size_t memoryLimit = instance->GetMemoryLimit();
std::size_t memoryUsage = instance->GetMemoryUsage();
if (nsize == 0)
{
assert(memoryUsage >= osize);
instance->SetMemoryUsage(memoryUsage - osize);
std::free(ptr);
return nullptr;
}
else
{
std::size_t usage = memoryUsage + nsize;
if (ptr)
usage -= osize;
if (memoryLimit != 0 && usage > memoryLimit)
{
NazaraError("Lua memory usage is over memory limit (" + String::Number(usage) + " > " + String::Number(memoryLimit) + ')');
return nullptr;
}
instance->SetMemoryUsage(usage);
return std::realloc(ptr, nsize);
}
}
void LuaInstance::TimeLimiter(lua_State* internalState, lua_Debug* debug)
{
NazaraUnused(debug);
LuaInstance* instance;
lua_getallocf(internalState, reinterpret_cast<void**>(&instance));
if (instance->m_clock.GetMilliseconds() > instance->GetTimeLimit())
luaL_error(internalState, "maximum execution time exceeded");
}
}

View File

@@ -1,875 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Lua scripting module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Lua/LuaState.hpp>
#include <Lua/lauxlib.h>
#include <Lua/lua.h>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/MemoryHelper.hpp>
#include <Nazara/Core/MemoryView.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Lua/LuaCoroutine.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <Nazara/Lua/Debug.hpp>
namespace Nz
{
namespace
{
LuaType FromLuaType(int type)
{
switch (type)
{
case LUA_TBOOLEAN:
return LuaType_Boolean;
case LUA_TFUNCTION:
return LuaType_Function;
case LUA_TLIGHTUSERDATA:
return LuaType_LightUserdata;
case LUA_TNIL:
return LuaType_Nil;
case LUA_TNONE:
return LuaType_None;
case LUA_TNUMBER:
return LuaType_Number;
case LUA_TSTRING:
return LuaType_String;
case LUA_TTABLE:
return LuaType_Table;
case LUA_TTHREAD:
return LuaType_Thread;
case LUA_TUSERDATA:
return LuaType_Userdata;
default:
return LuaType_None;
}
}
struct StreamData
{
Stream* stream;
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
};
const char* StreamReader(lua_State* internalState, void* data, std::size_t* size)
{
NazaraUnused(internalState);
StreamData* streamData = static_cast<StreamData*>(data);
if (streamData->stream->EndOfStream())
return nullptr;
else
{
*size = streamData->stream->Read(streamData->buffer, NAZARA_CORE_FILE_BUFFERSIZE);
return streamData->buffer;
}
}
int s_comparisons[] = {
LUA_OPEQ, // LuaComparison_Equality
LUA_OPLT, // LuaComparison_Less
LUA_OPLE // LuaComparison_LessOrEqual
};
static_assert(sizeof(s_comparisons)/sizeof(int) == LuaComparison_Max+1, "Lua comparison array is incomplete");
int s_operations[] = {
LUA_OPADD, // LuaOperation_Addition
LUA_OPBAND, // LuaOperation_BitwiseAnd
LUA_OPSHL, // LuaOperation_BitwiseLeftShift
LUA_OPBNOT, // LuaOperation_BitwiseNot
LUA_OPBOR, // LuaOperation_BitwiseOr
LUA_OPSHR, // LuaOperation_BitwiseRightShift
LUA_OPBXOR, // LuaOperation_BitwiseXOr
LUA_OPDIV, // LuaOperation_Division
LUA_OPPOW, // LuaOperation_Exponentiation
LUA_OPIDIV, // LuaOperation_FloorDivision
LUA_OPMUL, // LuaOperation_Multiplication
LUA_OPMOD, // LuaOperation_Modulo
LUA_OPUNM, // LuaOperation_Negation
LUA_OPSUB // LuaOperation_Substraction
};
static_assert(sizeof(s_operations)/sizeof(int) == LuaOperation_Max+1, "Lua operation array is incomplete");
int s_types[] = {
LUA_TBOOLEAN, // LuaType_Boolean
LUA_TFUNCTION, // LuaType_Function
LUA_TLIGHTUSERDATA, // LuaType_LightUserdata
LUA_TNIL, // LuaType_Nil
LUA_TNUMBER, // LuaType_Number
LUA_TNONE, // LuaType_None
LUA_TSTRING, // LuaType_String
LUA_TTABLE, // LuaType_Table
LUA_TTHREAD, // LuaType_Thread
LUA_TUSERDATA // LuaType_Userdata
};
static_assert(sizeof(s_types)/sizeof(int) == LuaType_Max+1, "Lua type array is incomplete");
}
void LuaState::ArgCheck(bool condition, unsigned int argNum, const char* error) const
{
luaL_argcheck(m_state, condition, argNum, error);
}
void LuaState::ArgCheck(bool condition, unsigned int argNum, const String& error) const
{
luaL_argcheck(m_state, condition, argNum, error.GetConstBuffer());
}
int LuaState::ArgError(unsigned int argNum, const char* error) const
{
return luaL_argerror(m_state, argNum, error);
}
int LuaState::ArgError(unsigned int argNum, const String& error) const
{
return luaL_argerror(m_state, argNum, error.GetConstBuffer());
}
bool LuaState::Call(unsigned int argCount)
{
return Run(argCount, LUA_MULTRET, 0);
}
bool LuaState::Call(unsigned int argCount, unsigned int resultCount)
{
return Run(argCount, resultCount, 0);
}
bool LuaState::CallWithHandler(unsigned int argCount, int errorHandler)
{
return Run(argCount, LUA_MULTRET, errorHandler);
}
bool LuaState::CallWithHandler(unsigned int argCount, unsigned int resultCount, int errorHandler)
{
return Run(argCount, resultCount, errorHandler);
}
void LuaState::CheckAny(int index) const
{
luaL_checkany(m_state, index);
}
bool LuaState::CheckBoolean(int index) const
{
if (lua_isnoneornil(m_state, index))
{
const char* msg = lua_pushfstring(m_state, "%s expected, got %s", lua_typename(m_state, LUA_TBOOLEAN), luaL_typename(m_state, index));
luaL_argerror(m_state, index, msg); // Lance une exception
return false;
}
return lua_toboolean(m_state, index) != 0;
}
bool LuaState::CheckBoolean(int index, bool defValue) const
{
if (lua_isnoneornil(m_state, index))
return defValue;
return lua_toboolean(m_state, index) != 0;
}
long long LuaState::CheckInteger(int index) const
{
return luaL_checkinteger(m_state, index);
}
long long LuaState::CheckInteger(int index, long long defValue) const
{
return luaL_optinteger(m_state, index, defValue);
}
double LuaState::CheckNumber(int index) const
{
return luaL_checknumber(m_state, index);
}
double LuaState::CheckNumber(int index, double defValue) const
{
return luaL_optnumber(m_state, index, defValue);
}
void LuaState::CheckStack(int space, const char* error) const
{
luaL_checkstack(m_state, space, error);
}
void LuaState::CheckStack(int space, const String& error) const
{
CheckStack(space, error.GetConstBuffer());
}
const char* LuaState::CheckString(int index, std::size_t* length) const
{
return luaL_checklstring(m_state, index, length);
}
const char* LuaState::CheckString(int index, const char* defValue, std::size_t* length) const
{
return luaL_optlstring(m_state, index, defValue, length);
}
void LuaState::CheckType(int index, LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return;
}
#endif
luaL_checktype(m_state, index, s_types[type]);
}
void* LuaState::CheckUserdata(int index, const char* tname) const
{
return luaL_checkudata(m_state, index, tname);
}
void* LuaState::CheckUserdata(int index, const String& tname) const
{
return luaL_checkudata(m_state, index, tname.GetConstBuffer());
}
bool LuaState::Compare(int index1, int index2, LuaComparison comparison) const
{
#ifdef NAZARA_DEBUG
if (comparison > LuaComparison_Max)
{
NazaraError("Lua comparison out of enum");
return false;
}
#endif
return (lua_compare(m_state, index1, index2, s_comparisons[comparison]) != 0);
}
void LuaState::Compute(LuaOperation operation) const
{
#ifdef NAZARA_DEBUG
if (operation > LuaOperation_Max)
{
NazaraError("Lua operation out of enum");
return;
}
#endif
lua_arith(m_state, s_operations[operation]);
}
void LuaState::Concatenate(int count) const
{
lua_concat(m_state, count);
}
int LuaState::CreateReference()
{
return luaL_ref(m_state, LUA_REGISTRYINDEX);
}
void LuaState::DestroyReference(int ref)
{
luaL_unref(m_state, LUA_REGISTRYINDEX, ref);
}
String LuaState::DumpStack() const
{
StringStream stream;
unsigned int stackTop = GetStackTop();
stream << stackTop << " entries\n";
for (unsigned int i = 1; i <= stackTop; ++i)
{
stream << i << ": ";
switch (GetType(i))
{
case LuaType_Boolean:
stream << "Boolean(" << ToBoolean(i) << ')';
break;
case LuaType_Function:
stream << "Function(" << ToPointer(i) << ')';
break;
case LuaType_LightUserdata:
case LuaType_Userdata:
stream << "Userdata(" << ToUserdata(i) << ')';
break;
case LuaType_Nil:
stream << "Nil";
break;
case LuaType_None:
stream << "None";
break;
case LuaType_Number:
stream << "Number(" << ToNumber(i) << ')';
break;
case LuaType_String:
stream << "String(" << ToString(i) << ')';
break;
case LuaType_Table:
stream << "Table(" << ToPointer(i) << ')';
break;
case LuaType_Thread:
stream << "Thread(" << ToPointer(i) << ')';
break;
default:
stream << "Unknown(" << ToPointer(i) << ')';
break;
}
stream << '\n';
}
return stream.ToString();
}
void LuaState::Error(const char* message) const
{
luaL_error(m_state, message);
}
void LuaState::Error(const String& message) const
{
luaL_error(m_state, message.GetConstBuffer());
}
bool LuaState::Execute(const String& code, int errorHandler)
{
if (code.IsEmpty())
return true;
if (!Load(code))
return false;
return CallWithHandler(errorHandler, 0);
}
bool LuaState::ExecuteFromFile(const std::filesystem::path& filePath, int errorHandler)
{
if (!LoadFromFile(filePath))
return false;
return CallWithHandler(errorHandler, 0);
}
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size, int errorHandler)
{
MemoryView stream(data, size);
return ExecuteFromStream(stream, errorHandler);
}
bool LuaState::ExecuteFromStream(Stream& stream, int errorHandler)
{
if (!LoadFromStream(stream))
return false;
return CallWithHandler(errorHandler, 0);
}
int LuaState::GetAbsIndex(int index) const
{
return lua_absindex(m_state, index);
}
LuaType LuaState::GetField(const char* fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName));
}
LuaType LuaState::GetField(const String& fieldName, int tableIndex) const
{
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName.GetConstBuffer()));
}
LuaType LuaState::GetGlobal(const char* name) const
{
return FromLuaType(lua_getglobal(m_state, name));
}
LuaType LuaState::GetGlobal(const String& name) const
{
return FromLuaType(lua_getglobal(m_state, name.GetConstBuffer()));
}
LuaType LuaState::GetMetatable(const char* tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname));
}
LuaType LuaState::GetMetatable(const String& tname) const
{
return FromLuaType(luaL_getmetatable(m_state, tname.GetConstBuffer()));
}
bool LuaState::GetMetatable(int index) const
{
return lua_getmetatable(m_state, index) != 0;
}
unsigned int LuaState::GetStackTop() const
{
return static_cast<int>(lua_gettop(m_state));
}
LuaType LuaState::GetTable(int index) const
{
return FromLuaType(lua_gettable(m_state, index));
}
LuaType LuaState::GetTableRaw(int index) const
{
return FromLuaType(lua_rawget(m_state, index));
}
LuaType LuaState::GetType(int index) const
{
return FromLuaType(lua_type(m_state, index));
}
const char* LuaState::GetTypeName(LuaType type) const
{
#ifdef NAZARA_DEBUG
if (type > LuaType_Max)
{
NazaraError("Lua type out of enum");
return nullptr;
}
#endif
return lua_typename(m_state, s_types[type]);
}
void LuaState::Insert(int index) const
{
lua_insert(m_state, index);
}
bool LuaState::IsOfType(int index, LuaType type) const
{
switch (type)
{
case LuaType_Boolean:
return lua_isboolean(m_state, index) != 0;
case LuaType_Function:
return lua_isfunction(m_state, index) != 0;
case LuaType_LightUserdata:
return lua_islightuserdata(m_state, index) != 0;
case LuaType_Nil:
return lua_isnil(m_state, index) != 0;
case LuaType_None:
return lua_isnone(m_state, index) != 0;
case LuaType_Number:
return lua_isnumber(m_state, index) != 0;
case LuaType_String:
return lua_isstring(m_state, index) != 0;
case LuaType_Table:
return lua_istable(m_state, index) != 0;
case LuaType_Thread:
return lua_isthread(m_state, index) != 0;
case LuaType_Userdata:
return lua_isuserdata(m_state, index) != 0;
}
NazaraError("Lua type not handled (0x" + String::Number(type, 16) + ')');
return false;
}
bool LuaState::IsOfType(int index, const char* tname) const
{
void* ud = luaL_testudata(m_state, index, tname);
return ud != nullptr;
}
bool LuaState::IsOfType(int index, const String& tname) const
{
return IsOfType(index, tname.GetConstBuffer());
}
bool LuaState::IsValid(int index) const
{
return lua_isnoneornil(m_state, index) == 0;
}
bool LuaState::Load(const String& code)
{
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
bool LuaState::LoadFromFile(const std::filesystem::path& filePath)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
return false;
}
std::size_t length = static_cast<std::size_t>(file.GetSize());
String source(length, '\0');
if (file.Read(&source[0], length) != length)
{
NazaraError("Failed to read file");
return false;
}
file.Close();
return Load(source);
}
bool LuaState::LoadFromMemory(const void* data, std::size_t size)
{
MemoryView stream(data, size);
return LoadFromStream(stream);
}
bool LuaState::LoadFromStream(Stream& stream)
{
StreamData data;
data.stream = &stream;
if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
long long LuaState::Length(int index) const
{
return luaL_len(m_state, index);
}
std::size_t LuaState::LengthRaw(int index) const
{
return lua_rawlen(m_state, index);
}
void LuaState::MoveTo(LuaState* instance, int n) const
{
lua_xmove(m_state, instance->m_state, n);
}
LuaCoroutine LuaState::NewCoroutine()
{
lua_State* thread = lua_newthread(m_state);
int ref = luaL_ref(m_state, LUA_REGISTRYINDEX);
return LuaCoroutine(thread, ref);
}
bool LuaState::NewMetatable(const char* str)
{
return luaL_newmetatable(m_state, str) != 0;
}
bool LuaState::NewMetatable(const String& str)
{
return luaL_newmetatable(m_state, str.GetConstBuffer()) != 0;
}
bool LuaState::Next(int index) const
{
return lua_next(m_state, index) != 0;
}
void LuaState::Pop(unsigned int n) const
{
lua_pop(m_state, static_cast<int>(n));
}
void LuaState::PushBoolean(bool value) const
{
lua_pushboolean(m_state, (value) ? 1 : 0);
}
void LuaState::PushCFunction(LuaCFunction func, unsigned int upvalueCount) const
{
lua_pushcclosure(m_state, func, upvalueCount);
}
void LuaState::PushFunction(LuaFunction func) const
{
LuaFunction* luaFunc = static_cast<LuaFunction*>(lua_newuserdata(m_state, sizeof(LuaFunction)));
PlacementNew(luaFunc, std::move(func));
lua_pushcclosure(m_state, ProxyFunc, 1);
}
void LuaState::PushInteger(long long value) const
{
lua_pushinteger(m_state, value);
}
void LuaState::PushLightUserdata(void* value) const
{
lua_pushlightuserdata(m_state, value);
}
void LuaState::PushMetatable(const char* str) const
{
luaL_getmetatable(m_state, str);
}
void LuaState::PushMetatable(const String& str) const
{
luaL_getmetatable(m_state, str.GetConstBuffer());
}
void LuaState::PushNil() const
{
lua_pushnil(m_state);
}
void LuaState::PushNumber(double value) const
{
lua_pushnumber(m_state, value);
}
void LuaState::PushReference(int ref) const
{
lua_rawgeti(m_state, LUA_REGISTRYINDEX, ref);
}
void LuaState::PushString(const char* str) const
{
lua_pushstring(m_state, str);
}
void LuaState::PushString(const char* str, std::size_t size) const
{
lua_pushlstring(m_state, str, size);
}
void LuaState::PushString(const String& str) const
{
lua_pushlstring(m_state, str.GetConstBuffer(), str.GetSize());
}
void LuaState::PushTable(std::size_t sequenceElementCount, std::size_t arrayElementCount) const
{
constexpr std::size_t maxInt = std::numeric_limits<int>::max();
lua_createtable(m_state, static_cast<int>(std::min(sequenceElementCount, maxInt)), static_cast<int>(std::min(arrayElementCount, maxInt)));
}
void* LuaState::PushUserdata(std::size_t size) const
{
return lua_newuserdata(m_state, size);
}
void LuaState::PushValue(int index) const
{
lua_pushvalue(m_state, index);
}
bool LuaState::RawEqual(int index1, int index2) const
{
return lua_rawequal(m_state, index1, index2);
}
void LuaState::Remove(int index) const
{
lua_remove(m_state, index);
}
void LuaState::Replace(int index) const
{
lua_replace(m_state, index);
}
void LuaState::SetField(const char* name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name);
}
void LuaState::SetField(const String& name, int tableIndex) const
{
lua_setfield(m_state, tableIndex, name.GetConstBuffer());
}
void LuaState::SetGlobal(const char* name)
{
lua_setglobal(m_state, name);
}
void LuaState::SetGlobal(const String& name)
{
lua_setglobal(m_state, name.GetConstBuffer());
}
void LuaState::SetMetatable(const char* tname) const
{
luaL_setmetatable(m_state, tname);
}
void LuaState::SetMetatable(const String& tname) const
{
luaL_setmetatable(m_state, tname.GetConstBuffer());
}
void LuaState::SetMetatable(int index) const
{
lua_setmetatable(m_state, index);
}
void LuaState::SetTable(int index) const
{
lua_settable(m_state, index);
}
void LuaState::SetTableRaw(int index) const
{
lua_rawset(m_state, index);
}
bool LuaState::ToBoolean(int index) const
{
return lua_toboolean(m_state, index) != 0;
}
long long LuaState::ToInteger(int index, bool* succeeded) const
{
int success;
long long result = lua_tointegerx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
double LuaState::ToNumber(int index, bool* succeeded) const
{
int success;
double result = lua_tonumberx(m_state, index, &success);
if (succeeded)
*succeeded = (success != 0);
return result;
}
const void* LuaState::ToPointer(int index) const
{
return lua_topointer(m_state, index);
}
const char* LuaState::ToString(int index, std::size_t* length) const
{
return lua_tolstring(m_state, index, length);
}
void* LuaState::ToUserdata(int index) const
{
return lua_touserdata(m_state, index);
}
void* LuaState::ToUserdata(int index, const char* tname) const
{
return luaL_testudata(m_state, index, tname);
}
void* LuaState::ToUserdata(int index, const String& tname) const
{
return luaL_testudata(m_state, index, tname.GetConstBuffer());
}
void LuaState::Traceback(const char* message, int level)
{
luaL_traceback(m_state, m_state, message, level);
}
bool LuaState::Run(int argCount, int resultCount, int errHandler)
{
LuaInstance& instance = GetInstance(m_state);
if (instance.m_level++ == 0)
instance.m_clock.Restart();
int status = lua_pcall(m_state, argCount, resultCount, errHandler);
instance.m_level--;
if (status != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
int LuaState::GetIndexOfUpValue(int upValue)
{
return lua_upvalueindex(upValue);
}
LuaInstance& LuaState::GetInstance(lua_State* internalState)
{
LuaInstance* instance;
lua_getallocf(internalState, reinterpret_cast<void**>(&instance));
return *instance;
}
int LuaState::ProxyFunc(lua_State* internalState)
{
LuaFunction& func = *static_cast<LuaFunction*>(lua_touserdata(internalState, lua_upvalueindex(1)));
LuaState state = GetState(internalState);
return func(state);
}
}

View File

@@ -1,31 +0,0 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Config.hpp>
#if NAZARA_NOISE_MANAGE_MEMORY
#include <Nazara/Core/MemoryManager.hpp>
#include <new> // Nécessaire ?
void* operator new(std::size_t size)
{
return Nz::MemoryManager::Allocate(size, false);
}
void* operator new[](std::size_t size)
{
return Nz::MemoryManager::Allocate(size, true);
}
void operator delete(void* pointer) noexcept
{
Nz::MemoryManager::Free(pointer, false);
}
void operator delete[](void* pointer) noexcept
{
Nz::MemoryManager::Free(pointer, true);
}
#endif // NAZARA_NOISE_MANAGE_MEMORY

View File

@@ -1,62 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/FBM.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
FBM::FBM(const NoiseBase & source): m_source(source)
{
}
///TODO: Handle with variadic templates
float FBM::Get(float x, float y, float scale) const
{
float value = 0.f;
for(int i = 0; i < m_octaves; ++i)
{
value += m_source.Get(x, y, scale) * m_exponent_array.at(i);
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if(std::fabs(remainder) > 0.01f)
value += remainder * m_source.Get(x, y, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum;
}
float FBM::Get(float x, float y, float z, float scale) const
{
float value = 0.f;
for(int i = 0; i < m_octaves; ++i)
{
value += m_source.Get(x, y, z, scale) * m_exponent_array.at(i);
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if(std::fabs(remainder) > 0.01f)
value += remainder * m_source.Get(x, y, z, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum;
}
float FBM::Get(float x, float y, float z, float w, float scale) const
{
float value = 0.f;
for(int i = 0; i < m_octaves; ++i)
{
value += m_source.Get(x, y, z, w, scale) * m_exponent_array.at(i);
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if(std::fabs(remainder) > 0.01f)
value += remainder * m_source.Get(x, y, z, w, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum;
}
}

View File

@@ -1,98 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/HybridMultiFractal.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
HybridMultiFractal::HybridMultiFractal(const NoiseBase & source) :
m_source(source)
{
}
float HybridMultiFractal::Get(float x, float y, float scale) const
{
float offset = 1.0f;
float value = (m_source.Get(x, y, scale) + offset) * m_exponent_array.at(0);
float weight = value;
float signal = 0.f;
scale *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (weight > 1.f)
weight = 1.f;
signal = (m_source.Get(x, y, scale) + offset) * m_exponent_array.at(i);
value += weight * signal;
weight *= signal;
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if (remainder > 0.f)
value += remainder * m_source.Get(x, y, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum - offset;
}
float HybridMultiFractal::Get(float x, float y, float z, float scale) const
{
float offset = 1.0f;
float value = (m_source.Get(x, y, z, scale) + offset) * m_exponent_array.at(0);
float weight = value;
float signal = 0.f;
scale *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (weight > 1.f)
weight = 1.f;
signal = (m_source.Get(x, y, z, scale) + offset) * m_exponent_array.at(i);
value += weight * signal;
weight *= signal;
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if (remainder > 0.f)
value += remainder * m_source.Get(x, y, z, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum - offset;
}
float HybridMultiFractal::Get(float x, float y, float z, float w, float scale) const
{
float offset = 1.0f;
float value = (m_source.Get(x, y, z, w, scale) + offset) * m_exponent_array.at(0);
float weight = value;
float signal = 0.f;
scale *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (weight > 1.f)
weight = 1.f;
signal = (m_source.Get(x, y, z, w, scale) + offset) * m_exponent_array.at(i);
value += weight * signal;
weight *= signal;
scale *= m_lacunarity;
}
float remainder = m_octaves - static_cast<int>(m_octaves);
if (remainder > 0.f)
value += remainder * m_source.Get(x, y, z, w, scale) * m_exponent_array.at(static_cast<int>(m_octaves-1));
return value / m_sum - offset;
}
}

View File

@@ -1,56 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/MixerBase.hpp>
#include <cmath>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
MixerBase::MixerBase() :
m_hurst(1.2f),
m_lacunarity(5.f),
m_octaves(3.f)
{
Recompute();
}
float MixerBase::GetHurstParameter() const
{
return m_hurst;
}
float MixerBase::GetLacunarity() const
{
return m_lacunarity;
}
float MixerBase::GetOctaveNumber() const
{
return m_octaves;
}
void MixerBase::SetParameters(float hurst, float lacunarity, float octaves)
{
m_hurst = hurst;
m_lacunarity = lacunarity;
m_octaves = octaves;
Recompute();
}
void MixerBase::Recompute()
{
float frequency = 1.0;
m_sum = 0.f;
m_exponent_array.clear();
for (int i(0) ; i < static_cast<int>(m_octaves) ; ++i)
{
m_exponent_array.push_back(std::pow( frequency, -m_hurst ));
frequency *= m_lacunarity;
m_sum += m_exponent_array.at(i);
}
}
}

View File

@@ -1,65 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Noise.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
bool Noise::Initialize()
{
if (s_moduleReferenceCounter > 0)
{
s_moduleReferenceCounter++;
return true; // Déjà initialisé
}
// Initialisation des dépendances
if (!Core::Initialize())
{
NazaraError("Failed to initialize core module");
Uninitialize();
return false;
}
s_moduleReferenceCounter++;
// Initialisation du module
NazaraNotice("Initialized: Noise module");
return true;
}
bool Noise::IsInitialized()
{
return s_moduleReferenceCounter != 0;
}
void Noise::Uninitialize()
{
if (s_moduleReferenceCounter != 1)
{
// Le module est soit encore utilisé, soit pas initialisé
if (s_moduleReferenceCounter > 1)
s_moduleReferenceCounter--;
return;
}
// Libération du module
s_moduleReferenceCounter = 0;
NazaraNotice("Uninitialized: Noise module");
// Libération des dépendances
Core::Uninitialize();
}
unsigned int Noise::s_moduleReferenceCounter = 0;
}

View File

@@ -1,74 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/NoiseBase.hpp>
#include <numeric>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
NoiseBase::NoiseBase(unsigned int seed) :
m_scale(0.05f)
{
SetSeed(seed);
// Fill permutations with initial values
std::iota(m_permutations.begin(), m_permutations.begin() + 256, 0);
}
float NoiseBase::GetScale()
{
return m_scale;
}
void NoiseBase::SetScale(float scale)
{
m_scale = scale;
}
void NoiseBase::SetSeed(unsigned int seed)
{
m_randomEngine.seed(seed);
}
void NoiseBase::Shuffle()
{
std::shuffle(m_permutations.begin(), m_permutations.begin() + 256, m_randomEngine);
for(std::size_t i = 1; i < (m_permutations.size() / 256); ++i)
std::copy(m_permutations.begin(), m_permutations.begin() + 256, m_permutations.begin() + 256 * i);
}
std::array<Vector2f, 2 * 2 * 2> NoiseBase::s_gradients2 =
{
{
{1.f, 1.f}, {-1.f, 1.f}, {1.f, -1.f}, {-1.f, -1.f},
{1.f, 0.f}, {-1.f, 0.f}, {0.f, 1.f}, { 0.f, -1.f}
}
};
std::array<Vector3f, 2 * 2 * 2 * 2> NoiseBase::s_gradients3 =
{
{
{1.f,1.f,0.f}, {-1.f, 1.f, 0.f}, {1.f, -1.f, 0.f}, {-1.f, -1.f, 0.f},
{1.f,0.f,1.f}, {-1.f, 0.f, 1.f}, {1.f, 0.f, -1.f}, {-1.f, 0.f, -1.f},
{0.f,1.f,1.f}, { 0.f, -1.f, 1.f}, {0.f, 1.f, -1.f}, {0.f, -1.f, -1.f},
{1.f,1.f,0.f}, {-1.f, 1.f, 0.f}, {0.f, -1.f, 1.f}, {0.f, -1.f, -1.f}
}
};
std::array<Vector4f, 2 * 2 * 2 * 2 * 2> NoiseBase::s_gradients4 =
{
{
{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
{0,-1,1,1},{0,-1,1,-1},{0,-1,-1,1},{0,-1,-1,-1},
{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
{-1,0,1,1},{-1,0,1,-1},{-1,0,-1,1},{-1,0,-1,-1},
{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
{-1,1,0,1},{-1,1,0,-1},{-1,-1,0,1},{-1,-1,0,-1},
{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
{-1,1,1,0},{-1,1,-1,0},{-1,-1,1,0},{-1,-1,-1,0}
}
};
}

View File

@@ -1,28 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/NoiseTools.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
int fastfloor(float n)
{
return (n >= 0) ? static_cast<int>(n) : static_cast<int>(n-1);
}
int JenkinsHash(int a, int b, int c)
{
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 13);
b = b-c; b = b - a; b = b^(a << 8);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 13);
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 12);
b = b-c; b = b - a; b = b^(a << 16);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 5);
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 3);
b = b-c; b = b - a; b = b^(a << 10);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 15);
return c;
}
}

View File

@@ -1,274 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Perlin.hpp>
#include <Nazara/Noise/NoiseTools.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Perlin::Perlin(unsigned int seed) :
Perlin()
{
SetSeed(seed);
Shuffle();
}
float Perlin::Get(float x, float y, float scale) const
{
float xc, yc;
int x0, y0;
int gi0,gi1,gi2,gi3;
int ii, jj;
float s,t,u,v;
float Cx,Cy;
float Li1, Li2;
float tempx,tempy;
xc = x * scale;
yc = y * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
ii = x0 & 255;
jj = y0 & 255;
gi0 = m_permutations[ii + m_permutations[jj]] & 7;
gi1 = m_permutations[ii + 1 + m_permutations[jj]] & 7;
gi2 = m_permutations[ii + m_permutations[jj + 1]] & 7;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1]] & 7;
tempx = xc - x0;
tempy = yc - y0;
Cx = tempx * tempx * tempx * (tempx * (tempx * 6 - 15) + 10);
Cy = tempy * tempy * tempy * (tempy * (tempy * 6 - 15) + 10);
s = s_gradients2[gi0][0]*tempx + s_gradients2[gi0][1]*tempy;
tempx = xc - (x0 + 1);
t = s_gradients2[gi1][0]*tempx + s_gradients2[gi1][1]*tempy;
tempy = yc - (y0 + 1);
v = s_gradients2[gi3][0]*tempx + s_gradients2[gi3][1]*tempy;
tempx = xc - x0;
u = s_gradients2[gi2][0]*tempx + s_gradients2[gi2][1]*tempy;
Li1 = s + Cx*(t-s);
Li2 = u + Cx*(v-u);
return Li1 + Cy*(Li2-Li1);
}
float Perlin::Get(float x, float y, float z, float scale) const
{
float xc, yc, zc;
int x0, y0, z0;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7;
int ii, jj, kk;
float Li1,Li2,Li3,Li4,Li5,Li6;
float s[2],t[2],u[2],v[2];
float Cx,Cy,Cz;
float tempx,tempy,tempz;
xc = x * scale;
yc = y * scale;
zc = z * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
z0 = fastfloor(zc);
ii = x0 & 255;
jj = y0 & 255;
kk = z0 & 255;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk]]] & 15;
gi1 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk]]] & 15;
gi2 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk]]] & 15;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk]]] & 15;
gi4 = m_permutations[ii + m_permutations[jj + m_permutations[kk + 1]]] & 15;
gi5 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + 1]]] & 15;
gi6 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1]]] & 15;
gi7 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1]]] & 15;
tempx = xc - x0;
tempy = yc - y0;
tempz = zc - z0;
Cx = tempx * tempx * tempx * (tempx * (tempx * 6 - 15) + 10);
Cy = tempy * tempy * tempy * (tempy * (tempy * 6 - 15) + 10);
Cz = tempz * tempz * tempz * (tempz * (tempz * 6 - 15) + 10);
s[0] = s_gradients3[gi0][0]*tempx + s_gradients3[gi0][1]*tempy + s_gradients3[gi0][2]*tempz;
tempx = xc - (x0 + 1);
t[0] = s_gradients3[gi1][0]*tempx + s_gradients3[gi1][1]*tempy + s_gradients3[gi1][2]*tempz;
tempy = yc - (y0 + 1);
v[0] = s_gradients3[gi3][0]*tempx + s_gradients3[gi3][1]*tempy + s_gradients3[gi3][2]*tempz;
tempx = xc - x0;
u[0] = s_gradients3[gi2][0]*tempx + s_gradients3[gi2][1]*tempy + s_gradients3[gi2][2]*tempz;
tempy = yc - y0;
tempz = zc - (z0 + 1);
s[1] = s_gradients3[gi4][0]*tempx + s_gradients3[gi4][1]*tempy + s_gradients3[gi4][2]*tempz;
tempx = xc - (x0 + 1);
t[1] = s_gradients3[gi5][0]*tempx + s_gradients3[gi5][1]*tempy + s_gradients3[gi5][2]*tempz;
tempy = yc - (y0 + 1);
v[1] = s_gradients3[gi7][0]*tempx + s_gradients3[gi7][1]*tempy + s_gradients3[gi7][2]*tempz;
tempx = xc - x0;
u[1] = s_gradients3[gi6][0]*tempx + s_gradients3[gi6][1]*tempy + s_gradients3[gi6][2]*tempz;
Li1 = s[0] + Cx*(t[0]-s[0]);
Li2 = u[0] + Cx*(v[0]-u[0]);
Li3 = s[1] + Cx*(t[1]-s[1]);
Li4 = u[1] + Cx*(v[1]-u[1]);
Li5 = Li1 + Cy * (Li2-Li1);
Li6 = Li3 + Cy * (Li4-Li3);
return Li5 + Cz * (Li6-Li5);
}
float Perlin::Get(float x, float y, float z, float w, float scale) const
{
float xc,yc,zc,wc;
int x0,y0,z0,w0;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15;
int ii,jj,kk,ll;
float Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14;
float s[4],t[4],u[4],v[4];
float Cx,Cy,Cz,Cw;
float tempx,tempy,tempz,tempw;
xc = x * scale;
yc = y * scale;
zc = z * scale;
wc = w * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
z0 = fastfloor(zc);
w0 = fastfloor(wc);
ii = x0 & 255;
jj = y0 & 255;
kk = z0 & 255;
ll = w0 & 255;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk + m_permutations[ll]]]] & 31;
gi1 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + m_permutations[ll]]]] & 31;
gi2 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll]]]] & 31;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll]]]] & 31;
gi4 = m_permutations[ii + m_permutations[jj + + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi5 = m_permutations[ii + 1 + m_permutations[jj + + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi6 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi7 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll]]]] & 31;
gi8 = m_permutations[ii + m_permutations[jj + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi9 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi10 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi11 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + m_permutations[ll + 1]]]] & 31;
gi12 = m_permutations[ii + m_permutations[jj + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi13 = m_permutations[ii + 1 + m_permutations[jj + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi14 = m_permutations[ii + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
gi15 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll + 1]]]] & 31;
tempx = xc - x0;
tempy = yc - y0;
tempz = zc - z0;
tempw = wc - w0;
Cx = tempx * tempx * tempx * (tempx * (tempx * 6 - 15) + 10);
Cy = tempy * tempy * tempy * (tempy * (tempy * 6 - 15) + 10);
Cz = tempz * tempz * tempz * (tempz * (tempz * 6 - 15) + 10);
Cw = tempw * tempw * tempw * (tempw * (tempw * 6 - 15) + 10);
s[0] = s_gradients4[gi0][0]*tempx + s_gradients4[gi0][1]*tempy + s_gradients4[gi0][2]*tempz + s_gradients4[gi0][3]*tempw;
tempx = xc - (x0+1);
t[0] = s_gradients4[gi1][0]*tempx + s_gradients4[gi1][1]*tempy + s_gradients4[gi1][2]*tempz + s_gradients4[gi1][3]*tempw;
tempy = yc - (y0+1);
v[0] = s_gradients4[gi3][0]*tempx + s_gradients4[gi3][1]*tempy + s_gradients4[gi3][2]*tempz + s_gradients4[gi3][3]*tempw;
tempx = xc - x0;
u[0] = s_gradients4[gi2][0]*tempx + s_gradients4[gi2][1]*tempy + s_gradients4[gi2][2]*tempz + s_gradients4[gi2][3]*tempw;
tempy = yc - y0;
tempz = zc - (z0+1);
s[1] = s_gradients4[gi4][0]*tempx + s_gradients4[gi4][1]*tempy + s_gradients4[gi4][2]*tempz + s_gradients4[gi4][3]*tempw;
tempx = xc - (x0+1);
t[1] = s_gradients4[gi5][0]*tempx + s_gradients4[gi5][1]*tempy + s_gradients4[gi5][2]*tempz + s_gradients4[gi5][3]*tempw;
tempy = yc - (y0+1);
v[1] = s_gradients4[gi7][0]*tempx + s_gradients4[gi7][1]*tempy + s_gradients4[gi7][2]*tempz + s_gradients4[gi7][3]*tempw;
tempx = xc - x0;
u[1] = s_gradients4[gi6][0]*tempx + s_gradients4[gi6][1]*tempy + s_gradients4[gi6][2]*tempz + s_gradients4[gi6][3]*tempw;
tempy = yc - y0;
tempz = zc - z0;
tempw = wc - (w0+1);
s[2] = s_gradients4[gi8][0]*tempx + s_gradients4[gi8][1]*tempy + s_gradients4[gi8][2]*tempz + s_gradients4[gi8][3]*tempw;
tempx = xc - (x0+1);
t[2] = s_gradients4[gi9][0]*tempx + s_gradients4[gi9][1]*tempy + s_gradients4[gi9][2]*tempz + s_gradients4[gi9][3]*tempw;
tempy = yc - (y0+1);
v[2] = s_gradients4[gi11][0]*tempx + s_gradients4[gi11][1]*tempy + s_gradients4[gi11][2]*tempz + s_gradients4[gi11][3]*tempw;
tempx = xc - x0;
u[2] = s_gradients4[gi10][0]*tempx + s_gradients4[gi10][1]*tempy + s_gradients4[gi10][2]*tempz + s_gradients4[gi10][3]*tempw;
tempy = yc - y0;
tempz = zc - (z0+1);
s[3] = s_gradients4[gi12][0]*tempx + s_gradients4[gi12][1]*tempy + s_gradients4[gi12][2]*tempz + s_gradients4[gi12][3]*tempw;
tempx = xc - (x0+1);
t[3] = s_gradients4[gi13][0]*tempx + s_gradients4[gi13][1]*tempy + s_gradients4[gi13][2]*tempz + s_gradients4[gi13][3]*tempw;
tempy = yc - (y0+1);
v[3] = s_gradients4[gi15][0]*tempx + s_gradients4[gi15][1]*tempy + s_gradients4[gi15][2]*tempz + s_gradients4[gi15][3]*tempw;
tempx = xc - x0;
u[3] = s_gradients4[gi14][0]*tempx + s_gradients4[gi14][1]*tempy + s_gradients4[gi14][2]*tempz + s_gradients4[gi14][3]*tempw;
Li1 = s[0] + Cx*(t[0]-s[0]);
Li2 = u[0] + Cx*(v[0]-u[0]);
Li3 = s[1] + Cx*(t[1]-s[1]);
Li4 = u[1] + Cx*(v[1]-u[1]);
Li5 = s[2] + Cx*(t[2]-s[2]);
Li6 = u[2] + Cx*(v[2]-u[2]);
Li7 = s[3] + Cx*(t[3]-s[3]);
Li8 = u[3] + Cx*(v[3]-u[3]);
Li9 = Li1 + Cy*(Li2-Li1);
Li10 = Li3 + Cy*(Li4-Li3);
Li11 = Li5 + Cy*(Li6-Li5);
Li12 = Li7 + Cy*(Li8-Li7);
Li13 = Li9 + Cz*(Li10-Li9);
Li14 = Li11 + Cz*(Li12-Li11);
return Li13 + Cw*(Li14-Li13);
}
}

View File

@@ -1,374 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Simplex.hpp>
#include <Nazara/Noise/NoiseTools.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
namespace
{
constexpr float s_SkewCoeff2D = 0.5f * (float(M_SQRT3) - 1.f);
constexpr float s_UnskewCoeff2D = (3.f - float(M_SQRT3))/6.f;
constexpr float s_SkewCoeff3D = 1.f / 3.f;
constexpr float s_UnskewCoeff3D = 1.f / 6.f;
constexpr float s_SkewCoeff4D = (float(M_SQRT5) - 1.f)/4.f;
constexpr float s_UnskewCoeff4D = (5.f - float(M_SQRT5))/20.f;
}
Simplex::Simplex(unsigned int seed)
{
SetSeed(seed);
Shuffle();
}
float Simplex::Get(float x, float y, float scale) const
{
float xc = x * scale;
float yc = y * scale;
float sum = (xc + yc) * s_SkewCoeff2D;
Vector2i skewedCubeOrigin(fastfloor(xc + sum), fastfloor(yc + sum));
sum = (skewedCubeOrigin.x + skewedCubeOrigin.y) * s_UnskewCoeff2D;
Vector2f unskewedCubeOrigin(skewedCubeOrigin.x - sum, skewedCubeOrigin.y - sum);
Vector2f unskewedDistToOrigin(xc - unskewedCubeOrigin.x, yc - unskewedCubeOrigin.y);
Vector2ui off1;
if(unskewedDistToOrigin.x > unskewedDistToOrigin.y)
off1.Set(1, 0);
else
off1.Set(0, 1);
std::array<Vector2f, 3> d;
d[0] = -unskewedDistToOrigin;
d[1] = d[0] + Vector2f(off1) - Vector2f(s_UnskewCoeff2D);
d[2] = d[0] + Vector2f(1.f - 2.f * s_UnskewCoeff2D);
Vector2i offset(skewedCubeOrigin.x & 255, skewedCubeOrigin.y & 255);
std::array<std::size_t, 3> gi = {
{
m_permutations[offset.x + m_permutations[offset.y]] & 7,
m_permutations[offset.x + off1.x + m_permutations[offset.y + off1.y]] & 7,
m_permutations[offset.x + 1 + m_permutations[offset.y + 1]] & 7
}
};
float n = 0.f;
for (unsigned int i = 0; i < 3; ++i)
{
float c = 0.5f - d[i].x * d[i].x - d[i].y *d[i].y;
if (c > 0.f)
n += c * c * c * c * (s_gradients2[gi[i]].x * d[i].x + s_gradients2[gi[i]].y * d[i].y);
}
return n*70.f;
}
float Simplex::Get(float x, float y, float z, float scale) const
{
float xc, yc, zc;
int ii,jj,kk;
int gi0,gi1,gi2,gi3;
int skewedCubeOriginx,skewedCubeOriginy,skewedCubeOriginz;
int off1x,off1y,off1z;
int off2x,off2y,off2z;
float n1,n2,n3,n4;
float c1,c2,c3,c4;
float sum;
float unskewedCubeOriginx,unskewedCubeOriginy,unskewedCubeOriginz;
float unskewedDistToOriginx,unskewedDistToOriginy,unskewedDistToOriginz;
float d1x,d1y,d1z;
float d2x,d2y,d2z;
float d3x,d3y,d3z;
float d4x,d4y,d4z;
xc = x * scale;
yc = y * scale;
zc = z * scale;
sum = (xc + yc + zc) * s_SkewCoeff3D;
skewedCubeOriginx = fastfloor(xc + sum);
skewedCubeOriginy = fastfloor(yc + sum);
skewedCubeOriginz = fastfloor(zc + sum);
sum = (skewedCubeOriginx + skewedCubeOriginy + skewedCubeOriginz) * s_UnskewCoeff3D;
unskewedCubeOriginx = skewedCubeOriginx - sum;
unskewedCubeOriginy = skewedCubeOriginy - sum;
unskewedCubeOriginz = skewedCubeOriginz - sum;
unskewedDistToOriginx = xc - unskewedCubeOriginx;
unskewedDistToOriginy = yc - unskewedCubeOriginy;
unskewedDistToOriginz = zc - unskewedCubeOriginz;
if(unskewedDistToOriginx >= unskewedDistToOriginy)
{
if(unskewedDistToOriginy >= unskewedDistToOriginz)
{
off1x = 1;
off1y = 0;
off1z = 0;
off2x = 1;
off2y = 1;
off2z = 0;
}
else if(unskewedDistToOriginx >= unskewedDistToOriginz)
{
off1x = 1;
off1y = 0;
off1z = 0;
off2x = 1;
off2y = 0;
off2z = 1;
}
else
{
off1x = 0;
off1y = 0;
off1z = 1;
off2x = 1;
off2y = 0;
off2z = 1;
}
}
else
{
if(unskewedDistToOriginy < unskewedDistToOriginz)
{
off1x = 0;
off1y = 0;
off1z = 1;
off2x = 0;
off2y = 1;
off2z = 1;
}
else if(unskewedDistToOriginx < unskewedDistToOriginz)
{
off1x = 0;
off1y = 1;
off1z = 0;
off2x = 0;
off2y = 1;
off2z = 1;
}
else
{
off1x = 0;
off1y = 1;
off1z = 0;
off2x = 1;
off2y = 1;
off2z = 0;
}
}
d1x = unskewedDistToOriginx;
d1y = unskewedDistToOriginy;
d1z = unskewedDistToOriginz;
d2x = d1x - off1x + s_UnskewCoeff3D;
d2y = d1y - off1y + s_UnskewCoeff3D;
d2z = d1z - off1z + s_UnskewCoeff3D;
d3x = d1x - off2x + 2.f*s_UnskewCoeff3D;
d3y = d1y - off2y + 2.f*s_UnskewCoeff3D;
d3z = d1z - off2z + 2.f*s_UnskewCoeff3D;
d4x = d1x - 1.f + 3.f*s_UnskewCoeff3D;
d4y = d1y - 1.f + 3.f*s_UnskewCoeff3D;
d4z = d1z - 1.f + 3.f*s_UnskewCoeff3D;
ii = skewedCubeOriginx & 255;
jj = skewedCubeOriginy & 255;
kk = skewedCubeOriginz & 255;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk ]]] % 12;
gi1 = m_permutations[ii + off1x + m_permutations[jj + off1y + m_permutations[kk + off1z ]]] % 12;
gi2 = m_permutations[ii + off2x + m_permutations[jj + off2y + m_permutations[kk + off2z ]]] % 12;
gi3 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 ]]] % 12;
c1 = 0.6f - d1x * d1x - d1y * d1y - d1z * d1z;
c2 = 0.6f - d2x * d2x - d2y * d2y - d2z * d2z;
c3 = 0.6f - d3x * d3x - d3y * d3y - d3z * d3z;
c4 = 0.6f - d4x * d4x - d4y * d4y - d4z * d4z;
if(c1 < 0)
n1 = 0;
else
n1 = c1*c1*c1*c1*(s_gradients3[gi0][0] * d1x + s_gradients3[gi0][1] * d1y + s_gradients3[gi0][2] * d1z);
if(c2 < 0)
n2 = 0;
else
n2 = c2*c2*c2*c2*(s_gradients3[gi1][0] * d2x + s_gradients3[gi1][1] * d2y + s_gradients3[gi1][2] * d2z);
if(c3 < 0)
n3 = 0;
else
n3 = c3*c3*c3*c3*(s_gradients3[gi2][0] * d3x + s_gradients3[gi2][1] * d3y + s_gradients3[gi2][2] * d3z);
if(c4 < 0)
n4 = 0;
else
n4 = c4*c4*c4*c4*(s_gradients3[gi3][0] * d4x + s_gradients3[gi3][1] * d4y + s_gradients3[gi3][2] * d4z);
return (n1+n2+n3+n4)*32;
}
float Simplex::Get(float x, float y, float z, float w, float scale) const
{
static std::array<Vector4ui, 64> lookupTable =
{
{
{0,1,2,3}, {0,1,3,2}, {0,0,0,0}, {0,2,3,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {1,2,3,0},
{0,2,1,3}, {0,0,0,0}, {0,3,1,2}, {0,3,2,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {1,3,2,0},
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{1,2,0,3}, {0,0,0,0}, {1,3,0,2}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {2,3,0,1}, {2,3,1,0},
{1,0,2,3}, {1,0,3,2}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {2,0,3,1}, {0,0,0,0}, {2,1,3,0},
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{2,0,1,3}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {3,0,1,2}, {3,0,2,1}, {0,0,0,0}, {3,1,2,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}
}
};
float xc,yc,zc,wc;
int ii,jj,kk,ll;
int gi0,gi1,gi2,gi3,gi4;
int skewedCubeOriginx,skewedCubeOriginy,skewedCubeOriginz,skewedCubeOriginw;
int off1x,off1y,off1z,off1w;
int off2x,off2y,off2z,off2w;
int off3x,off3y,off3z,off3w;
int c;
float n1,n2,n3,n4,n5;
float c1,c2,c3,c4,c5;
float sum;
float unskewedCubeOriginx,unskewedCubeOriginy,unskewedCubeOriginz,unskewedCubeOriginw;
float unskewedDistToOriginx,unskewedDistToOriginy,unskewedDistToOriginz,unskewedDistToOriginw;
float d1x,d2x,d3x,d4x,d5x;
float d1y,d2y,d3y,d4y,d5y;
float d1z,d2z,d3z,d4z,d5z;
float d1w,d2w,d3w,d4w,d5w;
xc = x * scale;
yc = y * scale;
zc = z * scale;
wc = w * scale;
sum = (xc + yc + zc + wc) * s_SkewCoeff4D;
skewedCubeOriginx = fastfloor(xc + sum);
skewedCubeOriginy = fastfloor(yc + sum);
skewedCubeOriginz = fastfloor(zc + sum);
skewedCubeOriginw = fastfloor(wc + sum);
sum = (skewedCubeOriginx + skewedCubeOriginy + skewedCubeOriginz + skewedCubeOriginw) * s_UnskewCoeff4D;
unskewedCubeOriginx = skewedCubeOriginx - sum;
unskewedCubeOriginy = skewedCubeOriginy - sum;
unskewedCubeOriginz = skewedCubeOriginz - sum;
unskewedCubeOriginw = skewedCubeOriginw - sum;
unskewedDistToOriginx = xc - unskewedCubeOriginx;
unskewedDistToOriginy = yc - unskewedCubeOriginy;
unskewedDistToOriginz = zc - unskewedCubeOriginz;
unskewedDistToOriginw = wc - unskewedCubeOriginw;
c = 0;
c += (unskewedDistToOriginx > unskewedDistToOriginy) ? 32 : 0;
c += (unskewedDistToOriginx > unskewedDistToOriginz) ? 16 : 0;
c += (unskewedDistToOriginy > unskewedDistToOriginz) ? 8 : 0;
c += (unskewedDistToOriginx > unskewedDistToOriginw) ? 4 : 0;
c += (unskewedDistToOriginy > unskewedDistToOriginw) ? 2 : 0;
c += (unskewedDistToOriginz > unskewedDistToOriginw) ? 1 : 0;
off1x = lookupTable[c][0] >= 3 ? 1 : 0;
off1y = lookupTable[c][1] >= 3 ? 1 : 0;
off1z = lookupTable[c][2] >= 3 ? 1 : 0;
off1w = lookupTable[c][3] >= 3 ? 1 : 0;
off2x = lookupTable[c][0] >= 2 ? 1 : 0;
off2y = lookupTable[c][1] >= 2 ? 1 : 0;
off2z = lookupTable[c][2] >= 2 ? 1 : 0;
off2w = lookupTable[c][3] >= 2 ? 1 : 0;
off3x = lookupTable[c][0] >= 1 ? 1 : 0;
off3y = lookupTable[c][1] >= 1 ? 1 : 0;
off3z = lookupTable[c][2] >= 1 ? 1 : 0;
off3w = lookupTable[c][3] >= 1 ? 1 : 0;
d1x = unskewedDistToOriginx;
d1y = unskewedDistToOriginy;
d1z = unskewedDistToOriginz;
d1w = unskewedDistToOriginw;
d2x = d1x - off1x + s_UnskewCoeff4D;
d2y = d1y - off1y + s_UnskewCoeff4D;
d2z = d1z - off1z + s_UnskewCoeff4D;
d2w = d1w - off1w + s_UnskewCoeff4D;
d3x = d1x - off2x + 2.f*s_UnskewCoeff4D;
d3y = d1y - off2y + 2.f*s_UnskewCoeff4D;
d3z = d1z - off2z + 2.f*s_UnskewCoeff4D;
d3w = d1w - off2w + 2.f*s_UnskewCoeff4D;
d4x = d1x - off3x + 3.f*s_UnskewCoeff4D;
d4y = d1y - off3y + 3.f*s_UnskewCoeff4D;
d4z = d1z - off3z + 3.f*s_UnskewCoeff4D;
d4w = d1w - off3w + 3.f*s_UnskewCoeff4D;
d5x = d1x - 1.f + 4*s_UnskewCoeff4D;
d5y = d1y - 1.f + 4*s_UnskewCoeff4D;
d5z = d1z - 1.f + 4*s_UnskewCoeff4D;
d5w = d1w - 1.f + 4*s_UnskewCoeff4D;
ii = skewedCubeOriginx & 255;
jj = skewedCubeOriginy & 255;
kk = skewedCubeOriginz & 255;
ll = skewedCubeOriginw & 255;
gi0 = m_permutations[ii + m_permutations[jj + m_permutations[kk + m_permutations[ll ]]]] & 31;
gi1 = m_permutations[ii + off1x + m_permutations[jj + off1y + m_permutations[kk + off1z + m_permutations[ll + off1w]]]] & 31;
gi2 = m_permutations[ii + off2x + m_permutations[jj + off2y + m_permutations[kk + off2z + m_permutations[ll + off2w]]]] & 31;
gi3 = m_permutations[ii + off3x + m_permutations[jj + off3y + m_permutations[kk + off3z + m_permutations[ll + off3w]]]] & 31;
gi4 = m_permutations[ii + 1 + m_permutations[jj + 1 + m_permutations[kk + 1 + m_permutations[ll + 1 ]]]] % 32;
c1 = 0.6f - d1x*d1x - d1y*d1y - d1z*d1z - d1w*d1w;
c2 = 0.6f - d2x*d2x - d2y*d2y - d2z*d2z - d2w*d2w;
c3 = 0.6f - d3x*d3x - d3y*d3y - d3z*d3z - d3w*d3w;
c4 = 0.6f - d4x*d4x - d4y*d4y - d4z*d4z - d4w*d4w;
c5 = 0.6f - d5x*d5x - d5y*d5y - d5z*d5z - d5w*d5w;
if(c1 < 0)
n1 = 0;
else
n1 = c1*c1*c1*c1*(s_gradients4[gi0][0]*d1x + s_gradients4[gi0][1]*d1y + s_gradients4[gi0][2]*d1z + s_gradients4[gi0][3]*d1w);
if(c2 < 0)
n2 = 0;
else
n2 = c2*c2*c2*c2*(s_gradients4[gi1][0]*d2x + s_gradients4[gi1][1]*d2y + s_gradients4[gi1][2]*d2z + s_gradients4[gi1][3]*d2w);
if(c3 < 0)
n3 = 0;
else
n3 = c3*c3*c3*c3*(s_gradients4[gi2][0]*d3x + s_gradients4[gi2][1]*d3y + s_gradients4[gi2][2]*d3z + s_gradients4[gi2][3]*d3w);
if(c4 < 0)
n4 = 0;
else
n4 = c4*c4*c4*c4*(s_gradients4[gi3][0]*d4x + s_gradients4[gi3][1]*d4y + s_gradients4[gi3][2]*d4z + s_gradients4[gi3][3]*d4w);
if(c5 < 0)
n5 = 0;
else
n5 = c5*c5*c5*c5*(s_gradients4[gi4][0]*d5x + s_gradients4[gi4][1]*d5y + s_gradients4[gi4][2]*d5z + s_gradients4[gi4][3]*d5w);
return (n1+n2+n3+n4+n5)*27.f;
}
}

View File

@@ -1,155 +0,0 @@
// Copyright (C) 2017 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/Worley.hpp>
#include <Nazara/Noise/NoiseTools.hpp>
#include <stdexcept>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
namespace
{
static constexpr std::array<float, 4> m_functionScales = {
{
1.f / float(M_SQRT2),
0.5f / float(M_SQRT2),
0.5f / float(M_SQRT2),
0.5f / float(M_SQRT2)
}
};
}
Worley::Worley() :
m_function(WorleyFunction_F1)
{
}
Worley::Worley(unsigned int seed) :
Worley()
{
SetSeed(seed);
Shuffle();
}
float Worley::Get(float x, float y, float scale) const
{
std::map<float, Vector2f> featurePoints;
float xc, yc;
int x0, y0;
float fractx, fracty;
xc = x * scale;
yc = y * scale;
x0 = fastfloor(xc);
y0 = fastfloor(yc);
fractx = xc - static_cast<float>(x0);
fracty = yc - static_cast<float>(y0);
featurePoints.clear();
SquareTest(x0,y0,xc,yc,featurePoints);
std::size_t functionIndex = static_cast<std::size_t>(m_function);
auto it = featurePoints.begin();
std::advance(it, functionIndex);
if(fractx < it->first)
SquareTest(x0 - 1,y0,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if(1.f - fractx < it->first)
SquareTest(x0 + 1,y0,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if(fracty < it->first)
SquareTest(x0,y0 - 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if (1.f - fracty < it->first)
SquareTest(x0,y0 + 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if (fractx < it->first && fracty < it->first)
SquareTest(x0 - 1, y0 - 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if (1.f - fractx < it->first && fracty < it->first)
SquareTest(x0 + 1, y0 - 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if (fractx < it->first && 1.f - fracty < it->first)
SquareTest(x0 - 1, y0 + 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
if(1.f - fractx < it->first && 1.f - fracty < it->first)
SquareTest(x0 + 1, y0 + 1,xc,yc,featurePoints);
it = featurePoints.begin();
std::advance(it, functionIndex);
return it->first * m_functionScales[functionIndex];
}
float Worley::Get(float /*x*/, float /*y*/, float /*z*/, float /*scale*/) const
{
throw std::runtime_error("Worley 3D not available yet.");
}
float Worley::Get(float /*x*/, float /*y*/, float /*z*/, float /*w*/, float /*scale*/) const
{
throw std::runtime_error("Worley 4D not available yet.");
}
void Worley::Set(WorleyFunction func)
{
m_function = func;
}
void Worley::SquareTest(int xi, int yi, float x, float y, std::map<float, Vector2f>& featurePoints) const
{
int ii = xi & 255;
int jj = yi & 255;
std::size_t seed = m_permutations[ii + m_permutations[jj]];
//On initialise notre rng avec seed
std::minstd_rand0 randomNumberGenerator(static_cast<unsigned int>(seed));
//On prend un nombre de points à déterminer dans le cube, compris entre 1 et 8
std::size_t m = (seed & 7) + 1;
//On calcule les emplacements des différents points
for(std::size_t i(0) ; i < m; ++i)
{
Nz::Vector2f featurePoint;
featurePoint.x = (randomNumberGenerator() & 1023) / 1023.f + static_cast<float>(xi);
featurePoint.y = (randomNumberGenerator() & 1023) / 1023.f + static_cast<float>(yi);
// TODO : Check order is correct
float distance = std::sqrt((featurePoint.x - x) * (featurePoint.x - x) +
(featurePoint.y - y) * (featurePoint.y - y));
//Insertion dans la liste triée
featurePoints[distance] = featurePoint;
}
}
}