Perlin 2,3,4 optimisations + Simplex imp fixed + improved architecture

This commit is contained in:
Remi Beges
2012-06-05 22:00:09 +02:00
parent 4d975fa67e
commit 5d7f8acbc8
14 changed files with 307 additions and 317 deletions

View File

@@ -0,0 +1,36 @@
// 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_H
#define COMPLEXNOISEBASE_H
#include <Nazara/Prerequesites.hpp>
//#include <Nazara/Noise/NoiseBase.hpp>
#include "NoiseBase.hpp"
class NzComplexNoiseBase : public NzNoiseBase
{
public:
NzComplexNoiseBase();
~NzComplexNoiseBase();
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_H

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2012 AUTHORS
// 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

View File

@@ -18,6 +18,7 @@ class NzNoiseBase
void SetNewSeed(int seed);
int GetUniformRandomValue();
void ShufflePermutationTable();
int fastfloor(float n);
int JenkinsHash(int a, int b, int c);
protected:

View File

@@ -8,22 +8,20 @@
#define NOISEMACHINE_HPP
#include <Nazara/Prerequesites.hpp>
//#include <Nazara/Noise/NoiseBase.hpp>
#include "NoiseBase.hpp"
//#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include "ComplexNoiseBase.hpp"
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
//TODO : tableau de gradients en float au lieu de int ? Ou condition ternaires ?
// utiliser fastfloor partout
// utiliser copies paramètres pour économiser mémoire
// améliorer le mélange de la table de perm
//TODO : AMELIORER MELANGE TABLE PERMUTATION
// PB MULTIPLES APPELS SHUFFLEPERMUTATIONTABLE()
class NzNoiseMachine : public NzNoiseBase
class NzNoiseMachine : public NzComplexNoiseBase
{
public:
NzNoiseMachine(int seed = 0);
~NzNoiseMachine();
~NzNoiseMachine() = default;
float Get2DPerlinNoiseValue (float x, float y, float res);
float Get3DPerlinNoiseValue (float x, float y, float z, float res);
@@ -37,11 +35,6 @@ class NzNoiseMachine : public NzNoiseBase
float Get3DCellNoiseValue(float x, float y, float z, float res);
float Get4DCellNoiseValue(float x, float y, float z, float w, float res);
void SetLacunarity(float lacunarity);
void SetHurstParameter(float h);
void SetOctavesNumber(float octaves);
void RecomputeExponentArray();
float Get2DFBMNoiseValue(float x, float y, float res);
float Get3DFBMNoiseValue(float x, float y, float z, float res);
@@ -59,11 +52,11 @@ class NzNoiseMachine : public NzNoiseBase
//----------------------- Simplex variables --------------------------------------
float n1, n2, n3, n4, n5;
NzVector4f A;
NzVector4f d1,d2,d3,d4,d5;
NzVector4f IsoOriginDist;
NzVector4f d1,d2,d3,d4,d5,unskewedCubeOrigin,unskewedDistToOrigin;
NzVector4i off1, off2,off3,skewedCubeOrigin;
NzVector4f A,IsoOriginDist;
NzVector4i Origin;
NzVector4i off1, off2,off3;
int ii,jj,kk,ll;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15;
@@ -86,20 +79,14 @@ class NzNoiseMachine : public NzNoiseBase
float s[4],t[4],u[4],v[4];
float Cx, Cy, Cz, Cw;
NzVector4f temp;
float nx,ny,nz,nw;
float tmp;
//---------------------- Complex Noise Variables --------------------------------
float m_lacunarity;
float m_hurst;
float m_octaves;
bool m_parametersModified;
float exponent_array[30];
bool first;
float value;
float remainder;
float m_sum;
float smax;
float smin;

View File

@@ -27,8 +27,6 @@ template <typename T> class NzPerlin2D : public NzNoiseBase
T s,t,u,v;
T Cx,Cy;
T Li1, Li2;
T nx, ny;
T tmp;
NzVector2<T> temp;
};

View File

@@ -9,9 +9,8 @@
template <typename T>
NzPerlin2D<T>::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}};
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)
@@ -21,11 +20,11 @@ NzPerlin2D<T>::NzPerlin2D()
template <typename T>
T NzPerlin2D<T>::GetValue(T x, T y, T res)
{
nx = x/res;
ny = y/res;
x /= res;
y /= res;
x0 = static_cast<int>(nx);
y0 = static_cast<int>(ny);
x0 = fastfloor(x);
y0 = fastfloor(y);
ii = x0 & 255;
jj = y0 & 255;
@@ -35,31 +34,26 @@ T NzPerlin2D<T>::GetValue(T x, T y, T res)
gi2 = perm[ii + perm[jj + 1]] & 7;
gi3 = perm[ii + 1 + perm[jj + 1]] & 7;
temp.x = nx-x0;
temp.y = ny-y0;
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 = nx-(x0+1);
temp.y = ny-y0;
temp.x = x-(x0+1);
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);
temp.y = y-(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);
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);
tmp = ny - y0;
Cy = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10);
return Li1 + Cy*(Li2-Li1);
}

View File

@@ -24,78 +24,68 @@ NzPerlin3D<T>::NzPerlin3D()
template <typename T>
T NzPerlin3D<T>::GetValue(T x, T y, T z, T res)
{
nx = x/res;
ny = y/res;
nz = z/res;
x /= res;
y /= res;
z /= res;
x0 = static_cast<int>(nx);
y0 = static_cast<int>(ny);
z0 = static_cast<int>(nz);
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]]] % 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;
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]]] % 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;
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);
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;
temp.x = x-(x0+1);
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);
temp.y = y-(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);
temp.x = x-x0;
u[0] = gradient3[gi2][0]*temp.x + gradient3[gi2][1]*temp.y + gradient3[gi2][2]*temp.z;
temp.x = x-x0;
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 = nx-(x0+1);
temp.y = ny-y0;
temp.x = x-(x0+1);
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);
temp.y = y-(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);
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]);
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);
}

View File

@@ -34,10 +34,10 @@ T NzPerlin4D<T>::GetValue(T x, T y, T z, T w, T res)
nz = z/res;
nw = w/res;
x0 = static_cast<int>(nx);
y0 = static_cast<int>(ny);
z0 = static_cast<int>(nz);
w0 = static_cast<int>(nw);
x0 = fastfloor(nx);
y0 = fastfloor(ny);
z0 = fastfloor(nz);
w0 = fastfloor(nw);
ii = x0 & 255;
jj = y0 & 255;

View File

@@ -23,13 +23,13 @@ template <typename T> class NzSimplex2D : public NzNoiseBase
private:
int ii,jj;
int gi0,gi1,gi2;
NzVector2i Origin,off1;
NzVector2i skewedCubeOrigin,off1;
T n1,n2,n3;
T c1,c2,c3;
T gradient2[8][2];
T UnskewCoeff2D;
T SkewCoeff2D;
NzVector2<T> A, IsoOriginDist;
NzVector2<T> unskewedCubeOrigin, unskewedDistToOrigin;
NzVector2<T> d1,d2,d3;

View File

@@ -9,10 +9,8 @@
template <typename T>
NzSimplex2D<T>::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}};
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)
@@ -28,16 +26,16 @@ T NzSimplex2D<T>::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);
skewedCubeOrigin.x = fastfloor(x + (x + y) * SkewCoeff2D);
skewedCubeOrigin.y = fastfloor(y + (x + y) * SkewCoeff2D);
A.x = Origin.x - (Origin.x + Origin.y) * UnskewCoeff2D;
A.y = Origin.y - (Origin.x + Origin.y) * UnskewCoeff2D;
unskewedCubeOrigin.x = skewedCubeOrigin.x - (skewedCubeOrigin.x + skewedCubeOrigin.y) * UnskewCoeff2D;
unskewedCubeOrigin.y = skewedCubeOrigin.y - (skewedCubeOrigin.x + skewedCubeOrigin.y) * UnskewCoeff2D;
IsoOriginDist.x = x - A.x;
IsoOriginDist.y = y - A.y;
unskewedDistToOrigin.x = x - unskewedCubeOrigin.x;
unskewedDistToOrigin.y = y - unskewedCubeOrigin.y;
if(IsoOriginDist.x > IsoOriginDist.y)
if(unskewedDistToOrigin.x > unskewedDistToOrigin.y)
{
off1.x = 1;
off1.y = 0;
@@ -48,8 +46,9 @@ T NzSimplex2D<T>::GetValue(T x, T y, T res)
off1.y = 1;
}
d1.x = A.x - x;
d1.y = A.y - y;
d1.x = unskewedCubeOrigin.x - x;
d1.y = unskewedCubeOrigin.y - y;
d2.x = d1.x + off1.x - UnskewCoeff2D;
d2.y = d1.y + off1.y - UnskewCoeff2D;
@@ -57,32 +56,32 @@ T NzSimplex2D<T>::GetValue(T x, T y, T res)
d3.x = d1.x + 1.0 - 2 * UnskewCoeff2D;
d3.y = d1.y + 1.0 - 2 * UnskewCoeff2D;
ii = Origin.x & 255;
jj = Origin.y & 255;
ii = skewedCubeOrigin.x & 255;
jj = skewedCubeOrigin.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;
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)
c1 = 0;
n1 = 0;
else
n1 = c1*c1*c1*c1*(gradient2[gi0][0] * d1.x + gradient2[gi0][1] * d1.y);
if(c2 < 0)
c2 = 0;
n2 = 0;
else
n2 = c2*c2*c2*c2*(gradient2[gi1][0] * d2.x + gradient2[gi1][1] * d2.y);
if(c3 < 0)
c3 = 0;
n3 = 0;
else
n3 = c3*c3*c3*c3*(gradient2[gi2][0] * d3.x + gradient2[gi2][1] * d3.y);
n1 = c1*c1*c1*n1;
n2 = c2*c2*c2*n2;
n3 = c3*c3*c3*n3;
return (n1+n2+n3)*23.2;
return (n1+n2+n3)*70;
}