Merge branch 'master' into vulkan

Former-commit-id: 4c9dc7fdf6ae85f2d8328ab464cb89c8a64da64c [formerly d5a7fd793bc0023b9c6837eb5db26749aed83612]
Former-commit-id: 9dc53b8ec16f2554c9bc2fa5fb62b5c9aa2d0e07
This commit is contained in:
Lynix 2016-06-19 15:06:23 +02:00
commit 6344268b4b
60 changed files with 1378 additions and 2037 deletions

View File

@ -51,7 +51,7 @@ namespace Ndk
BaseComponent& component = *m_components[index].get();
component.SetEntity(this);
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[i]->OnComponentAttached(component);
@ -73,8 +73,8 @@ namespace Ndk
void Entity::RemoveAllComponents()
{
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
RemoveComponent(i);
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
RemoveComponent(static_cast<ComponentIndex>(i));
NazaraAssert(m_componentBits.TestNone(), "All components should be gone");
@ -90,7 +90,7 @@ namespace Ndk
{
// On récupère le component et on informe les composants du détachement
BaseComponent& component = *m_components[index].get();
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[i]->OnComponentDetached(component);
@ -114,7 +114,7 @@ namespace Ndk
void Entity::Destroy()
{
// On informe chaque système
for (SystemIndex index = m_systemBits.FindFirst(); index != m_systemBits.npos; index = m_systemBits.FindNext(index))
for (std::size_t index = m_systemBits.FindFirst(); index != m_systemBits.npos; index = m_systemBits.FindNext(index))
{
if (m_world->HasSystem(index))
{

View File

@ -94,7 +94,7 @@ namespace Ndk
void World::Update()
{
// Gestion des entités tuées depuis le dernier appel
for (unsigned int i = m_killedEntities.FindFirst(); i != m_killedEntities.npos; i = m_killedEntities.FindNext(i))
for (std::size_t i = m_killedEntities.FindFirst(); i != m_killedEntities.npos; i = m_killedEntities.FindNext(i))
{
EntityBlock& block = m_entities[i];
Entity& entity = block.entity;
@ -127,7 +127,7 @@ namespace Ndk
m_killedEntities.Reset();
// Gestion des entités nécessitant une mise à jour de leurs systèmes
for (unsigned int i = m_dirtyEntities.FindFirst(); i != m_dirtyEntities.npos; i = m_dirtyEntities.FindNext(i))
for (std::size_t i = m_dirtyEntities.FindFirst(); i != m_dirtyEntities.npos; i = m_dirtyEntities.FindNext(i))
{
NazaraAssert(i < m_entities.size(), "Entity index out of range");

View File

@ -29,7 +29,7 @@ Nz::Vector3f DampedString(const Nz::Vector3f& currentPos, const Nz::Vector3f& ta
int main()
{
// Pour commencer, nous initialisons le SDK de Nazara, celui-ci va préparer le terrain en initialisant le moteur,
// Pour commencer, nous initialisons le SDK de Nazara, celui-ci va préparer le terrain en initialisant le moteur,
// les composants, systèmes, etc.
// NzInitializer est une classe RAII appelant Initialize dans son constructeur et Uninitialize dans son destructeur.
// Autrement dit, une fois ceci fait nous n'avons plus à nous soucier de la libération du moteur.
@ -101,7 +101,7 @@ int main()
// Le format OBJ ne précise aucune échelle pour ses données, contrairement à Nazara (une unité = un mètre en 3D).
// Comme le vaisseau est très grand (Des centaines de mètres de long), nous allons le rendre plus petit pour les besoins de la démo.
// Ce paramètre sert à indiquer la mise à l'échelle désirée lors du chargement du modèle.
params.mesh.scale.Set(0.01f); // Un centième de la taille originelle
params.mesh.matrix.MakeScale(Nz::Vector3f(0.01f)); // Un centième de la taille originelle
// Les UVs de ce fichier sont retournées (repère OpenGL, origine coin bas-gauche) par rapport à ce que le moteur attend (haut-gauche)
// Nous devons donc indiquer au moteur de les retourner lors du chargement
@ -156,7 +156,7 @@ int main()
// Nous devons donc lui rajouter les composants que nous voulons.
// Un NodeComponent donne à notre entité une position, rotation, échelle, et nous permet de l'attacher à d'autres entités (ce que nous ne ferons pas ici).
// Étant donné que par défaut, un NodeComponent se place en (0,0,0) sans rotation et avec une échelle de 1,1,1 et que cela nous convient,
// Étant donné que par défaut, un NodeComponent se place en (0,0,0) sans rotation et avec une échelle de 1,1,1 et que cela nous convient,
// nous n'avons pas besoin d'agir sur le composant créé.
spaceship->AddComponent<Ndk::NodeComponent>();

View File

@ -1,3 +1,4 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Mathematics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
@ -28,6 +29,10 @@
#define M_SQRT3 1.7320508075688772935274463
#endif
#ifndef M_SQRT5
#define M_SQRT5 2.23606797749979
#endif
namespace Nz
{
template<typename T> /*constexpr*/ T Approach(T value, T objective, T increment);

View File

@ -1,26 +0,0 @@
// Copyright (C) 2015 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_ABSTRACT2DNOISE_HPP
#define NAZARA_ABSTRACT2DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API Abstract2DNoise : public MappedNoiseBase
{
public:
virtual ~Abstract2DNoise();
float GetBasicValue(float x, float y);
float GetMappedValue(float x, float y);
virtual float GetValue(float x, float y, float resolution) = 0;
};
}
#endif // NAZARA_ABSTRACT2DNOISE_HPP

View File

@ -1,26 +0,0 @@
// Copyright (C) 2015 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_ABSTRACT3DNOISE_HPP
#define NAZARA_ABSTRACT3DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API Abstract3DNoise : public MappedNoiseBase
{
public:
virtual ~Abstract3DNoise();
float GetBasicValue(float x, float y, float z);
float GetMappedValue(float x, float y, float z);
virtual float GetValue(float x, float y, float z, float resolution) = 0;
};
}
#endif // NAZARA_ABSTRACT3DNOISE_HPP

View File

@ -1,26 +0,0 @@
// Copyright (C) 2015 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_ABSTRACT4DNOISE_HPP
#define NAZARA_ABSTRACT4DNOISE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MappedNoiseBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API Abstract4DNoise : public MappedNoiseBase
{
public:
virtual ~Abstract4DNoise();
float GetBasicValue(float x, float y, float z, float w);
float GetMappedValue(float x, float y, float z, float w);
virtual float GetValue(float x, float y, float z, float w, float resolution) = 0;
};
}
#endif // NAZARA_ABSTRACT4DNOISE_HPP

View File

@ -1,41 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef COMPLEXNOISEBASE_HPP
#define COMPLEXNOISEBASE_HPP
#include <Nazara/Prerequesites.hpp>
#include <array>
namespace Nz
{
class NAZARA_NOISE_API ComplexNoiseBase
{
public:
ComplexNoiseBase();
~ComplexNoiseBase() = default;
float GetHurstParameter() const;
float GetLacunarity() const;
float GetOctaveNumber() const;
void SetHurstParameter(float h);
void SetLacunarity(float lacunarity);
void SetOctavesNumber(float octaves);
void RecomputeExponentArray();
protected:
float m_lacunarity;
float m_hurst;
float m_octaves;
std::array<float, 30> m_exponent_array;
float m_sum;
private:
bool m_parametersModified;
};
}
#endif // COMPLEXNOISEBASE_HPP

View File

@ -1,7 +1,7 @@
/*
Nazara Engine - Noise module
Copyright (C) 2015 Rémi "Overdrivr" Bèges (remi.beges@laposte.net)
Copyright (C) 2016 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

View File

@ -1,4 +1,4 @@
// Copyright (C) 2015 Jérôme Leclercq
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp

View File

@ -1,4 +1,4 @@
// Copyright (C) 2015 Rémi Bèges
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp

View File

@ -1,4 +1,4 @@
// Copyright (C) 2015 Rémi Bèges
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp

View File

@ -0,0 +1,18 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_ENUMS_NOISE_HPP
#define NAZARA_ENUMS_NOISE_HPP
namespace Nz
{
enum WorleyFunction
{
WorleyFunction_F1 = 0,
WorleyFunction_F2 = 1,
WorleyFunction_F3 = 2,
WorleyFunction_F4 = 3
};
}
#endif // NAZARA_ENUMS_NOISE_HPP

View File

@ -0,0 +1,32 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_FBM_HPP
#define NAZARA_FBM_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/Enums.hpp>
#include <Nazara/Noise/MixerBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API FBM : public MixerBase
{
public:
FBM(const NoiseBase& source);
FBM(const FBM&) = delete;
~FBM() = default;
float Get(float x, float y, float scale) const override;
float Get(float x, float y, float z, float scale) const override;
float Get(float x, float y, float z, float w, float scale) const override;
FBM& operator=(const FBM&) = delete;
private:
const NoiseBase& m_source;
};
}
#endif // NAZARA_FBM_HPP

View File

@ -1,31 +0,0 @@
// Copyright (C) 2015 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 FBM2D_HPP
#define FBM2D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API FBM2D : public Abstract2DNoise, public ComplexNoiseBase
{
public:
FBM2D(NoiseType source, unsigned int seed);
float GetValue(float x, float y, float resolution);
~FBM2D();
private:
Abstract2DNoise* m_source;
float m_value;
float m_remainder;
NoiseType m_noiseType;
};
}
#endif // FBM2D_HPP

View File

@ -1,32 +0,0 @@
// Copyright (C) 2015 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 FBM3D_HPP
#define FBM3D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API FBM3D : public Abstract3DNoise, public ComplexNoiseBase
{
public:
FBM3D(NoiseType source, unsigned int seed);
float GetValue(float x, float y, float z, float resolution);
~FBM3D();
private:
Abstract3DNoise* m_source;
float m_value;
float m_remainder;
NoiseType m_noiseType;
};
}
#endif // FBM3D_HPP

View File

@ -1,32 +0,0 @@
// Copyright (C) 2015 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 FBM4D_HPP
#define FBM4D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API FBM4D : public Abstract4DNoise, public ComplexNoiseBase
{
public:
FBM4D(NoiseType source, unsigned int seed);
float GetValue(float x, float y, float z, float w, float resolution);
~FBM4D();
private:
Abstract4DNoise* m_source;
float m_value;
float m_remainder;
NoiseType m_noiseType;
};
}
#endif // FBM4D_HPP

View File

@ -0,0 +1,36 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_HYBRIDMULTIFRACTAL_HPP
#define NAZARA_HYBRIDMULTIFRACTAL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/MixerBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API HybridMultiFractal : public MixerBase
{
public:
HybridMultiFractal(const NoiseBase & source);
HybridMultiFractal(const HybridMultiFractal&) = delete;
~HybridMultiFractal() = default;
float Get(float x, float y, float scale) const override;
float Get(float x, float y, float z, float scale) const override;
float Get(float x, float y, float z, float w, float scale) const override;
HybridMultiFractal& operator=(const HybridMultiFractal&) = delete;
private:
const NoiseBase& m_source;
float m_value;
float m_remainder;
float m_offset;
float m_weight;
float m_signal;
};
}
#endif // NAZARA_HYBRIDMULTIFRACTAL_HPP

View File

@ -1,36 +0,0 @@
// Copyright (C) 2015 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 HYBRIDMULTIFRACTAL2D_HPP
#define HYBRIDMULTIFRACTAL2D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API HybridMultiFractal2D : public Abstract2DNoise, public ComplexNoiseBase
{
public:
HybridMultiFractal2D(NoiseType source, unsigned int seed);
~HybridMultiFractal2D();
float GetValue(float x, float y, float resolution);
private:
Abstract2DNoise* m_source;
float m_value;
float m_remainder;
float m_offset;
float m_weight;
float m_signal;
NoiseType m_noiseType;
};
}
#endif // HYBRIDMULTIFRACTAL2D_HPP

View File

@ -1,36 +0,0 @@
// Copyright (C) 2015 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 HYBRIDMULTIFRACTAL3D_HPP
#define HYBRIDMULTIFRACTAL3D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API HybridMultiFractal3D : public Abstract3DNoise, public ComplexNoiseBase
{
public:
HybridMultiFractal3D(NoiseType source, unsigned int seed);
~HybridMultiFractal3D();
float GetValue(float x, float y, float z, float resolution);
private:
Abstract3DNoise* m_source;
float m_value;
float m_remainder;
float m_offset;
float m_weight;
float m_signal;
NoiseType m_noiseType;
};
}
#endif // HYBRIDMULTIFRACTAL3D_HPP

View File

@ -1,36 +0,0 @@
// Copyright (C) 2015 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 HYBRIDMULTIFRACTAL4D_HPP
#define HYBRIDMULTIFRACTAL4D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
namespace Nz
{
class NAZARA_NOISE_API HybridMultiFractal4D : public Abstract4DNoise, public ComplexNoiseBase
{
public:
HybridMultiFractal4D(NoiseType source, unsigned int seed);
~HybridMultiFractal4D();
float GetValue(float x, float y, float z, float w, float resolution);
private:
Abstract4DNoise* m_source;
float m_value;
float m_remainder;
float m_offset;
float m_weight;
float m_signal;
NoiseType m_noiseType;
};
}
#endif // HYBRIDMULTIFRACTAL4D_HPP

View File

@ -1,35 +0,0 @@
// Copyright (C) 2015 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_MAPPEDNOISEBASE_HPP
#define NAZARA_MAPPEDNOISEBASE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
namespace Nz
{
class NAZARA_NOISE_API MappedNoiseBase : public NoiseBase
{
public:
MappedNoiseBase();
~MappedNoiseBase() = default;
float GetGain() const;
float GetOffset() const;
float GetResolution() const;
void SetGain(float gain);
void SetOffset(float offset);
void SetResolution(float resolution);
protected:
float m_gain;
float m_offset;
float m_resolution;
};
}
#endif // NAZARA_MAPPEDNOISEBASE_HPP

View File

@ -0,0 +1,42 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_MIXERBASE_HPP
#define NAZARA_MIXERBASE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <array>
namespace Nz
{
class NAZARA_NOISE_API MixerBase
{
public:
MixerBase();
~MixerBase() = default;
virtual float Get(float x, float y, float scale) const = 0;
virtual float Get(float x, float y, float z, float scale) const = 0;
virtual float Get(float x, float y, float z, float w, float scale) const = 0;
float GetHurstParameter() const;
float GetLacunarity() const;
float GetOctaveNumber() const;
void SetParameters(float hurst, float lacunarity, float octaves);
protected:
float m_hurst;
float m_lacunarity;
float m_octaves;
std::vector<float> m_exponent_array;
float m_sum;
private:
void Recompute();
};
}
#endif // NAZARA_MIXERBASE_HPP

View File

@ -1,4 +1,4 @@
// Copyright (C) 2015 Rémi Bèges
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp

View File

@ -1,48 +1,47 @@
// Copyright (C) 2015 Rémi Bèges
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NOISEBASE_HPP
#define NOISEBASE_HPP
#ifndef NAZARA_NOISEBASE_HPP
#define NAZARA_NOISEBASE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Noise/Config.hpp>
#include <array>
#include <random>
namespace Nz
{
enum NoiseType
{
PERLIN,
SIMPLEX,
CELL
};
class NAZARA_NOISE_API NoiseBase
{
public:
NoiseBase(unsigned int seed = 0);
~NoiseBase() = default;
void SetNewSeed(unsigned int seed);
virtual float Get(float x, float y, float scale) const = 0;
virtual float Get(float x, float y, float z, float scale) const = 0;
virtual float Get(float x, float y, float z, float w, float scale) const = 0;
float GetScale();
void ShufflePermutationTable();
void SetScale(float scale);
void SetSeed(unsigned int seed);
unsigned int GetUniformRandomValue();
int fastfloor(float n);
int JenkinsHash(int a, int b, int c);
void Shuffle();
protected:
unsigned int perm[512];
std::array<std::size_t, 3 * 256> m_permutations;
float m_scale;
static std::array<Vector2f, 2 * 2 * 2> s_gradients2;
static std::array<Vector3f, 2 * 2 * 2 * 2> s_gradients3;
static std::array<Vector4f, 2 * 2 * 2 * 2 * 2> s_gradients4;
private:
unsigned int Ua, Uc, Um;
unsigned int UcurrentSeed;
unsigned int Uprevious, Ulast;
std::default_random_engine m_randomEngine;
};
}
#endif // NOISEBASE_HPP
#endif // NAZARA_NOISEBASE_HPP

View File

@ -0,0 +1,14 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_NOISETOOLS_HPP
#define NAZARA_NOISETOOLS_HPP
namespace Nz
{
int fastfloor(float n);
int JenkinsHash(int a, int b, int c);
}
#endif // NAZARA_NOISETOOLS_HPP

View File

@ -0,0 +1,28 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_PERLIN_HPP
#define NAZARA_PERLIN_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <initializer_list>
namespace Nz
{
class NAZARA_NOISE_API Perlin : public NoiseBase
{
public:
Perlin() = default;
Perlin(unsigned int seed);
~Perlin() = default;
float Get(float x, float y, float scale) const override;
float Get(float x, float y, float z, float scale) const override;
float Get(float x, float y, float z, float w, float scale) const override;
};
}
#endif // NAZARA_PERLIN_HPP

View File

@ -1,39 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef PERLIN2D_HPP
#define PERLIN2D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
#include <Nazara/Math/Vector2.hpp>
namespace Nz
{
class NAZARA_NOISE_API Perlin2D : public Abstract2DNoise
{
public:
Perlin2D();
Perlin2D(unsigned int seed);
~Perlin2D() = default;
float GetValue(float x, float y, float resolution);
private:
int x0, y0;
int gi0,gi1,gi2,gi3;
int ii, jj;
float gradient2[8][2];
float s,t,u,v;
float Cx,Cy;
float Li1, Li2;
Vector2<float> temp;
};
}
#endif // PERLIN2D_HPP

View File

@ -1,40 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef PERLIN3D_HPP
#define PERLIN3D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
#include <Nazara/Math/Vector3.hpp>
namespace Nz
{
class NAZARA_NOISE_API Perlin3D : public Abstract3DNoise
{
public:
Perlin3D();
Perlin3D(unsigned int seed);
~Perlin3D() = default;
float GetValue(float x, float y, float z, float resolution);
private:
int x0,y0,z0;
int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7;
int ii,jj,kk;
float gradient3[16][3];
float Li1,Li2,Li3,Li4,Li5,Li6;
float s[2],t[2],u[2],v[2];
float Cx,Cy,Cz;
float nx,ny,nz;
float tmp;
Vector3<float> temp;
};
}
#endif // PERLIN3D_HPP

View File

@ -1,39 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef PERLIN4D_HPP
#define PERLIN4D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
#include <Nazara/Math/Vector4.hpp>
namespace Nz
{
class NAZARA_NOISE_API Perlin4D : public Abstract4DNoise
{
public:
Perlin4D();
Perlin4D(unsigned int seed);
~Perlin4D() = default;
float GetValue(float x, float y, float z, float w, float resolution);
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;
float gradient4[32][4];
float Li1,Li2,Li3,Li4,Li5,Li6,Li7,Li8,Li9,Li10,Li11,Li12,Li13,Li14;
float s[4],t[4],u[4],v[4];
float Cx,Cy,Cz,Cw;
float tmp;
Vector4<float> temp;
};
}
#endif // PERLIN4D_HPP

View File

@ -0,0 +1,28 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef SIMPLEX_HPP
#define SIMPLE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <initializer_list>
namespace Nz
{
class NAZARA_NOISE_API Simplex : public NoiseBase
{
public:
Simplex() = default;
Simplex(unsigned int seed);
~Simplex() = default;
float Get(float x, float y, float scale) const override;
float Get(float x, float y, float z, float scale) const override;
float Get(float x, float y, float z, float w, float scale) const override;
};
}
#endif // SIMPLEX_HPP

View File

@ -1,42 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef SIMPLEX2D_HPP
#define SIMPLEX2D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
#include <Nazara/Math/Vector2.hpp>
namespace Nz
{
class NAZARA_NOISE_API Simplex2D : public Abstract2DNoise
{
public:
Simplex2D();
Simplex2D(unsigned int seed);
virtual ~Simplex2D() = default;
float GetValue(float x, float y, float resolution);
private:
int ii,jj;
int gi0,gi1,gi2;
Vector2i skewedCubeOrigin,off1;
float n1,n2,n3;
float c1,c2,c3;
float gradient2[8][2];
float UnskewCoeff2D;
float SkewCoeff2D;
float sum;
Vector2<float> unskewedCubeOrigin, unskewedDistToOrigin;
Vector2<float> d1,d2,d3;
};
}
#endif // SIMPLEX2D_HPP

View File

@ -1,42 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef SIMPLEX3D_HPP
#define SIMPLEX3D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
#include <Nazara/Math/Vector3.hpp>
namespace Nz
{
class NAZARA_NOISE_API Simplex3D : public Abstract3DNoise
{
public:
Simplex3D();
Simplex3D(unsigned int seed);
~Simplex3D() = default;
float GetValue(float x, float y, float z, float resolution);
private:
int ii,jj,kk;
int gi0,gi1,gi2,gi3;
Vector3i skewedCubeOrigin,off1,off2;
float n1,n2,n3,n4;
float c1,c2,c3,c4;
float gradient3[12][3];
float UnskewCoeff3D;
float SkewCoeff3D;
float sum;
Vector3<float> unskewedCubeOrigin, unskewedDistToOrigin;
Vector3<float> d1,d2,d3,d4;
};
}
#endif // SIMPLEX3D_HPP

View File

@ -1,44 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef SIMPLEX4D_HPP
#define SIMPLEX4D_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
#include <Nazara/Math/Vector4.hpp>
namespace Nz
{
class NAZARA_NOISE_API Simplex4D : public Abstract4DNoise
{
public:
Simplex4D();
Simplex4D(unsigned int seed);
~Simplex4D() = default;
float GetValue(float x, float y, float z, float w, float resolution);
private:
int ii,jj,kk,ll;
int gi0,gi1,gi2,gi3,gi4;
Vector4i skewedCubeOrigin,off1,off2,off3;
int lookupTable4D[64][4];
int c;
float n1,n2,n3,n4,n5;
float c1,c2,c3,c4,c5,c6;
float gradient4[32][4];
float UnskewCoeff4D;
float SkewCoeff4D;
float sum;
Vector4<float> unskewedCubeOrigin, unskewedDistToOrigin;
Vector4<float> d1,d2,d3,d4,d5;
};
}
#endif // SIMPLEX4D_H

View File

@ -0,0 +1,38 @@
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifndef NAZARA_WORLEY_HPP
#define NAZARA_WORLEY_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Enums.hpp>
#include <Nazara/Noise/NoiseBase.hpp>
#include <initializer_list>
#include <map>
namespace Nz
{
class NAZARA_NOISE_API Worley : public NoiseBase
{
public:
Worley();
Worley(unsigned int seed);
~Worley() = default;
float Get(float x, float y, float scale) const override;
float Get(float x, float y, float z, float scale) const override;
float Get(float x, float y, float z, float w, float scale) const override;
void Set(WorleyFunction func);
private:
void SquareTest(int xi, int yi, float x, float y, std::map<float, Vector2f> & featurePoints) const;
WorleyFunction m_function;
};
}
#endif // NAZARA_WORLEY_HPP

View File

@ -1,24 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/StringStream.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Abstract2DNoise.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Abstract2DNoise::~Abstract2DNoise() = default;
float Abstract2DNoise::GetBasicValue(float x, float y)
{
return this->GetValue(x,y,m_resolution);
}
float Abstract2DNoise::GetMappedValue(float x, float y)
{
return (this->GetValue(x,y,m_resolution) + m_offset) * m_gain;
}
}

View File

@ -1,24 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/StringStream.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Abstract3DNoise.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Abstract3DNoise::~Abstract3DNoise() = default;
float Abstract3DNoise::GetBasicValue(float x, float y, float z)
{
return this->GetValue(x,y,z,m_resolution);
}
float Abstract3DNoise::GetMappedValue(float x, float y, float z)
{
return (this->GetValue(x,y,z,m_resolution) + m_offset) * m_gain ;
}
}

View File

@ -1,24 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/StringStream.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Abstract4DNoise.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Abstract4DNoise::~Abstract4DNoise() = default;
float Abstract4DNoise::GetBasicValue(float x, float y, float z, float w)
{
return this->GetValue(x,y,z,w,m_resolution);
}
float Abstract4DNoise::GetMappedValue(float x, float y, float z, float w)
{
return (this->GetValue(x,y,z,w,m_resolution) + m_offset) * m_gain ;
}
}

View File

@ -1,83 +0,0 @@
// Copyright (C) 2015 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 <cmath>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/ComplexNoiseBase.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
ComplexNoiseBase::ComplexNoiseBase()
{
m_parametersModified = true;
m_lacunarity = 5.0f;
m_hurst = 1.2f;
m_octaves = 3.0f;
for (int i(0) ; i < m_octaves; ++i)
{
m_exponent_array[i] = 0;
}
}
float ComplexNoiseBase::GetLacunarity() const
{
return m_lacunarity;
}
float ComplexNoiseBase::GetHurstParameter() const
{
return m_hurst;
}
float ComplexNoiseBase::GetOctaveNumber() const
{
return m_octaves;
}
void ComplexNoiseBase::SetLacunarity(float lacunarity)
{
m_lacunarity = lacunarity;
m_parametersModified = true;
}
void ComplexNoiseBase::SetHurstParameter(float h)
{
m_hurst = h;
m_parametersModified = true;
}
void ComplexNoiseBase::SetOctavesNumber(float octaves)
{
if(octaves <= 30.0f)
m_octaves = octaves;
else
m_octaves = 30.0f;
m_parametersModified = true;
}
void ComplexNoiseBase::RecomputeExponentArray()
{
if(m_parametersModified)
{
float frequency = 1.0;
m_sum = 0.f;
for (int i(0) ; i < static_cast<int>(m_octaves) ; ++i)
{
m_exponent_array[i] = std::pow( frequency, -m_hurst );
frequency *= m_lacunarity;
m_sum += m_exponent_array[i];
}
m_parametersModified = false;
}
}
}

62
src/Nazara/Noise/FBM.cpp Normal file
View File

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

View File

@ -1,54 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/FBM2D.hpp>
#include <Nazara/Noise/Perlin2D.hpp>
#include <Nazara/Noise/Simplex2D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
FBM2D::FBM2D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin2D();
break;
default:
m_source = new Simplex2D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float FBM2D::GetValue(float x, float y, float resolution)
{
this->RecomputeExponentArray();
m_value = 0.0;
for (int i(0); i < m_octaves; ++i)
{
m_value += m_source->GetValue(x,y,resolution) * m_exponent_array[i];
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(!NumberEquals(m_remainder, static_cast<float>(0.0)))
m_value += m_remainder * m_source->GetValue(x,y,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
FBM2D::~FBM2D()
{
delete m_source;
}
}

View File

@ -1,54 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/FBM3D.hpp>
#include <Nazara/Noise/Perlin3D.hpp>
#include <Nazara/Noise/Simplex3D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
FBM3D::FBM3D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin3D();
break;
default:
m_source = new Simplex3D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float FBM3D::GetValue(float x, float y, float z, float resolution)
{
this->RecomputeExponentArray();
m_value = 0.0;
for (int i(0); i < m_octaves; ++i)
{
m_value += m_source->GetValue(x,y,z,resolution) * m_exponent_array[i];
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(!NumberEquals(m_remainder, static_cast<float>(0.0)))
m_value += m_remainder * m_source->GetValue(x,y,z,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
FBM3D::~FBM3D()
{
delete m_source;
}
}

View File

@ -1,54 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/FBM4D.hpp>
#include <Nazara/Noise/Perlin4D.hpp>
#include <Nazara/Noise/Simplex4D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
FBM4D::FBM4D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin4D();
break;
default:
m_source = new Simplex4D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float FBM4D::GetValue(float x, float y, float z, float w, float resolution)
{
this->RecomputeExponentArray();
m_value = 0.0;
for (int i(0); i < m_octaves; ++i)
{
m_value += m_source->GetValue(x,y,z,w,resolution) * m_exponent_array[i];
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if(!NumberEquals(m_remainder, static_cast<float>(0.0)))
m_value += m_remainder * m_source->GetValue(x,y,z,w,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum;
}
FBM4D::~FBM4D()
{
delete m_source;
}
}

View File

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

View File

@ -1,67 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/HybridMultiFractal3D.hpp>
#include <Nazara/Noise/Perlin3D.hpp>
#include <Nazara/Noise/Simplex3D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
HybridMultiFractal3D::HybridMultiFractal3D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin3D();
break;
default:
m_source = new Simplex3D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float HybridMultiFractal3D::GetValue(float x, float y, float z, float resolution)
{
this->RecomputeExponentArray();
m_offset = 1.0f;
m_value = (m_source->GetValue(x,y,z,resolution) + m_offset) * m_exponent_array[0];
m_weight = m_value;
m_signal = 0.f;
resolution *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (m_weight > 1.f)
m_weight = 1.f;
m_signal = (m_source->GetValue(x,y,z,resolution) + m_offset) * m_exponent_array[i];
m_value += m_weight * m_signal;
m_weight *= m_signal;
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if (m_remainder > 0.f)
m_value += m_remainder * m_source->GetValue(x,y,z,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum - m_offset;
}
HybridMultiFractal3D::~HybridMultiFractal3D()
{
delete m_source;
}
}

View File

@ -1,67 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/HybridMultiFractal4D.hpp>
#include <Nazara/Noise/Perlin4D.hpp>
#include <Nazara/Noise/Simplex4D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
HybridMultiFractal4D::HybridMultiFractal4D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin4D();
break;
default:
m_source = new Simplex4D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float HybridMultiFractal4D::GetValue(float x, float y, float z, float w, float resolution)
{
this->RecomputeExponentArray();
m_offset = 1.0f;
m_value = (m_source->GetValue(x,y,z,w,resolution) + m_offset) * m_exponent_array[0];
m_weight = m_value;
m_signal = 0.f;
resolution *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (m_weight > 1.f)
m_weight = 1.f;
m_signal = (m_source->GetValue(x,y,z,w,resolution) + m_offset) * m_exponent_array[i];
m_value += m_weight * m_signal;
m_weight *= m_signal;
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if (m_remainder > 0.f)
m_value += m_remainder * m_source->GetValue(x,y,z,w,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum - m_offset;
}
HybridMultiFractal4D::~HybridMultiFractal4D()
{
delete m_source;
}
}

View File

@ -1,67 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/HybridMultiFractal2D.hpp>
#include <Nazara/Noise/Perlin2D.hpp>
#include <Nazara/Noise/Simplex2D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
HybridMultiFractal2D::HybridMultiFractal2D(NoiseType source, unsigned int seed)
{
switch(source)
{
case PERLIN:
m_source = new Perlin2D();
break;
default:
m_source = new Simplex2D();
break;
}
m_source->SetNewSeed(seed);
m_source->ShufflePermutationTable();
m_noiseType = source;
}
float HybridMultiFractal2D::GetValue(float x, float y, float resolution)
{
this->RecomputeExponentArray();
m_offset = 1.0f;
m_value = (m_source->GetValue(x,y,resolution) + m_offset) * m_exponent_array[0];
m_weight = m_value;
m_signal = 0.f;
resolution *= m_lacunarity;
for(int i(1) ; i < m_octaves; ++i)
{
if (m_weight > 1.f)
m_weight = 1.f;
m_signal = (m_source->GetValue(x,y,resolution) + m_offset) * m_exponent_array[i];
m_value += m_weight * m_signal;
m_weight *= m_signal;
resolution *= m_lacunarity;
}
m_remainder = m_octaves - static_cast<int>(m_octaves);
if (m_remainder > 0.f)
m_value += m_remainder * m_source->GetValue(x,y,resolution) * m_exponent_array[static_cast<int>(m_octaves-1)];
return m_value/this->m_sum - m_offset;
}
HybridMultiFractal2D::~HybridMultiFractal2D()
{
delete m_source;
}
}

View File

@ -1,56 +0,0 @@
// Copyright (C) 2015 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 <Nazara/Core/StringStream.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Math/Algorithm.hpp>
#include <Nazara/Noise/Config.hpp>
#include <stdexcept>
#include <Nazara/Noise/MappedNoiseBase.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
MappedNoiseBase::MappedNoiseBase() : m_gain(1.f), m_offset(0.f), m_resolution(30.f)
{
}
float MappedNoiseBase::GetGain() const
{
return m_gain;
}
float MappedNoiseBase::GetOffset() const
{
return m_offset;
}
float MappedNoiseBase::GetResolution() const
{
return m_resolution;
}
void MappedNoiseBase::SetGain(float gain)
{
m_gain = gain;
}
void MappedNoiseBase::SetOffset(float offset)
{
m_offset = offset;
}
void MappedNoiseBase::SetResolution(float resolution)
{
if (NumberEquals(resolution, 0.f))
{
StringStream ss;
ss << __FILE__ << ':' << __LINE__ << " : resolution cannot be 0.0f";
throw std::domain_error(ss.ToString());
}
m_resolution = resolution;
}
}

View File

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

View File

@ -1,79 +1,73 @@
// Copyright (C) 2015 Rémi Bèges
// Copyright (C) 2016 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Noise/NoiseBase.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
NoiseBase::NoiseBase(unsigned int seed)
NoiseBase::NoiseBase(unsigned int seed) :
m_scale(0.05f)
{
Ua = 16807;
Uc = 0;
Um = 2147483647;
UcurrentSeed = 0;
Uprevious = 0;
SetNewSeed(seed);
for(int i(0) ; i < 512 ; i++)
perm[i] = i & 255;
SetSeed(seed);
// Fill permutations with initial values
std::iota(m_permutations.begin(), m_permutations.begin() + 256, 0);
}
void NoiseBase::SetNewSeed(unsigned int seed)
float NoiseBase::GetScale()
{
Uprevious = seed;
UcurrentSeed = seed;
return m_scale;
}
unsigned int NoiseBase::GetUniformRandomValue()
void NoiseBase::SetScale(float scale)
{
Ulast = Ua*Uprevious + Uc%Um;
Uprevious = Ulast;
return Ulast;
m_scale = scale;
}
void NoiseBase::ShufflePermutationTable()
void NoiseBase::SetSeed(unsigned int seed)
{
int xchanger;
unsigned int ncase;
for(unsigned int i(0) ; i < 256 ; i++)
perm[i] = i;
for(unsigned int j(0) ; j < 20 ; ++j)
for (unsigned int i(0); i < 256 ; ++i)
{
ncase = this->GetUniformRandomValue() & 255;
xchanger = perm[i];
perm[i] = perm[ncase];
perm[ncase] = xchanger;
}
for(unsigned int i(256) ; i < 512; ++i)
perm[i] = perm[i & 255];
m_randomEngine.seed(seed);
}
int NoiseBase::fastfloor(float n)
void NoiseBase::Shuffle()
{
return (n >= 0) ? static_cast<int>(n) : static_cast<int>(n-1);
std::shuffle(m_permutations.begin(), m_permutations.begin() + 256, m_randomEngine);
for(std::size_t i = 1; i < (m_permutations.size() / 256); ++i)
std::copy(m_permutations.begin(), m_permutations.begin() + 256, m_permutations.begin() + 256 * i);
}
int NoiseBase::JenkinsHash(int a, int b, int c)
std::array<Vector2f, 2 * 2 * 2> NoiseBase::s_gradients2 =
{
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 13);
b = b-c; b = b - a; b = b^(a << 8);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 13);
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 12);
b = b-c; b = b - a; b = b^(a << 16);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 5);
a = a-b; a = a - c; a = a^(static_cast<unsigned int>(c) >> 3);
b = b-c; b = b - a; b = b^(a << 10);
c = c-a; c = c - b; c = c^(static_cast<unsigned int>(b) >> 15);
return c;
}
{
{1.f, 1.f}, {-1.f, 1.f}, {1.f, -1.f}, {-1.f, -1.f},
{1.f, 0.f}, {-1.f, 0.f}, {0.f, 1.f}, { 0.f, -1.f}
}
};
std::array<Vector3f, 2 * 2 * 2 * 2> NoiseBase::s_gradients3 =
{
{
{1.f,1.f,0.f}, {-1.f, 1.f, 0.f}, {1.f, -1.f, 0.f}, {-1.f, -1.f, 0.f},
{1.f,0.f,1.f}, {-1.f, 0.f, 1.f}, {1.f, 0.f, -1.f}, {-1.f, 0.f, -1.f},
{0.f,1.f,1.f}, { 0.f, -1.f, 1.f}, {0.f, 1.f, -1.f}, {0.f, -1.f, -1.f},
{1.f,1.f,0.f}, {-1.f, 1.f, 0.f}, {0.f, -1.f, 1.f}, {0.f, -1.f, -1.f}
}
};
std::array<Vector4f, 2 * 2 * 2 * 2 * 2> NoiseBase::s_gradients4 =
{
{
{0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1},
{0,-1,1,1},{0,-1,1,-1},{0,-1,-1,1},{0,-1,-1,-1},
{1,0,1,1}, {1,0,1,-1}, {1,0,-1,1}, {1,0,-1,-1},
{-1,0,1,1},{-1,0,1,-1},{-1,0,-1,1},{-1,0,-1,-1},
{1,1,0,1}, {1,1,0,-1}, {1,-1,0,1}, {1,-1,0,-1},
{-1,1,0,1},{-1,1,0,-1},{-1,-1,0,1},{-1,-1,0,-1},
{1,1,1,0}, {1,1,-1,0}, {1,-1,1,0}, {1,-1,-1,0},
{-1,1,1,0},{-1,1,-1,0},{-1,-1,1,0},{-1,-1,-1,0}
}
};
}

View File

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

280
src/Nazara/Noise/Perlin.cpp Normal file
View File

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

View File

@ -1,75 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Perlin2D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Perlin2D::Perlin2D()
{
float grad2Temp[][2] = {
{1.f,1.f},
{-1.f,1.f},
{1.f,-1.f},
{-1.f,-1.f},
{1.f,0.f},
{-1.f,0.f},
{0.f,1.f},
{0.f,-1.f}
};
for(int i(0) ; i < 8 ; ++i)
for(int j(0) ; j < 2 ; ++j)
gradient2[i][j] = grad2Temp[i][j];
}
Perlin2D::Perlin2D(unsigned int seed) : Perlin2D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Perlin2D::GetValue(float x, float y, float resolution)
{
x *= resolution;
y *= resolution;
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);
}
}

View File

@ -1,98 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Perlin3D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Perlin3D::Perlin3D()
{
float 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];
}
Perlin3D::Perlin3D(unsigned int seed) : Perlin3D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Perlin3D::GetValue(float x, float y, float z, float resolution)
{
x /= resolution;
y /= resolution;
z /= resolution;
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);
}
}

View File

@ -1,156 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Perlin4D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Perlin4D::Perlin4D()
{
float 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];
}
Perlin4D::Perlin4D(unsigned int seed) : Perlin4D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Perlin4D::GetValue(float x, float y, float z, float w, float resolution)
{
x *= resolution;
y *= resolution;
z *= resolution;
w *= resolution;
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);
}
}

View File

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

View File

@ -1,94 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Simplex2D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Simplex2D::Simplex2D()
{
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];
SkewCoeff2D = 0.5f*(std::sqrt(3.f) - 1.f);
UnskewCoeff2D = (3.f-std::sqrt(3.f))/6.f;
}
Simplex2D::Simplex2D(unsigned int seed) : Simplex2D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Simplex2D::GetValue(float x, float y, float resolution)
{
x *= resolution;
y *= resolution;
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.f - 2.f * UnskewCoeff2D;
d3.y = d1.y + 1.f - 2.f * 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.5f - d1.x * d1.x - d1.y * d1.y;
c2 = 0.5f - d2.x * d2.x - d2.y * d2.y;
c3 = 0.5f - 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.f;
}
}

View File

@ -1,176 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Simplex3D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Simplex3D::Simplex3D()
{
SkewCoeff3D = 1/3.f;
UnskewCoeff3D = 1/6.f;
float grad3Temp[][3] = {
{1.f,1.f,0.f},
{-1.f,1.f,0.f},
{1.f,-1.f,0.f},
{-1.f,-1.f,0.f},
{1.f,0.f,1.f},
{-1.f,0.f,1.f},
{1.f,0.f,-1.f},
{-1.f,0.f,-1.f},
{0.f,1.f,1.f},
{0.f,-1.f,1.f},
{0.f,1.f,-1.f},
{0.f,-1.f,-1.f}
};
for(int i(0) ; i < 12 ; ++i)
for(int j(0) ; j < 3 ; ++j)
gradient3[i][j] = grad3Temp[i][j];
}
Simplex3D::Simplex3D(unsigned int seed) : Simplex3D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Simplex3D::GetValue(float x, float y, float z, float resolution)
{
x *= resolution;
y *= resolution;
z *= resolution;
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.f*UnskewCoeff3D;
d3.y = d1.y - off2.y + 2.f*UnskewCoeff3D;
d3.z = d1.z - off2.z + 2.f*UnskewCoeff3D;
d4.x = d1.x - 1.f + 3.f*UnskewCoeff3D;
d4.y = d1.y - 1.f + 3.f*UnskewCoeff3D;
d4.z = d1.z - 1.f + 3.f*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.6f - d1.x * d1.x - d1.y * d1.y - d1.z * d1.z;
c2 = 0.6f - d2.x * d2.x - d2.y * d2.y - d2.z * d2.z;
c3 = 0.6f - d3.x * d3.x - d3.y * d3.y - d3.z * d3.z;
c4 = 0.6f - 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;
}
}

View File

@ -1,169 +0,0 @@
// Copyright (C) 2015 Rémi Bèges
// This file is part of the "Nazara Engine - Noise module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Noise/Config.hpp>
#include <Nazara/Noise/Simplex4D.hpp>
#include <Nazara/Noise/Debug.hpp>
namespace Nz
{
Simplex4D::Simplex4D()
{
SkewCoeff4D = (std::sqrt(5.f) - 1.f)/4.f;
UnskewCoeff4D = (5.f - std::sqrt(5.f))/20.f;
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 grad4Temp[][4] =
{
{0.f,1.f,1.f,1.f}, {0.f,1.f,1.f,-1.f}, {0.f,1.f,-1.f,1.f}, {0.f,1.f,-1.f,-1.f},
{0.f,-1.f,1.f,1.f},{0.f,-1.f,1.f,-1.f},{0.f,-1.f,-1.f,1.f},{0.f,-1.f,-1.f,-1.f},
{1.f,0.f,1.f,1.f}, {1.f,0.f,1.f,-1.f}, {1.f,0.f,-1.f,1.f}, {1.f,0.f,-1.f,-1.f},
{-1.f,0.f,1.f,1.f},{-1.f,0.f,1.f,-1.f},{-1.f,0.f,-1.f,1.f},{-1.f,0.f,-1.f,-1.f},
{1.f,1.f,0.f,1.f}, {1.f,1.f,0.f,-1.f}, {1.f,-1.f,0.f,1.f}, {1.f,-1.f,0.f,-1.f},
{-1.f,1.f,0.f,1.f},{-1.f,1.f,0.f,-1.f},{-1.f,-1.f,0.f,1.f},{-1.f,-1.f,0.f,-1.f},
{1.f,1.f,1.f,0.f}, {1.f,1.f,-1.f,0.f}, {1.f,-1.f,1.f,0.f}, {1.f,-1.f,-1.f,0.f},
{-1.f,1.f,1.f,0.f},{-1.f,1.f,-1.f,0.f},{-1.f,-1.f,1.f,0.f},{-1.f,-1.f,-1.f,0.f}
};
for(int i(0) ; i < 32 ; ++i)
for(int j(0) ; j < 4 ; ++j)
gradient4[i][j] = grad4Temp[i][j];
}
Simplex4D::Simplex4D(unsigned int seed) : Simplex4D()
{
this->SetNewSeed(seed);
this->ShufflePermutationTable();
}
float Simplex4D::GetValue(float x, float y, float z, float w, float resolution)
{
x *= resolution;
y *= resolution;
z *= resolution;
w *= resolution;
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.f*UnskewCoeff4D;
d3.y = d1.y - off2.y + 2.f*UnskewCoeff4D;
d3.z = d1.z - off2.z + 2.f*UnskewCoeff4D;
d3.w = d1.w - off2.w + 2.f*UnskewCoeff4D;
d4.x = d1.x - off3.x + 3.f*UnskewCoeff4D;
d4.y = d1.y - off3.y + 3.f*UnskewCoeff4D;
d4.z = d1.z - off3.z + 3.f*UnskewCoeff4D;
d4.w = d1.w - off3.w + 3.f*UnskewCoeff4D;
d5.x = d1.x - 1.f + 4*UnskewCoeff4D;
d5.y = d1.y - 1.f + 4*UnskewCoeff4D;
d5.z = d1.z - 1.f + 4*UnskewCoeff4D;
d5.w = d1.w - 1.f + 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.6f - d1.x*d1.x - d1.y*d1.y - d1.z*d1.z - d1.w*d1.w;
c2 = 0.6f - d2.x*d2.x - d2.y*d2.y - d2.z*d2.z - d2.w*d2.w;
c3 = 0.6f - d3.x*d3.x - d3.y*d3.y - d3.z*d3.z - d3.w*d3.w;
c4 = 0.6f - d4.x*d4.x - d4.y*d4.y - d4.z*d4.z - d4.w*d4.w;
c5 = 0.6f - 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.f;
}
}

153
src/Nazara/Noise/Worley.cpp Normal file
View File

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