diff --git a/build/scripts/module/noise.lua b/build/scripts/module/noise.lua new file mode 100644 index 000000000..55f21fc76 --- /dev/null +++ b/build/scripts/module/noise.lua @@ -0,0 +1,31 @@ +project "NazaraNoise" + +files +{ + "../include/Nazara/Noise/**.hpp", + "../include/Nazara/Noise/**.inl", + "../src/Nazara/Noise/**.hpp", + "../src/Nazara/Noise/**.cpp" +} + +if (os.is("windows")) then + excludes { "../src/Nazara/Noise/Posix/*.hpp", "../src/Nazara/Noise/Posix/*.cpp" } +else + excludes { "../src/Nazara/Noise/Win32/*.hpp", "../src/Nazara/Noise/Win32/*.cpp" } +end + +configuration "DebugStatic" + links "NazaraCored-s" + targetname "NazaraNoised" + +configuration "ReleaseStatic" + links "NazaraCore-s" + targetname "NazaraNoise" + +configuration "DebugDLL" + links "NazaraCored" + targetname "NazaraNoised" + +configuration "ReleaseDLL" + links "NazaraCore" + targetname "NazaraNoise" \ No newline at end of file diff --git a/include/Nazara/Noise/ComplexNoiseBase.hpp b/include/Nazara/Noise/ComplexNoiseBase.hpp new file mode 100644 index 000000000..a9db6870e --- /dev/null +++ b/include/Nazara/Noise/ComplexNoiseBase.hpp @@ -0,0 +1,35 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef COMPLEXNOISEBASE_HPP +#define COMPLEXNOISEBASE_HPP + +#include +#include + +class NAZARA_API NzComplexNoiseBase : public NzNoiseBase +{ + public: + NzComplexNoiseBase(); + ~NzComplexNoiseBase() = default; + + void SetLacunarity(float lacunarity); + void SetHurstParameter(float h); + void SetOctavesNumber(float octaves); + void RecomputeExponentArray(); + + protected: + float m_lacunarity; + float m_hurst; + float m_octaves; + float exponent_array[30]; + float m_sum; + private: + bool m_parametersModified; + +}; + +#endif // COMPLEXNOISEBASE_HPP diff --git a/include/Nazara/Noise/Config.hpp b/include/Nazara/Noise/Config.hpp new file mode 100644 index 000000000..77cb9d990 --- /dev/null +++ b/include/Nazara/Noise/Config.hpp @@ -0,0 +1,42 @@ +/* + Nazara Engine + + Copyright (C) 2012 Jérôme "Lynix" Leclercq (Lynix680@gmail.com) + + Nazara Engine - NzNoise Module + + Copyright (C) 2012 Rémi "Overdrivr" Bèges (remi.beges@laposte.net) + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#pragma once + +#ifndef NAZARA_CONFIG_NOISE_HPP +#define NAZARA_CONFIG_NOISE_HPP + +/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci + +// Utilise un tracker pour repérer les éventuels leaks (Ralentit l'exécution) +#define NAZARA_NOISE_MEMORYLEAKTRACKER 0 + +// Active les tests de sécurité basés sur le code (Conseillé pour le développement) +#define NAZARA_NOISE_SAFE 1 + +#endif // NAZARA_CONFIG_MODULENAME_HPP diff --git a/include/Nazara/Noise/Debug.hpp b/include/Nazara/Noise/Debug.hpp new file mode 100644 index 000000000..1f8a08966 --- /dev/null +++ b/include/Nazara/Noise/Debug.hpp @@ -0,0 +1,11 @@ +// Copyright (C) 2012 Rémi "Overdrivr" Bèges (remi.beges@laposte.net) +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) + #include + + #define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete + #define new new(__FILE__, __LINE__) +#endif diff --git a/include/Nazara/Noise/DebugOff.hpp b/include/Nazara/Noise/DebugOff.hpp new file mode 100644 index 000000000..1162e79c0 --- /dev/null +++ b/include/Nazara/Noise/DebugOff.hpp @@ -0,0 +1,8 @@ +// Copyright (C) 2012 Rémi "Overdrivr" Bèges (remi.beges@laposte.net) +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) + #undef delete + #undef new +#endif diff --git a/include/Nazara/Noise/Noise.hpp b/include/Nazara/Noise/Noise.hpp new file mode 100644 index 000000000..a42cad927 --- /dev/null +++ b/include/Nazara/Noise/Noise.hpp @@ -0,0 +1,27 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_NOISE_HPP +#define NAZARA_NOISE_HPP + +#include + +class NAZARA_API NzNoise +{ + public: + NzNoise(); + ~NzNoise(); + + bool Initialize(); + void Uninitialize(); + + static bool IsInitialized(); + + private: + static bool s_initialized; +}; + +#endif // NOISE_HPP diff --git a/include/Nazara/Noise/NoiseBase.hpp b/include/Nazara/Noise/NoiseBase.hpp new file mode 100644 index 000000000..331d227df --- /dev/null +++ b/include/Nazara/Noise/NoiseBase.hpp @@ -0,0 +1,33 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NOISEBASE_HPP +#define NOISEBASE_HPP + +#include + +class NAZARA_API NzNoiseBase +{ + public: + NzNoiseBase(int seed = 0); + ~NzNoiseBase() = default; + + void SetNewSeed(int seed); + int GetUniformRandomValue(); + void ShufflePermutationTable(); + + int fastfloor(float n); + int JenkinsHash(int a, int b, int c); + protected: + int perm[512]; + private: + int Ua, Uc, Um; + int UcurrentSeed; + int Uprevious, Ulast; + +}; + +#endif // NOISEBASE_HPP diff --git a/include/Nazara/Noise/NoiseMachine.hpp b/include/Nazara/Noise/NoiseMachine.hpp new file mode 100644 index 000000000..4c221628f --- /dev/null +++ b/include/Nazara/Noise/NoiseMachine.hpp @@ -0,0 +1,93 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NOISEMACHINE_HPP +#define NOISEMACHINE_HPP + +#include +#include +#include +#include +#include + +class NAZARA_API NzNoiseMachine : public NzComplexNoiseBase +{ + public: + NzNoiseMachine(int seed = 0); + ~NzNoiseMachine() = default; + + float Get2DPerlinNoiseValue (float x, float y, float res); + float Get3DPerlinNoiseValue (float x, float y, float z, float res); + float Get4DPerlinNoiseValue (float x, float y, float z, float w, float res); + + float Get2DSimplexNoiseValue(float x, float y, float res); + float Get3DSimplexNoiseValue(float x, float y, float z, float res); + float Get4DSimplexNoiseValue(float x, float y, float z, float w, float res); + + float Get2DCellNoiseValue(float x, float y, float res); + float Get3DCellNoiseValue(float x, float y, float z, float res); + float Get4DCellNoiseValue(float x, float y, float z, float w, float res); + + float Get2DFBMNoiseValue(float x, float y, float res); + float Get3DFBMNoiseValue(float x, float y, float z, float res); + + float Get2DHybridMultiFractalNoiseValue(float x, float y, float res); + float Get3DHybridMultiFractalNoiseValue(float x, float y, float z, float res); + + protected: + private: + + float gradient2[8][2]; + int gradient3[16][3]; + int gradient4[32][4]; + int lookupTable4D[64][4]; + + //----------------------- Common variables -------------------------------------- + int ii,jj,kk,ll; + int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15; + + //----------------------- Simplex variables -------------------------------------- + + float n1, n2, n3, n4, n5; + NzVector4f d1,d2,d3,d4,d5,unskewedCubeOrigin,unskewedDistToOrigin; + NzVector4i off1, off2,off3,skewedCubeOrigin; + + + float c1,c2,c3,c4,c5,c6; + int c; + + float SkewCoeff2D; + float UnskewCoeff2D; + + float SkewCoeff3D; + float UnskewCoeff3D; + + float SkewCoeff4D; + float UnskewCoeff4D; + + float sum; + + //----------------------- Perlin Variables ------------------------------------- + + int x0,y0,z0,w0; + 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; + NzVector4f temp; + float tmp; + + //---------------------- Complex Noise Variables -------------------------------- + + + bool first; + float value; + float remainder; + float smax; + float smin; + +}; + +#endif // NOISEMACHINE_HPP diff --git a/include/Nazara/Noise/Perlin2D.hpp b/include/Nazara/Noise/Perlin2D.hpp new file mode 100644 index 000000000..18e7ae875 --- /dev/null +++ b/include/Nazara/Noise/Perlin2D.hpp @@ -0,0 +1,38 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef PERLIN2D_HPP +#define PERLIN2D_HPP + +#include +#include +#include + +template class NAZARA_API NzPerlin2D : public NzNoiseBase +{ + public: + NzPerlin2D(); + T GetValue(T x, T y, T res); + ~NzPerlin2D() = default; + protected: + private: + int x0, y0; + int gi0,gi1,gi2,gi3; + int ii, jj; + T gradient2[8][2]; + T s,t,u,v; + T Cx,Cy; + T Li1, Li2; + NzVector2 temp; +}; + +typedef NzPerlin2D NzPerlin2Df; +typedef NzPerlin2D NzPerlin2Dd; + +#include + +#endif // PERLIN2D_HPP + diff --git a/include/Nazara/Noise/Perlin2D.inl b/include/Nazara/Noise/Perlin2D.inl new file mode 100644 index 000000000..a823aba81 --- /dev/null +++ b/include/Nazara/Noise/Perlin2D.inl @@ -0,0 +1,60 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzPerlin2D::NzPerlin2D() +{ + T grad2Temp[][2] = {{1,1},{-1,1},{1,-1},{-1,-1}, + {1,0},{-1,0},{0,1},{0,-1}}; + + for(int i(0) ; i < 8 ; ++i) + for(int j(0) ; j < 2 ; ++j) + gradient2[i][j] = grad2Temp[i][j]; +} + +template +T NzPerlin2D::GetValue(T x, T y, T res) +{ + x /= res; + y /= res; + + x0 = fastfloor(x); + y0 = fastfloor(y); + + ii = x0 & 255; + jj = y0 & 255; + + gi0 = perm[ii + perm[jj]] & 7; + gi1 = perm[ii + 1 + perm[jj]] & 7; + gi2 = perm[ii + perm[jj + 1]] & 7; + gi3 = perm[ii + 1 + perm[jj + 1]] & 7; + + temp.x = x-x0; + temp.y = y-y0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + + s = gradient2[gi0][0]*temp.x + gradient2[gi0][1]*temp.y; + + temp.x = x-(x0+1); + t = gradient2[gi1][0]*temp.x + gradient2[gi1][1]*temp.y; + + temp.y = y-(y0+1); + v = gradient2[gi3][0]*temp.x + gradient2[gi3][1]*temp.y; + + temp.x = x-x0; + u = gradient2[gi2][0]*temp.x + gradient2[gi2][1]*temp.y; + + Li1 = s + Cx*(t-s); + Li2 = u + Cx*(v-u); + + return Li1 + Cy*(Li2-Li1); +} + +#include diff --git a/include/Nazara/Noise/Perlin3D.hpp b/include/Nazara/Noise/Perlin3D.hpp new file mode 100644 index 000000000..a425dc384 --- /dev/null +++ b/include/Nazara/Noise/Perlin3D.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef PERLIN3D_HPP +#define PERLIN3D_HPP + +#include +#include +#include + +template class NAZARA_API NzPerlin3D : public NzNoiseBase +{ + public: + NzPerlin3D(); + T GetValue(T x, T y, T z, T res); + ~NzPerlin3D() = default; + protected: + private: + int x0,y0,z0; + int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7; + int ii,jj,kk; + int gradient3[16][3]; + T Li1,Li2,Li3,Li4,Li5,Li6; + T s[2],t[2],u[2],v[2]; + T Cx,Cy,Cz; + T nx,ny,nz; + T tmp; + NzVector3 temp; + +}; + +typedef NzPerlin3D NzPerlin3Df; +typedef NzPerlin3D NzPerlin3Dd; + +#include + +#endif // PERLIN3D_HPP diff --git a/include/Nazara/Noise/Perlin3D.inl b/include/Nazara/Noise/Perlin3D.inl new file mode 100644 index 000000000..60231979a --- /dev/null +++ b/include/Nazara/Noise/Perlin3D.inl @@ -0,0 +1,92 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzPerlin3D::NzPerlin3D() +{ + int grad3Temp[][3] = { + {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, + {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, + {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}, + {1,1,0},{-1,1,0},{0,-1,1},{0,-1,-1} + }; + + for(int i(0) ; i < 16 ; ++i) + for(int j(0) ; j < 3 ; ++j) + gradient3[i][j] = grad3Temp[i][j]; +} + +template +T NzPerlin3D::GetValue(T x, T y, T z, T res) +{ + x /= res; + y /= res; + z /= res; + + x0 = fastfloor(x); + y0 = fastfloor(y); + z0 = fastfloor(z); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk]]] & 15; + gi1 = perm[ii + 1 + perm[jj + perm[kk]]] & 15; + gi2 = perm[ii + perm[jj + 1 + perm[kk]]] & 15; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk]]] & 15; + + gi4 = perm[ii + perm[jj + perm[kk + 1]]] & 15; + gi5 = perm[ii + 1 + perm[jj + perm[kk + 1]]] & 15; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1]]] & 15; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] & 15; + + temp.x = x-x0; + temp.y = y-y0; + temp.z = z-z0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + Cz = temp.z * temp.z * temp.z * (temp.z * (temp.z * 6 - 15) + 10); + + s[0] = gradient3[gi0][0]*temp.x + gradient3[gi0][1]*temp.y + gradient3[gi0][2]*temp.z; + + temp.x = x-(x0+1); + t[0] = gradient3[gi1][0]*temp.x + gradient3[gi1][1]*temp.y + gradient3[gi1][2]*temp.z; + + temp.y = y-(y0+1); + v[0] = gradient3[gi3][0]*temp.x + gradient3[gi3][1]*temp.y + gradient3[gi3][2]*temp.z; + + temp.x = x-x0; + u[0] = gradient3[gi2][0]*temp.x + gradient3[gi2][1]*temp.y + gradient3[gi2][2]*temp.z; + + temp.y = y-y0; + temp.z = z-(z0+1); + s[1] = gradient3[gi4][0]*temp.x + gradient3[gi4][1]*temp.y + gradient3[gi4][2]*temp.z; + + temp.x = x-(x0+1); + t[1] = gradient3[gi5][0]*temp.x + gradient3[gi5][1]*temp.y + gradient3[gi5][2]*temp.z; + + temp.y = y-(y0+1); + v[1] = gradient3[gi7][0]*temp.x + gradient3[gi7][1]*temp.y + gradient3[gi7][2]*temp.z; + + temp.x = x-x0; + u[1] = gradient3[gi6][0]*temp.x + gradient3[gi6][1]*temp.y + gradient3[gi6][2]*temp.z; + + 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); +} + +#include diff --git a/include/Nazara/Noise/Perlin4D.hpp b/include/Nazara/Noise/Perlin4D.hpp new file mode 100644 index 000000000..b5a25ee4d --- /dev/null +++ b/include/Nazara/Noise/Perlin4D.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef PERLIN4D_HPP +#define PERLIN4D_HPP + +#include +#include +#include + +template class NAZARA_API NzPerlin4D : public NzNoiseBase +{ + public: + NzPerlin4D(); + T GetValue(T x, T y, T z, T w, T res); + ~NzPerlin4D() = default; + protected: + private: + 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; + int gradient4[32][4]; + T Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14; + T s[4],t[4],u[4],v[4]; + T Cx,Cy,Cz,Cw; + T nx,ny,nz,nw; + T tmp; + NzVector4 temp; + +}; + +typedef NzPerlin4D NzPerlin4Df; +typedef NzPerlin4D NzPerlin4Dd; + +#include + +#endif // PERLIN4D_HPP diff --git a/include/Nazara/Noise/Perlin4D.inl b/include/Nazara/Noise/Perlin4D.inl new file mode 100644 index 000000000..b009098f8 --- /dev/null +++ b/include/Nazara/Noise/Perlin4D.inl @@ -0,0 +1,150 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzPerlin4D::NzPerlin4D() +{ + int grad4Temp[][4] = + { + {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} + }; + + for(int i(0) ; i < 32 ; ++i) + for(int j(0) ; j < 4 ; ++j) + gradient4[i][j] = grad4Temp[i][j]; +} + +template +T NzPerlin4D::GetValue(T x, T y, T z, T w, T res) +{ + nx = x/res; + ny = y/res; + nz = z/res; + nw = w/res; + + x0 = fastfloor(nx); + y0 = fastfloor(ny); + z0 = fastfloor(nz); + w0 = fastfloor(nw); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + ll = w0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk + perm[ll ]]]] & 31; + gi1 = perm[ii + 1 + perm[jj + perm[kk + perm[ll ]]]] & 31; + gi2 = perm[ii + perm[jj + 1 + perm[kk + perm[ll ]]]] & 31; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll ]]]] & 31; + + gi4 = perm[ii + perm[jj + + perm[kk + 1 + perm[ll ]]]] & 31; + gi5 = perm[ii + 1 + perm[jj + + perm[kk + 1 + perm[ll ]]]] & 31; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] & 31; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] & 31; + + gi8 = perm[ii + perm[jj + perm[kk + perm[ll + 1]]]] & 31; + gi9 = perm[ii + 1 + perm[jj + perm[kk + perm[ll + 1]]]] & 31; + gi10 = perm[ii + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31; + gi11 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31; + + gi12 = perm[ii + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi13 = perm[ii + 1 + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi14 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi15 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31; + + temp.x = nx-x0; + temp.y = ny-y0; + temp.z = nz-z0; + temp.w = nw-w0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + Cz = temp.z * temp.z * temp.z * (temp.z * (temp.z * 6 - 15) + 10); + Cw = temp.w * temp.w * temp.w * (temp.w * (temp.w * 6 - 15) + 10); + + s[0] = gradient4[gi0][0]*temp.x + gradient4[gi0][1]*temp.y + gradient4[gi0][2]*temp.z + gradient4[gi0][3]*temp.w; + + temp.x = nx-(x0+1); + t[0] = gradient4[gi1][0]*temp.x + gradient4[gi1][1]*temp.y + gradient4[gi1][2]*temp.z + gradient4[gi1][3]*temp.w; + + temp.y = ny-(y0+1); + v[0] = gradient4[gi3][0]*temp.x + gradient4[gi3][1]*temp.y + gradient4[gi3][2]*temp.z + gradient4[gi3][3]*temp.w; + + temp.x = nx-x0; + u[0] = gradient4[gi2][0]*temp.x + gradient4[gi2][1]*temp.y + gradient4[gi2][2]*temp.z + gradient4[gi2][3]*temp.w; + + temp.y = ny-y0; + temp.z = nz-(z0+1); + s[1] = gradient4[gi4][0]*temp.x + gradient4[gi4][1]*temp.y + gradient4[gi4][2]*temp.z + gradient4[gi4][3]*temp.w; + + temp.x = nx-(x0+1); + t[1] = gradient4[gi5][0]*temp.x + gradient4[gi5][1]*temp.y + gradient4[gi5][2]*temp.z + gradient4[gi5][3]*temp.w; + + temp.y = ny-(y0+1); + v[1] = gradient4[gi7][0]*temp.x + gradient4[gi7][1]*temp.y + gradient4[gi7][2]*temp.z + gradient4[gi7][3]*temp.w; + + temp.x = nx-x0; + u[1] = gradient4[gi6][0]*temp.x + gradient4[gi6][1]*temp.y + gradient4[gi6][2]*temp.z + gradient4[gi6][3]*temp.w; + + + temp.y = ny-y0; + temp.z = nz-z0; + temp.w = nw-(w0+1); + s[2] = gradient4[gi8][0]*temp.x + gradient4[gi8][1]*temp.y + gradient4[gi8][2]*temp.z + gradient4[gi8][3]*temp.w; + + temp.x = nx-(x0+1); + t[2] = gradient4[gi9][0]*temp.x + gradient4[gi9][1]*temp.y + gradient4[gi9][2]*temp.z + gradient4[gi9][3]*temp.w; + + temp.y = ny-(y0+1); + v[2] = gradient4[gi11][0]*temp.x + gradient4[gi11][1]*temp.y + gradient4[gi11][2]*temp.z + gradient4[gi11][3]*temp.w; + + temp.x = nx-x0; + u[2] = gradient4[gi10][0]*temp.x + gradient4[gi10][1]*temp.y + gradient4[gi10][2]*temp.z + gradient4[gi10][3]*temp.w; + + + temp.y = ny-y0; + temp.z = nz-(z0+1); + s[3] = gradient4[gi12][0]*temp.x + gradient4[gi12][1]*temp.y + gradient4[gi12][2]*temp.z + gradient4[gi12][3]*temp.w; + + temp.x = nx-(x0+1); + t[3] = gradient4[gi13][0]*temp.x + gradient4[gi13][1]*temp.y + gradient4[gi13][2]*temp.z + gradient4[gi13][3]*temp.w; + + temp.y = ny-(y0+1); + v[3] = gradient4[gi15][0]*temp.x + gradient4[gi15][1]*temp.y + gradient4[gi15][2]*temp.z + gradient4[gi15][3]*temp.w; + + temp.x = nx-x0; + u[3] = gradient4[gi14][0]*temp.x + gradient4[gi14][1]*temp.y + gradient4[gi14][2]*temp.z + gradient4[gi14][3]*temp.w; + + 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); +} + +#include diff --git a/include/Nazara/Noise/Simplex2D.hpp b/include/Nazara/Noise/Simplex2D.hpp new file mode 100644 index 000000000..78d896f7d --- /dev/null +++ b/include/Nazara/Noise/Simplex2D.hpp @@ -0,0 +1,43 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef SIMPLEX2D_HPP +#define SIMPLEX2D_HPP + +#include +#include +#include + +template class NAZARA_API NzSimplex2D : public NzNoiseBase +{ + public: + NzSimplex2D(); + T GetValue(T x, T y, T res); + ~NzSimplex2D() = default; + protected: + private: + int ii,jj; + int gi0,gi1,gi2; + NzVector2i skewedCubeOrigin,off1; + T n1,n2,n3; + T c1,c2,c3; + T gradient2[8][2]; + T UnskewCoeff2D; + T SkewCoeff2D; + T sum; + NzVector2 unskewedCubeOrigin, unskewedDistToOrigin; + NzVector2 d1,d2,d3; + + +}; + +typedef NzSimplex2D NzSimplex2Df; +typedef NzSimplex2D NzSimplex2Dd; + +#include + +#endif // SIMPLEX2D_HPP + diff --git a/include/Nazara/Noise/Simplex2D.inl b/include/Nazara/Noise/Simplex2D.inl new file mode 100644 index 000000000..3d019fd54 --- /dev/null +++ b/include/Nazara/Noise/Simplex2D.inl @@ -0,0 +1,88 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzSimplex2D::NzSimplex2D() +{ + T grad2Temp[][2] = {{1,1},{-1,1},{1,-1},{-1,-1}, + {1,0},{-1,0},{0,1},{0,-1}}; + + for(int i(0) ; i < 8 ; ++i) + for(int j(0) ; j < 2 ; ++j) + gradient2[i][j] = grad2Temp[i][j]; + + SkewCoeff2D = 0.5*(sqrt(3.0) - 1.0); + UnskewCoeff2D = (3.0-sqrt(3.0))/6.; +} + +template +T NzSimplex2D::GetValue(T x, T y, T res) +{ + x /= res; + y /= res; + + sum = (x + y) * SkewCoeff2D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y) * UnskewCoeff2D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + + if(unskewedDistToOrigin.x > unskewedDistToOrigin.y) + { + off1.x = 1; + off1.y = 0; + } + else + { + off1.x = 0; + off1.y = 1; + } + + d1 = - unskewedDistToOrigin; + + d2.x = d1.x + off1.x - UnskewCoeff2D; + d2.y = d1.y + off1.y - UnskewCoeff2D; + + d3.x = d1.x + 1.0 - 2 * UnskewCoeff2D; + d3.y = d1.y + 1.0 - 2 * UnskewCoeff2D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + + gi0 = perm[ii + perm[jj ]] & 7; + gi1 = perm[ii + off1.x + perm[jj + off1.y]] & 7; + gi2 = perm[ii + 1 + perm[jj + 1 ]] & 7; + + c1 = 0.5 - d1.x * d1.x - d1.y * d1.y; + c2 = 0.5 - d2.x * d2.x - d2.y * d2.y; + c3 = 0.5 - d3.x * d3.x - d3.y * d3.y; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient2[gi0][0] * d1.x + gradient2[gi0][1] * d1.y); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient2[gi1][0] * d2.x + gradient2[gi1][1] * d2.y); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient2[gi2][0] * d3.x + gradient2[gi2][1] * d3.y); + + return (n1+n2+n3)*70; +} + +#include diff --git a/include/Nazara/Noise/Simplex3D.hpp b/include/Nazara/Noise/Simplex3D.hpp new file mode 100644 index 000000000..399705072 --- /dev/null +++ b/include/Nazara/Noise/Simplex3D.hpp @@ -0,0 +1,43 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef SIMPLEX3D_HPP +#define SIMPLEX3D_HPP + +#include +#include +#include + +template class NAZARA_API NzSimplex3D : public NzNoiseBase +{ + public: + NzSimplex3D(); + T GetValue(T x, T y, T z, T res); + ~NzSimplex3D() = default; + protected: + private: + int ii,jj,kk; + int gi0,gi1,gi2,gi3; + NzVector3i skewedCubeOrigin,off1,off2; + T n1,n2,n3,n4; + T c1,c2,c3,c4; + T gradient3[12][3]; + T UnskewCoeff3D; + T SkewCoeff3D; + T sum; + NzVector3 unskewedCubeOrigin, unskewedDistToOrigin; + NzVector3 d1,d2,d3,d4; + + +}; + +typedef NzSimplex3D NzSimplex3Df; +typedef NzSimplex3D NzSimplex3Dd; + +#include + +#endif // SIMPLEX3D_HPP + diff --git a/include/Nazara/Noise/Simplex3D.inl b/include/Nazara/Noise/Simplex3D.inl new file mode 100644 index 000000000..d0bd3bf0d --- /dev/null +++ b/include/Nazara/Noise/Simplex3D.inl @@ -0,0 +1,157 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzSimplex3D::NzSimplex3D() +{ + SkewCoeff3D = 1/3.; + UnskewCoeff3D = 1/6.; + + int grad3Temp[][3] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, + {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, + {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}}; + + for(int i(0) ; i < 12 ; ++i) + for(int j(0) ; j < 3 ; ++j) + gradient3[i][j] = grad3Temp[i][j]; +} + +template +T NzSimplex3D::GetValue(T x, T y, T z, T res) +{ + x /= res; + y /= res; + z /= res; + + sum = (x + y + z) * SkewCoeff3D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + skewedCubeOrigin.z = fastfloor(z + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y + skewedCubeOrigin.z) * UnskewCoeff3D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + unskewedCubeOrigin.z = skewedCubeOrigin.z - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + unskewedDistToOrigin.z = z - unskewedCubeOrigin.z; + + if(unskewedDistToOrigin.x >= unskewedDistToOrigin.y) + { + if(unskewedDistToOrigin.y >= unskewedDistToOrigin.z) + { + off1.x = 1; + off1.y = 0; + off1.z = 0; + off2.x = 1; + off2.y = 1; + off2.z = 0; + } + else if(unskewedDistToOrigin.x >= unskewedDistToOrigin.z) + { + off1.x = 1; + off1.y = 0; + off1.z = 0; + off2.x = 1; + off2.y = 0; + off2.z = 1; + } + else + { + off1.x = 0; + off1.y = 0; + off1.z = 1; + off2.x = 1; + off2.y = 0; + off2.z = 1; + } + } + else + { + if(unskewedDistToOrigin.y < unskewedDistToOrigin.z) + { + off1.x = 0; + off1.y = 0; + off1.z = 1; + off2.x = 0; + off2.y = 1; + off2.z = 1; + } + else if(unskewedDistToOrigin.x < unskewedDistToOrigin.z) + { + off1.x = 0; + off1.y = 1; + off1.z = 0; + off2.x = 0; + off2.y = 1; + off2.z = 1; + } + else + { + off1.x = 0; + off1.y = 1; + off1.z = 0; + off2.x = 1; + off2.y = 1; + off2.z = 0; + } + } + + d1 = unskewedDistToOrigin; + + d2.x = d1.x - off1.x + UnskewCoeff3D; + d2.y = d1.y - off1.y + UnskewCoeff3D; + d2.z = d1.z - off1.z + UnskewCoeff3D; + + d3.x = d1.x - off2.x + 2*UnskewCoeff3D; + d3.y = d1.y - off2.y + 2*UnskewCoeff3D; + d3.z = d1.z - off2.z + 2*UnskewCoeff3D; + + d4.x = d1.x - 1.0 + 3*UnskewCoeff3D; + d4.y = d1.y - 1.0 + 3*UnskewCoeff3D; + d4.z = d1.z - 1.0 + 3*UnskewCoeff3D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + kk = skewedCubeOrigin.z & 255; + + gi0 = perm[ii + perm[jj + perm[kk ]]] % 12; + gi1 = perm[ii + off1.x + perm[jj + off1.y + perm[kk + off1.z]]] % 12; + gi2 = perm[ii + off2.x + perm[jj + off2.y + perm[kk + off2.z]]] % 12; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 ]]] % 12; + + c1 = 0.6 - d1.x * d1.x - d1.y * d1.y - d1.z * d1.z; + c2 = 0.6 - d2.x * d2.x - d2.y * d2.y - d2.z * d2.z; + c3 = 0.6 - d3.x * d3.x - d3.y * d3.y - d3.z * d3.z; + c4 = 0.6 - d4.x * d4.x - d4.y * d4.y - d4.z * d4.z; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient3[gi0][0] * d1.x + gradient3[gi0][1] * d1.y + gradient3[gi0][2] * d1.z); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient3[gi1][0] * d2.x + gradient3[gi1][1] * d2.y + gradient3[gi1][2] * d2.z); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient3[gi2][0] * d3.x + gradient3[gi2][1] * d3.y + gradient3[gi2][2] * d3.z); + + if(c4 < 0) + n4 = 0; + else + n4 = c4*c4*c4*c4*(gradient3[gi3][0] * d4.x + gradient3[gi3][1] * d4.y + gradient3[gi3][2] * d4.z); + + return (n1+n2+n3+n4)*32; +} + +#include diff --git a/include/Nazara/Noise/Simplex4D.hpp b/include/Nazara/Noise/Simplex4D.hpp new file mode 100644 index 000000000..f932d212b --- /dev/null +++ b/include/Nazara/Noise/Simplex4D.hpp @@ -0,0 +1,45 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef SIMPLEX4D_HPP +#define SIMPLEX4D_HPP + +#include +#include +#include + +template class NAZARA_API NzSimplex4D : public NzNoiseBase +{ + public: + NzSimplex4D(); + T GetValue(T x, T y, T z, T w, T res); + ~NzSimplex4D() = default; + protected: + private: + int ii,jj,kk,ll; + int gi0,gi1,gi2,gi3,gi4; + NzVector4i skewedCubeOrigin,off1,off2,off3; + T n1,n2,n3,n4,n5; + T c1,c2,c3,c4,c5,c6; + T gradient4[32][4]; + int lookupTable4D[64][4]; + int c; + T UnskewCoeff4D; + T SkewCoeff4D; + T sum; + NzVector4 unskewedCubeOrigin, unskewedDistToOrigin; + NzVector4 d1,d2,d3,d4,d5; + + +}; + +typedef NzSimplex4D NzSimplex4Df; +typedef NzSimplex4D NzSimplex4Dd; + +#include + +#endif // SIMPLEX4D_H + diff --git a/include/Nazara/Noise/Simplex4D.inl b/include/Nazara/Noise/Simplex4D.inl new file mode 100644 index 000000000..413467b85 --- /dev/null +++ b/include/Nazara/Noise/Simplex4D.inl @@ -0,0 +1,163 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +template +NzSimplex4D::NzSimplex4D() +{ + SkewCoeff4D = (sqrt(5.) - 1.)/4.; + UnskewCoeff4D = (5. - sqrt(5.))/20.; + + int lookupTemp4D[][4] = + { + {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} + }; + + for(int i(0) ; i < 64 ; ++i) + for(int j(0) ; j < 4 ; ++j) + lookupTable4D[i][j] = lookupTemp4D[i][j]; + + int grad4Temp[][4] = + { + {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} + }; + + for(int i(0) ; i < 32 ; ++i) + for(int j(0) ; j < 4 ; ++j) + gradient4[i][j] = grad4Temp[i][j]; +} + +template +T NzSimplex4D::GetValue(T x, T y, T z, T w, T res) +{ + x /= res; + y /= res; + z /= res; + w /= res; + + sum = (x + y + z + w) * SkewCoeff4D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + skewedCubeOrigin.z = fastfloor(z + sum); + skewedCubeOrigin.w = fastfloor(w + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y + skewedCubeOrigin.z + skewedCubeOrigin.w) * UnskewCoeff4D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + unskewedCubeOrigin.z = skewedCubeOrigin.z - sum; + unskewedCubeOrigin.w = skewedCubeOrigin.w - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + unskewedDistToOrigin.z = z - unskewedCubeOrigin.z; + unskewedDistToOrigin.w = w - unskewedCubeOrigin.w; + + c1 = (unskewedDistToOrigin.x > unskewedDistToOrigin.y) ? 32 : 0; + c2 = (unskewedDistToOrigin.x > unskewedDistToOrigin.z) ? 16 : 0; + c3 = (unskewedDistToOrigin.y > unskewedDistToOrigin.z) ? 8 : 0; + c4 = (unskewedDistToOrigin.x > unskewedDistToOrigin.w) ? 4 : 0; + c5 = (unskewedDistToOrigin.y > unskewedDistToOrigin.w) ? 2 : 0; + c6 = (unskewedDistToOrigin.z > unskewedDistToOrigin.w) ? 1 : 0; + c = c1 + c2 + c3 + c4 + c5 + c6; + + off1.x = lookupTable4D[c][0] >= 3 ? 1 : 0; + off1.y = lookupTable4D[c][1] >= 3 ? 1 : 0; + off1.z = lookupTable4D[c][2] >= 3 ? 1 : 0; + off1.w = lookupTable4D[c][3] >= 3 ? 1 : 0; + + off2.x = lookupTable4D[c][0] >= 2 ? 1 : 0; + off2.y = lookupTable4D[c][1] >= 2 ? 1 : 0; + off2.z = lookupTable4D[c][2] >= 2 ? 1 : 0; + off2.w = lookupTable4D[c][3] >= 2 ? 1 : 0; + + off3.x = lookupTable4D[c][0] >= 1 ? 1 : 0; + off3.y = lookupTable4D[c][1] >= 1 ? 1 : 0; + off3.z = lookupTable4D[c][2] >= 1 ? 1 : 0; + off3.w = lookupTable4D[c][3] >= 1 ? 1 : 0; + + d1 = unskewedDistToOrigin; + + d2.x = d1.x - off1.x + UnskewCoeff4D; + d2.y = d1.y - off1.y + UnskewCoeff4D; + d2.z = d1.z - off1.z + UnskewCoeff4D; + d2.w = d1.w - off1.w + UnskewCoeff4D; + + d3.x = d1.x - off2.x + 2*UnskewCoeff4D; + d3.y = d1.y - off2.y + 2*UnskewCoeff4D; + d3.z = d1.z - off2.z + 2*UnskewCoeff4D; + d3.w = d1.w - off2.w + 2*UnskewCoeff4D; + + d4.x = d1.x - off3.x + 3*UnskewCoeff4D; + d4.y = d1.y - off3.y + 3*UnskewCoeff4D; + d4.z = d1.z - off3.z + 3*UnskewCoeff4D; + d4.w = d1.w - off3.w + 3*UnskewCoeff4D; + + d5.x = d1.x - 1.0 + 4*UnskewCoeff4D; + d5.y = d1.y - 1.0 + 4*UnskewCoeff4D; + d5.z = d1.z - 1.0 + 4*UnskewCoeff4D; + d5.w = d1.w - 1.0 + 4*UnskewCoeff4D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + kk = skewedCubeOrigin.z & 255; + ll = skewedCubeOrigin.w & 255; + + gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] & 31; + gi1 = perm[ii + off1.x + perm[jj + off1.y + perm[kk + off1.z + perm[ll + off1.w]]]] & 31; + gi2 = perm[ii + off2.x + perm[jj + off2.y + perm[kk + off2.z + perm[ll + off2.w]]]] & 31; + gi3 = perm[ii + off3.x + perm[jj + off3.y + perm[kk + off3.z + perm[ll + off3.w]]]] & 31; + gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32; + + c1 = 0.6 - d1.x*d1.x - d1.y*d1.y - d1.z*d1.z - d1.w*d1.w; + c2 = 0.6 - d2.x*d2.x - d2.y*d2.y - d2.z*d2.z - d2.w*d2.w; + c3 = 0.6 - d3.x*d3.x - d3.y*d3.y - d3.z*d3.z - d3.w*d3.w; + c4 = 0.6 - d4.x*d4.x - d4.y*d4.y - d4.z*d4.z - d4.w*d4.w; + c5 = 0.6 - d5.x*d5.x - d5.y*d5.y - d5.z*d5.z - d5.w*d5.w; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient4[gi0][0]*d1.x + gradient4[gi0][1]*d1.y + gradient4[gi0][2]*d1.z + gradient4[gi0][3]*d1.w); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient4[gi1][0]*d2.x + gradient4[gi1][1]*d2.y + gradient4[gi1][2]*d2.z + gradient4[gi1][3]*d2.w); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient4[gi2][0]*d3.x + gradient4[gi2][1]*d3.y + gradient4[gi2][2]*d3.z + gradient4[gi2][3]*d3.w); + + if(c4 < 0) + n4 = 0; + else + n4 = c4*c4*c4*c4*(gradient4[gi3][0]*d4.x + gradient4[gi3][1]*d4.y + gradient4[gi3][2]*d4.z + gradient4[gi3][3]*d4.w); + + if(c5 < 0) + n5 = 0; + else + n5 = c5*c5*c5*c5*(gradient4[gi4][0]*d5.x + gradient4[gi4][1]*d5.y + gradient4[gi4][2]*d5.z + gradient4[gi4][3]*d5.w); + + return (n1+n2+n3+n4+n5)*27.0; +} + +#include diff --git a/readme.txt b/readme.txt index a4cc42567..02814821f 100644 --- a/readme.txt +++ b/readme.txt @@ -12,7 +12,7 @@ Authors ------- Jérôme "Lynix" Leclercq - main developper (Lynix680@gmail.com) -Rémi "overdrivr" Bèges - developper & helper (remi.beges@laposte.net) +Rémi "overdrivr" Bèges - developper & helper - NzNoise Module - (remi.beges@laposte.net) Install ------- diff --git a/readme_fr.txt b/readme_fr.txt index 81c3e99ad..a31c39eca 100644 --- a/readme_fr.txt +++ b/readme_fr.txt @@ -12,7 +12,7 @@ Auteurs ------- Jérôme "Lynix" Leclercq - développeur principal (Lynix680@gmail.com) -Rémi "overdrivr" Bèges - développeur & aide (remi.beges@laposte.net) +Rémi "overdrivr" Bèges - développeur & aide - module NzNoise - (remi.beges@laposte.net) Installation ------------ diff --git a/src/Nazara/Noise/ComplexNoiseBase.cpp b/src/Nazara/Noise/ComplexNoiseBase.cpp new file mode 100644 index 000000000..0388a7786 --- /dev/null +++ b/src/Nazara/Noise/ComplexNoiseBase.cpp @@ -0,0 +1,62 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include +#include + +NzComplexNoiseBase::NzComplexNoiseBase() +{ + m_parametersModified = true; + m_lacunarity = 5.0f; + m_hurst = 1.2f; + m_octaves = 3.0f; +} + +void NzComplexNoiseBase::SetLacunarity(float lacunarity) +{ + if(lacunarity != m_lacunarity) + { + m_lacunarity = lacunarity; + m_parametersModified = true; + } +} + +void NzComplexNoiseBase::SetHurstParameter(float h) +{ + if(h != m_hurst) + { + m_hurst = h; + m_parametersModified = true; + } +} + +void NzComplexNoiseBase::SetOctavesNumber(float octaves) +{ + if(octaves != m_octaves && octaves < 30) + { + m_octaves = octaves; + m_parametersModified = true; + } +} + +void NzComplexNoiseBase::RecomputeExponentArray() +{ + if(m_parametersModified) + { + float frequency = 1.0; + m_sum = 0.f; + for (int i(0) ; i < m_octaves; ++i) + { + exponent_array[i] = std::pow( frequency, -m_hurst ); + frequency *= m_lacunarity; + + m_sum += exponent_array[i]; + + } + m_parametersModified = false; + } +} diff --git a/src/Nazara/Noise/Debug/Leaks.cpp b/src/Nazara/Noise/Debug/Leaks.cpp new file mode 100644 index 000000000..3e65ef142 --- /dev/null +++ b/src/Nazara/Noise/Debug/Leaks.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#if NAZARA_NOISE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) +#include +#include + +void* operator new(std::size_t size) throw(std::bad_alloc) +{ + return NzMemoryManager::Allocate(size, false); +} + +void* operator new[](std::size_t size) throw(std::bad_alloc) +{ + return NzMemoryManager::Allocate(size, true); +} + +void operator delete(void* pointer) throw() +{ + NzMemoryManager::Free(pointer, false); +} + +void operator delete[](void* pointer) throw() +{ + NzMemoryManager::Free(pointer, true); +} + +#endif diff --git a/src/Nazara/Noise/Noise.cpp b/src/Nazara/Noise/Noise.cpp new file mode 100644 index 000000000..8d5dd92f5 --- /dev/null +++ b/src/Nazara/Noise/Noise.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +NzNoise::NzNoise() +{ +} + +NzNoise::~NzNoise() +{ + if (s_initialized) + Uninitialize(); +} + +bool NzNoise::Initialize() +{ + #if NAZARA_NOISE_SAFE + if (s_initialized) + { + NazaraError("NzNoise already initialized"); + return true; + } + #endif + + // Initialisation du module + + s_initialized = true; + + return true; +} + +void NzNoise::Uninitialize() +{ + #if NAZARA_NOISE_SAFE + if (!s_initialized) + { + NazaraError("NzNoise not initialized"); + return; + } + #endif + + // Libération du module + + s_initialized = false; +} + +bool NzNoise::IsInitialized() +{ + return s_initialized; +} + +bool NzNoise::s_initialized = false; + +//#include //A INCLURE ? diff --git a/src/Nazara/Noise/NoiseBase.cpp b/src/Nazara/Noise/NoiseBase.cpp new file mode 100644 index 000000000..410e2b95d --- /dev/null +++ b/src/Nazara/Noise/NoiseBase.cpp @@ -0,0 +1,78 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +NzNoiseBase::NzNoiseBase(int seed) +{ + Ua = 16807; + Uc = 0; + Um = 2147483647; + UcurrentSeed = 0; + Uprevious = 0; + + SetNewSeed(seed); + + for(int i(0) ; i < 256 ; i++) + perm[i] = i; + +} + +void NzNoiseBase::SetNewSeed(int seed) +{ + Uprevious = seed; + UcurrentSeed = seed; +} + +int NzNoiseBase::GetUniformRandomValue() +{ + Ulast = Ua*Uprevious + Uc%Um; + Uprevious = Ulast; + return Ulast; +} + +void NzNoiseBase::ShufflePermutationTable() +{ + int xchanger; + unsigned int ncase; + + for(int i(0) ; i < 256 ; i++) + perm[i] = i; + + for(int j(0) ; j < 20 ; ++j) + for (int i(0); i < 256 ; ++i) + { + ncase = this->GetUniformRandomValue() & 255; + xchanger = perm[i]; + perm[i] = perm[ncase]; + perm[ncase] = xchanger; + } + + for(int i(256) ; i < 512; ++i) + perm[i] = perm[i & 255]; +} + +int NzNoiseBase::fastfloor(float n) +{ + return (n >= 0) ? static_cast(n) : static_cast(n-1); +} + +int NzNoiseBase::JenkinsHash(int a, int b, int c) +{ + a = a-b; a = a - c; a = a^(static_cast(c) >> 13); + b = b-c; b = b - a; b = b^(a << 8); + c = c-a; c = c - b; c = c^(static_cast(b) >> 13); + a = a-b; a = a - c; a = a^(static_cast(c) >> 12); + b = b-c; b = b - a; b = b^(a << 16); + c = c-a; c = c - b; c = c^(static_cast(b) >> 5); + a = a-b; a = a - c; a = a^(static_cast(c) >> 3); + b = b-c; b = b - a; b = b^(a << 10); + c = c-a; c = c - b; c = c^(static_cast(b) >> 15); + return c; +} + +#include diff --git a/src/Nazara/Noise/NoiseMachine.cpp b/src/Nazara/Noise/NoiseMachine.cpp new file mode 100644 index 000000000..e695d46fe --- /dev/null +++ b/src/Nazara/Noise/NoiseMachine.cpp @@ -0,0 +1,762 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +NzNoiseMachine::NzNoiseMachine(int seed) +{ + SkewCoeff2D = 0.5*(sqrt(3.0) - 1.0); + UnskewCoeff2D = (3.0-sqrt(3.0))/6; + + SkewCoeff3D = 1/3; + UnskewCoeff3D = 1/6; + + SkewCoeff4D = (sqrt(5) - 1)/4; + UnskewCoeff4D = (5 - sqrt(5))/20; + + + int lookupTemp4D[][4] = + { + {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} + }; + + for(int i(0) ; i < 64 ; ++i) + for(int j(0) ; j < 4 ; ++j) + lookupTable4D[i][j] = lookupTemp4D[i][j]; + + float grad2Temp[][2] = {{1,1},{-1,1},{1,-1},{-1,-1}, + {1,0},{-1,0},{0,1},{0,-1}}; + + for(int i(0) ; i < 8 ; ++i) + for(int j(0) ; j < 2 ; ++j) + gradient2[i][j] = grad2Temp[i][j]; + + int grad3Temp[][3] = { + {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, + {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, + {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}, + {1,1,0},{-1,1,0},{0,-1,1},{0,-1,-1} + }; + + for(int i(0) ; i < 16 ; ++i) + for(int j(0) ; j < 3 ; ++j) + gradient3[i][j] = grad3Temp[i][j]; + + int grad4Temp[][4] = + { + {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} + }; + + for(int i(0) ; i < 32 ; ++i) + for(int j(0) ; j < 4 ; ++j) + gradient4[i][j] = grad4Temp[i][j]; +} + +//------------------------------ PERLIN ------------------------------ + +float NzNoiseMachine::Get2DPerlinNoiseValue(float x, float y, float res) +{ + x /= res; + y /= res; + + x0 = fastfloor(x); + y0 = fastfloor(y); + + ii = x0 & 255; + jj = y0 & 255; + + gi0 = perm[ii + perm[jj]] & 7; + gi1 = perm[ii + 1 + perm[jj]] & 7; + gi2 = perm[ii + perm[jj + 1]] & 7; + gi3 = perm[ii + 1 + perm[jj + 1]] & 7; + + temp.x = x-x0; + temp.y = y-y0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + + s[0] = gradient2[gi0][0]*temp.x + gradient2[gi0][1]*temp.y; + + temp.x = x-(x0+1); + t[0] = gradient2[gi1][0]*temp.x + gradient2[gi1][1]*temp.y; + + temp.y = y-(y0+1); + v[0] = gradient2[gi3][0]*temp.x + gradient2[gi3][1]*temp.y; + + temp.x = x-x0; + u[0] = gradient2[gi2][0]*temp.x + gradient2[gi2][1]*temp.y; + + Li1 = s[0] + Cx*(t[0]-s[0]); + Li2 = u[0] + Cx*(v[0]-u[0]); + + return Li1 + Cy*(Li2-Li1); +} +float NzNoiseMachine::Get3DPerlinNoiseValue(float x, float y, float z, float res) +{ + x /= res; + y /= res; + z /= res; + + x0 = fastfloor(x); + y0 = fastfloor(y); + z0 = fastfloor(z); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk ]]] & 15; + gi1 = perm[ii + 1 + perm[jj + perm[kk ]]] & 15; + gi2 = perm[ii + perm[jj + 1 + perm[kk ]]] & 15; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk ]]] & 15; + + gi4 = perm[ii + perm[jj + perm[kk + 1]]] & 15; + gi5 = perm[ii + 1 + perm[jj + perm[kk + 1]]] & 15; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1]]] & 15; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] & 15; + + temp.x = x-x0; + temp.y = y-y0; + temp.z = z-z0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + Cz = temp.z * temp.z * temp.z * (temp.z * (temp.z * 6 - 15) + 10); + + s[0] = gradient3[gi0][0]*temp.x + gradient3[gi0][1]*temp.y + gradient3[gi0][2]*temp.z; + + temp.x = x-(x0+1); + t[0] = gradient3[gi1][0]*temp.x + gradient3[gi1][1]*temp.y + gradient3[gi1][2]*temp.z; + + temp.y = y-(y0+1); + v[0] = gradient3[gi3][0]*temp.x + gradient3[gi3][1]*temp.y + gradient3[gi3][2]*temp.z; + + temp.x = x-x0; + u[0] = gradient3[gi2][0]*temp.x + gradient3[gi2][1]*temp.y + gradient3[gi2][2]*temp.z; + + temp.y = y-y0; + temp.z = z-(z0+1); + s[1] = gradient3[gi4][0]*temp.x + gradient3[gi4][1]*temp.y + gradient3[gi4][2]*temp.z; + + temp.x = x-(x0+1); + t[1] = gradient3[gi5][0]*temp.x + gradient3[gi5][1]*temp.y + gradient3[gi5][2]*temp.z; + + temp.y = y-(y0+1); + v[1] = gradient3[gi7][0]*temp.x + gradient3[gi7][1]*temp.y + gradient3[gi7][2]*temp.z; + + temp.x = x-x0; + u[1] = gradient3[gi6][0]*temp.x + gradient3[gi6][1]*temp.y + gradient3[gi6][2]*temp.z; + + 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 NzNoiseMachine::Get4DPerlinNoiseValue(float x, float y, float z, float w, float res) +{ + x /= res; + y /= res; + z /= res; + w /= res; + + x0 = fastfloor(x); + y0 = fastfloor(y); + z0 = fastfloor(z); + w0 = fastfloor(w); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + ll = w0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk + perm[ll ]]]] & 31; + gi1 = perm[ii + 1 + perm[jj + perm[kk + perm[ll ]]]] & 31; + gi2 = perm[ii + perm[jj + 1 + perm[kk + perm[ll ]]]] & 31; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll ]]]] & 31; + + gi4 = perm[ii + perm[jj + + perm[kk + 1 + perm[ll ]]]] & 31; + gi5 = perm[ii + 1 + perm[jj + + perm[kk + 1 + perm[ll ]]]] & 31; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] & 31; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] & 31; + + gi8 = perm[ii + perm[jj + perm[kk + perm[ll + 1]]]] & 31; + gi9 = perm[ii + 1 + perm[jj + perm[kk + perm[ll + 1]]]] & 31; + gi10 = perm[ii + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31; + gi11 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll + 1]]]] & 31; + + gi12 = perm[ii + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi13 = perm[ii + 1 + perm[jj + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi14 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31; + gi15 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] & 31; + + temp.x = x-x0; + temp.y = y-y0; + temp.z = z-z0; + temp.w = w-w0; + + Cx = temp.x * temp.x * temp.x * (temp.x * (temp.x * 6 - 15) + 10); + Cy = temp.y * temp.y * temp.y * (temp.y * (temp.y * 6 - 15) + 10); + Cz = temp.z * temp.z * temp.z * (temp.z * (temp.z * 6 - 15) + 10); + Cw = temp.w * temp.w * temp.w * (temp.w * (temp.w * 6 - 15) + 10); + + s[0] = gradient4[gi0][0]*temp.x + gradient4[gi0][1]*temp.y + gradient4[gi0][2]*temp.z + gradient4[gi0][3]*temp.w; + + temp.x = x-(x0+1); + t[0] = gradient4[gi1][0]*temp.x + gradient4[gi1][1]*temp.y + gradient4[gi1][2]*temp.z + gradient4[gi1][3]*temp.w; + + temp.y = y-(y0+1); + v[0] = gradient4[gi3][0]*temp.x + gradient4[gi3][1]*temp.y + gradient4[gi3][2]*temp.z + gradient4[gi3][3]*temp.w; + + temp.x = x-x0; + u[0] = gradient4[gi2][0]*temp.x + gradient4[gi2][1]*temp.y + gradient4[gi2][2]*temp.z + gradient4[gi2][3]*temp.w; + + temp.y = y-y0; + temp.z = z-(z0+1); + s[1] = gradient4[gi4][0]*temp.x + gradient4[gi4][1]*temp.y + gradient4[gi4][2]*temp.z + gradient4[gi4][3]*temp.w; + + temp.x = x-(x0+1); + t[1] = gradient4[gi5][0]*temp.x + gradient4[gi5][1]*temp.y + gradient4[gi5][2]*temp.z + gradient4[gi5][3]*temp.w; + + temp.y = y-(y0+1); + v[1] = gradient4[gi7][0]*temp.x + gradient4[gi7][1]*temp.y + gradient4[gi7][2]*temp.z + gradient4[gi7][3]*temp.w; + + temp.x = x-x0; + u[1] = gradient4[gi6][0]*temp.x + gradient4[gi6][1]*temp.y + gradient4[gi6][2]*temp.z + gradient4[gi6][3]*temp.w; + + + temp.y = y-y0; + temp.z = z-z0; + temp.w = w-(w0+1); + s[2] = gradient4[gi8][0]*temp.x + gradient4[gi8][1]*temp.y + gradient4[gi8][2]*temp.z + gradient4[gi8][3]*temp.w; + + temp.x = x-(x0+1); + t[2] = gradient4[gi9][0]*temp.x + gradient4[gi9][1]*temp.y + gradient4[gi9][2]*temp.z + gradient4[gi9][3]*temp.w; + + temp.y = y-(y0+1); + v[2] = gradient4[gi11][0]*temp.x + gradient4[gi11][1]*temp.y + gradient4[gi11][2]*temp.z + gradient4[gi11][3]*temp.w; + + temp.x = x-x0; + u[2] = gradient4[gi10][0]*temp.x + gradient4[gi10][1]*temp.y + gradient4[gi10][2]*temp.z + gradient4[gi10][3]*temp.w; + + temp.y = y-y0; + temp.z = z-(z0+1); + s[3] = gradient4[gi12][0]*temp.x + gradient4[gi12][1]*temp.y + gradient4[gi12][2]*temp.z + gradient4[gi12][3]*temp.w; + + temp.x = x-(x0+1); + t[3] = gradient4[gi13][0]*temp.x + gradient4[gi13][1]*temp.y + gradient4[gi13][2]*temp.z + gradient4[gi13][3]*temp.w; + + temp.y = y-(y0+1); + v[3] = gradient4[gi15][0]*temp.x + gradient4[gi15][1]*temp.y + gradient4[gi15][2]*temp.z + gradient4[gi15][3]*temp.w; + + temp.x = x-x0; + u[3] = gradient4[gi14][0]*temp.x + gradient4[gi14][1]*temp.y + gradient4[gi14][2]*temp.z + gradient4[gi14][3]*temp.w; + + 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); +} + +//------------------------------ SIMPLEX ------------------------------ + +float NzNoiseMachine::Get2DSimplexNoiseValue(float x, float y, float res) +{ + x /= res; + y /= res; + + sum = (x + y) * SkewCoeff2D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y) * UnskewCoeff2D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + + if(unskewedDistToOrigin.x > unskewedDistToOrigin.y) + { + off1.x = 1; + off1.y = 0; + } + else + { + off1.x = 0; + off1.y = 1; + } + + d1 = - unskewedDistToOrigin; + + d2.x = d1.x + off1.x - UnskewCoeff2D; + d2.y = d1.y + off1.y - UnskewCoeff2D; + + d3.x = d1.x + 1.0 - 2 * UnskewCoeff2D; + d3.y = d1.y + 1.0 - 2 * UnskewCoeff2D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + + gi0 = perm[ii + perm[jj ]] & 7; + gi1 = perm[ii + off1.x + perm[jj + off1.y]] & 7; + gi2 = perm[ii + 1 + perm[jj + 1 ]] & 7; + + c1 = 0.5 - d1.x * d1.x - d1.y * d1.y; + c2 = 0.5 - d2.x * d2.x - d2.y * d2.y; + c3 = 0.5 - d3.x * d3.x - d3.y * d3.y; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient2[gi0][0] * d1.x + gradient2[gi0][1] * d1.y); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient2[gi1][0] * d2.x + gradient2[gi1][1] * d2.y); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient2[gi2][0] * d3.x + gradient2[gi2][1] * d3.y); + + return (n1+n2+n3)*70; +} + +float NzNoiseMachine::Get3DSimplexNoiseValue(float x, float y, float z, float res) +{ + x /= res; + y /= res; + z /= res; + + sum = (x + y + z) * SkewCoeff3D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + skewedCubeOrigin.z = fastfloor(z + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y + skewedCubeOrigin.z) * UnskewCoeff3D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + unskewedCubeOrigin.z = skewedCubeOrigin.z - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + unskewedDistToOrigin.z = z - unskewedCubeOrigin.z; + + if(unskewedDistToOrigin.x >= unskewedDistToOrigin.y) + { + if(unskewedDistToOrigin.y >= unskewedDistToOrigin.z) + { + off1.x = 1; + off1.y = 0; + off1.z = 0; + off2.x = 1; + off2.y = 1; + off2.z = 0; + } + else if(unskewedDistToOrigin.x >= unskewedDistToOrigin.z) + { + off1.x = 1; + off1.y = 0; + off1.z = 0; + off2.x = 1; + off2.y = 0; + off2.z = 1; + } + else + { + off1.x = 0; + off1.y = 0; + off1.z = 1; + off2.x = 1; + off2.y = 0; + off2.z = 1; + } + } + else + { + if(unskewedDistToOrigin.y < unskewedDistToOrigin.z) + { + off1.x = 0; + off1.y = 0; + off1.z = 1; + off2.x = 0; + off2.y = 1; + off2.z = 1; + } + else if(unskewedDistToOrigin.x < unskewedDistToOrigin.z) + { + off1.x = 0; + off1.y = 1; + off1.z = 0; + off2.x = 0; + off2.y = 1; + off2.z = 1; + } + else + { + off1.x = 0; + off1.y = 1; + off1.z = 0; + off2.x = 1; + off2.y = 1; + off2.z = 0; + } + } + + d1 = unskewedDistToOrigin; + + d2.x = d1.x - off1.x + UnskewCoeff3D; + d2.y = d1.y - off1.y + UnskewCoeff3D; + d2.z = d1.z - off1.z + UnskewCoeff3D; + + d3.x = d1.x - off2.x + 2*UnskewCoeff3D; + d3.y = d1.y - off2.y + 2*UnskewCoeff3D; + d3.z = d1.z - off2.z + 2*UnskewCoeff3D; + + d4.x = d1.x - 1.0 + 3*UnskewCoeff3D; + d4.y = d1.y - 1.0 + 3*UnskewCoeff3D; + d4.z = d1.z - 1.0 + 3*UnskewCoeff3D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + kk = skewedCubeOrigin.z & 255; + + gi0 = perm[ii + perm[jj + perm[kk ]]] % 12; + gi1 = perm[ii + off1.x + perm[jj + off1.y + perm[kk + off1.z]]] % 12; + gi2 = perm[ii + off2.x + perm[jj + off2.y + perm[kk + off2.z]]] % 12; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 ]]] % 12; + + c1 = 0.6 - d1.x * d1.x - d1.y * d1.y - d1.z * d1.z; + c2 = 0.6 - d2.x * d2.x - d2.y * d2.y - d2.z * d2.z; + c3 = 0.6 - d3.x * d3.x - d3.y * d3.y - d3.z * d3.z; + c4 = 0.6 - d4.x * d4.x - d4.y * d4.y - d4.z * d4.z; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient3[gi0][0] * d1.x + gradient3[gi0][1] * d1.y + gradient3[gi0][2] * d1.z); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient3[gi1][0] * d2.x + gradient3[gi1][1] * d2.y + gradient3[gi1][2] * d2.z); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient3[gi2][0] * d3.x + gradient3[gi2][1] * d3.y + gradient3[gi2][2] * d3.z); + + if(c4 < 0) + n4 = 0; + else + n4 = c4*c4*c4*c4*(gradient3[gi3][0] * d4.x + gradient3[gi3][1] * d4.y + gradient3[gi3][2] * d4.z); + + return (n1+n2+n3+n4)*32; +} + +float NzNoiseMachine::Get4DSimplexNoiseValue(float x, float y, float z, float w, float res) +{ + x /= res; + y /= res; + z /= res; + w /= res; + + sum = (x + y + z + w) * SkewCoeff4D; + skewedCubeOrigin.x = fastfloor(x + sum); + skewedCubeOrigin.y = fastfloor(y + sum); + skewedCubeOrigin.z = fastfloor(z + sum); + skewedCubeOrigin.w = fastfloor(w + sum); + + sum = (skewedCubeOrigin.x + skewedCubeOrigin.y + skewedCubeOrigin.z + skewedCubeOrigin.w) * UnskewCoeff4D; + unskewedCubeOrigin.x = skewedCubeOrigin.x - sum; + unskewedCubeOrigin.y = skewedCubeOrigin.y - sum; + unskewedCubeOrigin.z = skewedCubeOrigin.z - sum; + unskewedCubeOrigin.w = skewedCubeOrigin.w - sum; + + unskewedDistToOrigin.x = x - unskewedCubeOrigin.x; + unskewedDistToOrigin.y = y - unskewedCubeOrigin.y; + unskewedDistToOrigin.z = z - unskewedCubeOrigin.z; + unskewedDistToOrigin.w = w - unskewedCubeOrigin.w; + + c1 = (unskewedDistToOrigin.x > unskewedDistToOrigin.y) ? 32 : 0; + c2 = (unskewedDistToOrigin.x > unskewedDistToOrigin.z) ? 16 : 0; + c3 = (unskewedDistToOrigin.y > unskewedDistToOrigin.z) ? 8 : 0; + c4 = (unskewedDistToOrigin.x > unskewedDistToOrigin.w) ? 4 : 0; + c5 = (unskewedDistToOrigin.y > unskewedDistToOrigin.w) ? 2 : 0; + c6 = (unskewedDistToOrigin.z > unskewedDistToOrigin.w) ? 1 : 0; + c = c1 + c2 + c3 + c4 + c5 + c6; + + off1.x = lookupTable4D[c][0] >= 3 ? 1 : 0; + off1.y = lookupTable4D[c][1] >= 3 ? 1 : 0; + off1.z = lookupTable4D[c][2] >= 3 ? 1 : 0; + off1.w = lookupTable4D[c][3] >= 3 ? 1 : 0; + + off2.x = lookupTable4D[c][0] >= 2 ? 1 : 0; + off2.y = lookupTable4D[c][1] >= 2 ? 1 : 0; + off2.z = lookupTable4D[c][2] >= 2 ? 1 : 0; + off2.w = lookupTable4D[c][3] >= 2 ? 1 : 0; + + off3.x = lookupTable4D[c][0] >= 1 ? 1 : 0; + off3.y = lookupTable4D[c][1] >= 1 ? 1 : 0; + off3.z = lookupTable4D[c][2] >= 1 ? 1 : 0; + off3.w = lookupTable4D[c][3] >= 1 ? 1 : 0; + + d1 = unskewedDistToOrigin; + + d2.x = d1.x - off1.x + UnskewCoeff4D; + d2.y = d1.y - off1.y + UnskewCoeff4D; + d2.z = d1.z - off1.z + UnskewCoeff4D; + d2.w = d1.w - off1.w + UnskewCoeff4D; + + d3.x = d1.x - off2.x + 2*UnskewCoeff4D; + d3.y = d1.y - off2.y + 2*UnskewCoeff4D; + d3.z = d1.z - off2.z + 2*UnskewCoeff4D; + d3.w = d1.w - off2.w + 2*UnskewCoeff4D; + + d4.x = d1.x - off3.x + 3*UnskewCoeff4D; + d4.y = d1.y - off3.y + 3*UnskewCoeff4D; + d4.z = d1.z - off3.z + 3*UnskewCoeff4D; + d4.w = d1.w - off3.w + 3*UnskewCoeff4D; + + d5.x = d1.x - 1.0 + 4*UnskewCoeff4D; + d5.y = d1.y - 1.0 + 4*UnskewCoeff4D; + d5.z = d1.z - 1.0 + 4*UnskewCoeff4D; + d5.w = d1.w - 1.0 + 4*UnskewCoeff4D; + + ii = skewedCubeOrigin.x & 255; + jj = skewedCubeOrigin.y & 255; + kk = skewedCubeOrigin.z & 255; + ll = skewedCubeOrigin.w & 255; + + gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] & 31; + gi1 = perm[ii + off1.x + perm[jj + off1.y + perm[kk + off1.z + perm[ll + off1.w]]]] & 31; + gi2 = perm[ii + off2.x + perm[jj + off2.y + perm[kk + off2.z + perm[ll + off2.w]]]] & 31; + gi3 = perm[ii + off3.x + perm[jj + off3.y + perm[kk + off3.z + perm[ll + off3.w]]]] & 31; + gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32; + + c1 = 0.6 - d1.x*d1.x - d1.y*d1.y - d1.z*d1.z - d1.w*d1.w; + c2 = 0.6 - d2.x*d2.x - d2.y*d2.y - d2.z*d2.z - d2.w*d2.w; + c3 = 0.6 - d3.x*d3.x - d3.y*d3.y - d3.z*d3.z - d3.w*d3.w; + c4 = 0.6 - d4.x*d4.x - d4.y*d4.y - d4.z*d4.z - d4.w*d4.w; + c5 = 0.6 - d5.x*d5.x - d5.y*d5.y - d5.z*d5.z - d5.w*d5.w; + + if(c1 < 0) + n1 = 0; + else + n1 = c1*c1*c1*c1*(gradient4[gi0][0]*d1.x + gradient4[gi0][1]*d1.y + gradient4[gi0][2]*d1.z + gradient4[gi0][3]*d1.w); + + if(c2 < 0) + n2 = 0; + else + n2 = c2*c2*c2*c2*(gradient4[gi1][0]*d2.x + gradient4[gi1][1]*d2.y + gradient4[gi1][2]*d2.z + gradient4[gi1][3]*d2.w); + + if(c3 < 0) + n3 = 0; + else + n3 = c3*c3*c3*c3*(gradient4[gi2][0]*d3.x + gradient4[gi2][1]*d3.y + gradient4[gi2][2]*d3.z + gradient4[gi2][3]*d3.w); + + if(c4 < 0) + n4 = 0; + else + n4 = c4*c4*c4*c4*(gradient4[gi3][0]*d4.x + gradient4[gi3][1]*d4.y + gradient4[gi3][2]*d4.z + gradient4[gi3][3]*d4.w); + + if(c5 < 0) + n5 = 0; + else + n5 = c5*c5*c5*c5*(gradient4[gi4][0]*d5.x + gradient4[gi4][1]*d5.y + gradient4[gi4][2]*d5.z + gradient4[gi4][3]*d5.w); + + return (n1+n2+n3+n4+n5)*27.0; +} + +//------------------------------ CELL ------------------------------ + +float NzNoiseMachine::Get2DCellNoiseValue(float x, float y, float res) +{ + return 0; +} + +float NzNoiseMachine::Get3DCellNoiseValue(float x, float y, float z, float res) +{ + x /= res; + y /= res; + z /= res; + + x0 = static_cast(x); + y0 = static_cast(y); + z0 = static_cast(z); + + return (this->JenkinsHash(x0,y0,z0) & 255); +} +float NzNoiseMachine::Get4DCellNoiseValue(float x, float y, float z, float w, float res) +{ + x /= res; + y /= res; + z /= res; + w /= res; + + x0 = static_cast(x) & 255; + y0 = static_cast(y) & 255; + z0 = static_cast(z) & 255; + w0 = static_cast(w) & 255; + + return 0; +} + +//------------------------------ FBM ------------------------------ + +float NzNoiseMachine::Get2DFBMNoiseValue(float x, float y, float res) +{ + value = 0.0; + RecomputeExponentArray(); + + for (int i(0); i < m_octaves; ++i) + { + value += Get2DPerlinNoiseValue(x,y,res) * exponent_array[i]; + x *= m_lacunarity; + y *= m_lacunarity; + } + remainder = m_octaves - (int)m_octaves; + + if(remainder != 0) + value += remainder * Get2DSimplexNoiseValue(x,y,res) * exponent_array[(int)m_octaves-1]; + + return value * m_sum; +} + +float NzNoiseMachine::Get3DFBMNoiseValue(float x, float y, float z, float res) +{ + value = 0.0; + RecomputeExponentArray(); + + for(int i(0); i < m_octaves; ++i) + { + value += Get3DSimplexNoiseValue(x,y,z,res) * exponent_array[i]; + x *= m_lacunarity; + y *= m_lacunarity; + z *= m_lacunarity; + } + remainder = m_octaves - (int)m_octaves; + + if(remainder != 0) + value += remainder * Get3DSimplexNoiseValue(x,y,z,res) * exponent_array[(int)m_octaves-1]; + + return value * m_sum; +} + +//------------------------------ HYBRID MULTIFRACTAL ------------------------------ + +float NzNoiseMachine::Get2DHybridMultiFractalNoiseValue(float x, float y, float res) +{ + float result, signal, weight, remainder; + float offset = 1; + + RecomputeExponentArray(); + + result = (Get2DSimplexNoiseValue(x,y,res) + offset) * exponent_array[0]; + weight = result; + + x *= m_lacunarity; + y *= m_lacunarity; + + for(int i(1) ; i < m_octaves; ++i) + { + if(weight > 1.0) + weight = 1.0; + + signal = (Get2DSimplexNoiseValue(x,y,res) + offset) * exponent_array[i]; + result += weight * signal; + + weight *= signal; + + x *= m_lacunarity; + y *= m_lacunarity; + + } + + remainder = m_octaves - (int)m_octaves; + + if(remainder != 0) + result += remainder * Get2DSimplexNoiseValue(x,y,res) * exponent_array[(int)m_octaves-1]; + + return result; + +} + +float NzNoiseMachine::Get3DHybridMultiFractalNoiseValue(float x, float y, float z, float res) +{ + float result, signal, weight, remainder; + float offset = 1; + + RecomputeExponentArray(); + + result = (Get3DSimplexNoiseValue(x,y,z,res) + offset) * exponent_array[0]; + weight = result; + + x *= m_lacunarity; + y *= m_lacunarity; + + for(int i(1) ; i < m_octaves; ++i) + { + + if(weight > 1.0) + weight = 1.0; + + signal = ( Get3DSimplexNoiseValue(x,y,z,res) + offset ) * exponent_array[i]; + result += weight * signal; + + weight *= signal; + + x *= m_lacunarity; + y *= m_lacunarity; + + } + + remainder = m_octaves - (int)m_octaves; + + if(remainder != 0) + result += remainder * Get3DSimplexNoiseValue(x,y,z,res) * exponent_array[(int)m_octaves-1]; + + return result; +} + +#include