diff --git a/include/Nazara/Noise/Perlin2D.hpp b/include/Nazara/Noise/Perlin2D.hpp new file mode 100644 index 000000000..1633c10fb --- /dev/null +++ b/include/Nazara/Noise/Perlin2D.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 PERLIN2D_H +#define PERLIN2D_H + +#include +#include +#include + +template class 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; + T nx, ny; + T tmp; + NzVector2 temp; +}; + +typedef NzPerlin2D NzPerlin2Df; +typedef NzPerlin2D NzPerlin2Dd; + +#include + +#endif // PERLIN2D_H + diff --git a/include/Nazara/Noise/Perlin2D.inl b/include/Nazara/Noise/Perlin2D.inl new file mode 100644 index 000000000..df06d2679 --- /dev/null +++ b/include/Nazara/Noise/Perlin2D.inl @@ -0,0 +1,65 @@ +// 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 unit = 1.0/sqrt(2); + T grad2Temp[][2] = {{unit,unit},{-unit,unit},{unit,-unit},{-unit,-unit}, + {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) +{ + nx = x/res; + ny = y/res; + + x0 = static_cast(nx); + y0 = static_cast(ny); + + 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 = nx-x0; + temp.y = ny-y0; + s = gradient2[gi0][0]*temp.x + gradient2[gi0][1]*temp.y; + + temp.x = nx-(x0+1); + temp.y = ny-y0; + t = gradient2[gi1][0]*temp.x + gradient2[gi1][1]*temp.y; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u = gradient2[gi2][0]*temp.x + gradient2[gi2][1]*temp.y; + + temp.x = nx-(x0+1); + temp.y = ny-(y0+1); + v = gradient2[gi3][0]*temp.x + gradient2[gi3][1]*temp.y; + + tmp = nx-x0; + Cx = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + Li1 = s + Cx*(t-s); + Li2 = u + Cx*(v-u); + + tmp = ny - y0; + Cy = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + return Li1 + Cy*(Li2-Li1); +} + diff --git a/include/Nazara/Noise/Perlin3D.hpp b/include/Nazara/Noise/Perlin3D.hpp new file mode 100644 index 000000000..ca01b891d --- /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_H +#define PERLIN3D_H + +#include +#include +#include + +template class 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_H diff --git a/include/Nazara/Noise/Perlin3D.inl b/include/Nazara/Noise/Perlin3D.inl new file mode 100644 index 000000000..8bf832dfe --- /dev/null +++ b/include/Nazara/Noise/Perlin3D.inl @@ -0,0 +1,101 @@ +// 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) +{ + nx = x/res; + ny = y/res; + nz = z/res; + + x0 = static_cast(nx); + y0 = static_cast(ny); + z0 = static_cast(nz); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk]]] % 16; + gi1 = perm[ii + 1 + perm[jj + perm[kk]]] % 16; + gi2 = perm[ii + perm[jj + 1 + perm[kk]]] % 16; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk]]] % 16; + + gi4 = perm[ii + perm[jj + perm[kk + 1]]] % 16; + gi5 = perm[ii + 1 + perm[jj + perm[kk + 1]]] % 16; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1]]] % 16; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 16; + + temp.x = nx-x0; + temp.y = ny-y0; + temp.z = nz-z0; + s[0] = gradient3[gi0][0]*temp.x + gradient3[gi0][1]*temp.y + gradient3[gi0][2]*temp.z; + + temp.x = nx-(x0+1); + temp.y = ny-y0; + t[0] = gradient3[gi1][0]*temp.x + gradient3[gi1][1]*temp.y + gradient3[gi1][2]*temp.z; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[0] = gradient3[gi2][0]*temp.x + gradient3[gi2][1]*temp.y + gradient3[gi2][2]*temp.z; + + temp.x = nx-(x0+1); + temp.y = ny-(y0+1); + v[0] = gradient3[gi3][0]*temp.x + gradient3[gi3][1]*temp.y + gradient3[gi3][2]*temp.z; + + temp.x = nx-x0; + temp.y = ny-y0; + temp.z = nz-(z0+1); + s[1] = gradient3[gi4][0]*temp.x + gradient3[gi4][1]*temp.y + gradient3[gi4][2]*temp.z; + + temp.x = nx-(x0+1); + temp.y = ny-y0; + t[1] = gradient3[gi5][0]*temp.x + gradient3[gi5][1]*temp.y + gradient3[gi5][2]*temp.z; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[1] = gradient3[gi6][0]*temp.x + gradient3[gi6][1]*temp.y + gradient3[gi6][2]*temp.z; + + temp.x = nx-(x0+1); + temp.y = ny-(y0+1); + v[1] = gradient3[gi7][0]*temp.x + gradient3[gi7][1]*temp.y + gradient3[gi7][2]*temp.z; + + tmp = nx-x0; + Cx = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + 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]); + + tmp = ny-y0; + Cy = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + Li5 = Li1 + Cy*(Li2-Li1); + Li6 = Li3 + Cy*(Li4-Li3); + + tmp = nz-z0; + Cz = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + return Li5 + Cz*(Li6-Li5); +} diff --git a/include/Nazara/Noise/Perlin4D.hpp b/include/Nazara/Noise/Perlin4D.hpp new file mode 100644 index 000000000..c0af6feb1 --- /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_H +#define PERLIN4D_H + +#include +#include +#include + +template class 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_H diff --git a/include/Nazara/Noise/Perlin4D.inl b/include/Nazara/Noise/Perlin4D.inl new file mode 100644 index 000000000..4b51c127d --- /dev/null +++ b/include/Nazara/Noise/Perlin4D.inl @@ -0,0 +1,167 @@ +// 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 = static_cast(nx); + y0 = static_cast(ny); + z0 = static_cast(nz); + w0 = static_cast(nw); + + ii = x0 & 255; + jj = y0 & 255; + kk = z0 & 255; + ll = w0 & 255; + + gi0 = perm[ii + perm[jj + perm[kk + perm[ll ]]]] % 32; + gi1 = perm[ii + 1 + perm[jj + perm[kk + perm[ll ]]]] % 32; + gi2 = perm[ii + perm[jj + 1 + perm[kk + perm[ll ]]]] % 32; + gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll ]]]] % 32; + + gi4 = perm[ii + perm[jj + + perm[kk + 1 + perm[ll ]]]] % 32; + gi5 = perm[ii + 1 + perm[jj + + perm[kk + 1 + perm[ll ]]]] % 32; + gi6 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] % 32; + gi7 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll ]]]] % 32; + + gi8 = perm[ii + perm[jj + perm[kk + perm[ll + 1]]]] % 32; + gi9 = perm[ii + 1 + perm[jj + perm[kk + perm[ll + 1]]]] % 32; + gi10 = perm[ii + perm[jj + 1 + perm[kk + perm[ll + 1]]]] % 32; + gi11 = perm[ii + 1 + perm[jj + 1 + perm[kk + perm[ll + 1]]]] % 32; + + gi12 = perm[ii + perm[jj + perm[kk + 1 + perm[ll + 1]]]] % 32; + gi13 = perm[ii + 1 + perm[jj + perm[kk + 1 + perm[ll + 1]]]] % 32; + gi14 = perm[ii + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32; + gi15 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32; + + temp.x = nx-x0; + temp.y = ny-y0; + temp.z = nz-z0; + temp.w = nw-w0; + 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); + temp.y = ny-y0; + t[0] = gradient4[gi1][0]*temp.x + gradient4[gi1][1]*temp.y + gradient4[gi1][2]*temp.z + gradient4[gi1][3]*temp.w; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[0] = gradient4[gi2][0]*temp.x + gradient4[gi2][1]*temp.y + gradient4[gi2][2]*temp.z + gradient4[gi2][3]*temp.w; + + temp.x = nx-(x0+1); + 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; + 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); + temp.y = ny-y0; + t[1] = gradient4[gi5][0]*temp.x + gradient4[gi5][1]*temp.y + gradient4[gi5][2]*temp.z + gradient4[gi5][3]*temp.w; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[1] = gradient4[gi6][0]*temp.x + gradient4[gi6][1]*temp.y + gradient4[gi6][2]*temp.z + gradient4[gi6][3]*temp.w; + + temp.x = nx-(x0+1); + 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; + 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); + temp.y = ny-y0; + t[2] = gradient4[gi9][0]*temp.x + gradient4[gi9][1]*temp.y + gradient4[gi9][2]*temp.z + gradient4[gi9][3]*temp.w; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[2] = gradient4[gi10][0]*temp.x + gradient4[gi10][1]*temp.y + gradient4[gi10][2]*temp.z + gradient4[gi10][3]*temp.w; + + temp.x = nx-(x0+1); + 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; + 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); + temp.y = ny-y0; + t[3] = gradient4[gi13][0]*temp.x + gradient4[gi13][1]*temp.y + gradient4[gi13][2]*temp.z + gradient4[gi13][3]*temp.w; + + temp.x = nx-x0; + temp.y = ny-(y0+1); + u[3] = gradient4[gi14][0]*temp.x + gradient4[gi14][1]*temp.y + gradient4[gi14][2]*temp.z + gradient4[gi14][3]*temp.w; + + temp.x = nx-(x0+1); + 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; + + tmp = nx-x0; + Cx = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + 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]); + + tmp = ny-y0; + Cy = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + Li9 = Li1 + Cy*(Li2-Li1); + Li10 = Li3 + Cy*(Li4-Li3); + Li11 = Li5 + Cy*(Li6-Li5); + Li12 = Li7 + Cy*(Li8-Li7); + + tmp = nz-z0; + Cz = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + Li13 = Li9 + Cz*(Li10-Li9); + Li14 = Li11 + Cz*(Li12-Li11); + + tmp = nw-w0; + Cw = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); + + return Li13 + Cw*(Li14-Li13); +} diff --git a/include/Nazara/Noise/Simplex2D.hpp b/include/Nazara/Noise/Simplex2D.hpp new file mode 100644 index 000000000..79434b471 --- /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_H +#define SIMPLEX2D_H + +#include +#include +#include + + +template class 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 Origin,off1; + T n1,n2,n3; + T c1,c2,c3; + T gradient2[8][2]; + T UnskewCoeff2D; + T SkewCoeff2D; + NzVector2 A, IsoOriginDist; + NzVector2 d1,d2,d3; + + +}; + +typedef NzSimplex2D NzSimplex2Df; +typedef NzSimplex2D NzSimplex2Dd; + +#include + +#endif // SIMPLEX2D_H + diff --git a/include/Nazara/Noise/Simplex2D.inl b/include/Nazara/Noise/Simplex2D.inl new file mode 100644 index 000000000..129501617 --- /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 unit = 1.0/sqrt(2); + T grad2Temp[][2] = {{unit,unit},{-unit,unit},{unit,-unit},{-unit,-unit}, + {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; + + Origin.x = fastfloor(x + (x + y) * SkewCoeff2D); + Origin.y = fastfloor(y + (x + y) * SkewCoeff2D); + + A.x = Origin.x - (Origin.x + Origin.y) * UnskewCoeff2D; + A.y = Origin.y - (Origin.x + Origin.y) * UnskewCoeff2D; + + IsoOriginDist.x = x - A.x; + IsoOriginDist.y = y - A.y; + + if(IsoOriginDist.x > IsoOriginDist.y) + { + off1.x = 1; + off1.y = 0; + } + else + { + off1.x = 0; + off1.y = 1; + } + + d1.x = A.x - x; + d1.y = A.y - y; + + 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 = Origin.x & 255; + jj = Origin.y & 255; + + gi0 = perm[ii + perm[jj]] % 8; + gi1 = perm[ii + off1.x + perm[jj + off1.y]] % 8; + gi2 = perm[ii + 1 + perm[jj + 1]] % 8; + + n1 = gradient2[gi0][0] * d1.x + gradient2[gi0][1] * d1.y; + n2 = gradient2[gi1][0] * d2.x + gradient2[gi1][1] * d2.y; + n3 = gradient2[gi2][0] * d3.x + gradient2[gi2][1] * d3.y; + + 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) + c1 = 0; + if(c2 < 0) + c2 = 0; + if(c3 < 0) + c3 = 0; + + n1 = c1*c1*c1*n1; + n2 = c2*c2*c2*n2; + n3 = c3*c3*c3*n3; + + return (n1+n2+n3)*23.2; +} +