diff --git a/include/Nazara/Noise/Noise.hpp b/include/Nazara/Noise/Noise.hpp index 9a702c44f..c4925ac0a 100644 --- a/include/Nazara/Noise/Noise.hpp +++ b/include/Nazara/Noise/Noise.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2012 Rémi "Overdrivr" Bèges (remi{dot}beges{at}gmail{dot}com) +// Copyright (C) 2012 AUTHORS // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp diff --git a/include/Nazara/Noise/NoiseBase.hpp b/include/Nazara/Noise/NoiseBase.hpp index af8a66632..0bd37ab47 100644 --- a/include/Nazara/Noise/NoiseBase.hpp +++ b/include/Nazara/Noise/NoiseBase.hpp @@ -18,6 +18,8 @@ class NzNoiseBase void SetNewSeed(int seed); int GetUniformRandomValue(); void ShufflePermutationTable(); + int fastfloor(float n); + int JenkinsHash(int a, int b, int c); protected: int perm[512]; private: diff --git a/include/Nazara/Noise/NoiseMachine.hpp b/include/Nazara/Noise/NoiseMachine.hpp index fa3a60ea1..751c186c7 100644 --- a/include/Nazara/Noise/NoiseMachine.hpp +++ b/include/Nazara/Noise/NoiseMachine.hpp @@ -16,7 +16,8 @@ //TODO : tableau de gradients en float au lieu de int ? Ou condition ternaires ? // utiliser fastfloor partout -// vérifier bon fonctionnement perlin1d +// utiliser copies paramètres pour économiser mémoire +// améliorer le mélange de la table de perm class NzNoiseMachine : public NzNoiseBase { @@ -24,7 +25,6 @@ class NzNoiseMachine : public NzNoiseBase NzNoiseMachine(int seed = 0); ~NzNoiseMachine(); - float Get1DPerlinNoiseValue (float x, float res); float Get2DPerlinNoiseValue (float x, float y, float res); float Get3DPerlinNoiseValue (float x, float y, float z, float res); float Get4DPerlinNoiseValue (float x, float y, float z, float w, float res); @@ -42,7 +42,6 @@ class NzNoiseMachine : public NzNoiseBase void SetOctavesNumber(float octaves); void RecomputeExponentArray(); - float Get1DFBMNoiseValue(float x, float res); float Get2DFBMNoiseValue(float x, float y, float res); float Get3DFBMNoiseValue(float x, float y, float z, float res); @@ -52,10 +51,6 @@ class NzNoiseMachine : public NzNoiseBase protected: private: - //Pour tronquer les nombres - int fastfloor(float n); - - int gradient1[2]; float gradient2[8][2]; int gradient3[16][3]; int gradient4[32][4]; @@ -65,15 +60,13 @@ class NzNoiseMachine : public NzNoiseBase float n1, n2, n3, n4, n5; NzVector4f A; - NzVector4i Origin; NzVector4f d1,d2,d3,d4,d5; - NzVector4i off1, off2,off3; NzVector4f IsoOriginDist; - NzVector4f H[5]; + NzVector4i Origin; + NzVector4i off1, off2,off3; int ii,jj,kk,ll; int gi0,gi1,gi2,gi3,gi4,gi5,gi6,gi7,gi8,gi9,gi10,gi11,gi12,gi13,gi14,gi15; - float lenght; float c1,c2,c3,c4,c5,c6; int c; diff --git a/include/Nazara/Noise/Perlin1D.hpp b/include/Nazara/Noise/Perlin1D.hpp deleted file mode 100644 index 533ccc0a7..000000000 --- a/include/Nazara/Noise/Perlin1D.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (C) 2012 Rémi Bèges -// This file is part of the "Nazara Engine". -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef PERLIN1D_H -#define PERLIN1D_H - -#include -//#include -#include "NoiseBase.hpp" - -template class NzPerlin1D : public NzNoiseBase -{ - public: - NzPerlin1D(); - T GetValue(T x, T res); - ~NzPerlin1D() = default; - protected: - private: - int x0; - int gi0,gi1; - int ii; - int gradient1[16]; - T s,t; - T Cx; - T nx; - T tmp; -}; - -typedef NzPerlin1D NzPerlin1Df; -typedef NzPerlin1D NzPerlin1Dd; - -//#include -#include "Perlin1D.inl" - -#endif // PERLIN1D_H diff --git a/include/Nazara/Noise/Perlin1D.inl b/include/Nazara/Noise/Perlin1D.inl deleted file mode 100644 index 75b5c524e..000000000 --- a/include/Nazara/Noise/Perlin1D.inl +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (C) 2012 Rémi Bèges -// This file is part of the "Nazara Engine". -// For conditions of distribution and use, see copyright notice in Config.hpp - -//#include -//#include -//#include -#include - -template -NzPerlin1D::NzPerlin1D() -{ - gradient1[0] = 1; - gradient1[1] = -1; -} - -template -T NzPerlin1D::GetValue(T x, T res) -{ - nx = x/res; - x0 = static_cast(nx); - ii = x0 & 255; - - gi0 = perm[ii] % 2; - gi1 = perm[ii + 1] % 2; - - tmp = nx-x0; - s = gradient1[gi0]*tmp; - - tmp = nx-(x0+1); - t = gradient1[gi1]*tmp; - - tmp = nx-x0; - Cx = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); - - return s + Cx*(t-s); -} diff --git a/include/Nazara/Noise/Perlin2D.hpp b/include/Nazara/Noise/Perlin2D.hpp index 1633c10fb..6b693fa23 100644 --- a/include/Nazara/Noise/Perlin2D.hpp +++ b/include/Nazara/Noise/Perlin2D.hpp @@ -8,7 +8,8 @@ #define PERLIN2D_H #include -#include +//#include +#include "NoiseBase.hpp" #include template class NzPerlin2D : public NzNoiseBase @@ -34,7 +35,8 @@ template class NzPerlin2D : public NzNoiseBase typedef NzPerlin2D NzPerlin2Df; typedef NzPerlin2D NzPerlin2Dd; -#include +//#include +#include "Perlin2D.inl" #endif // PERLIN2D_H diff --git a/include/Nazara/Noise/Perlin2D.inl b/include/Nazara/Noise/Perlin2D.inl index df06d2679..2d84517fb 100644 --- a/include/Nazara/Noise/Perlin2D.inl +++ b/include/Nazara/Noise/Perlin2D.inl @@ -2,9 +2,9 @@ // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include -#include +//#include +//#include +//#include template NzPerlin2D::NzPerlin2D() diff --git a/include/Nazara/Noise/Perlin3D.hpp b/include/Nazara/Noise/Perlin3D.hpp index ca01b891d..74eb53557 100644 --- a/include/Nazara/Noise/Perlin3D.hpp +++ b/include/Nazara/Noise/Perlin3D.hpp @@ -8,7 +8,8 @@ #define PERLIN3D_H #include -#include +//#include +#include "NoiseBase.hpp" #include template class NzPerlin3D : public NzNoiseBase @@ -35,6 +36,7 @@ template class NzPerlin3D : public NzNoiseBase typedef NzPerlin3D NzPerlin3Df; typedef NzPerlin3D NzPerlin3Dd; -#include +//#include +#include "Perlin3D.inl" #endif // PERLIN3D_H diff --git a/include/Nazara/Noise/Perlin3D.inl b/include/Nazara/Noise/Perlin3D.inl index 8bf832dfe..99f3334e9 100644 --- a/include/Nazara/Noise/Perlin3D.inl +++ b/include/Nazara/Noise/Perlin3D.inl @@ -2,9 +2,9 @@ // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include -#include +//#include +//#include +//#include template NzPerlin3D::NzPerlin3D() diff --git a/include/Nazara/Noise/Perlin4D.hpp b/include/Nazara/Noise/Perlin4D.hpp index c0af6feb1..897e3250e 100644 --- a/include/Nazara/Noise/Perlin4D.hpp +++ b/include/Nazara/Noise/Perlin4D.hpp @@ -8,7 +8,8 @@ #define PERLIN4D_H #include -#include +//#include +#include "NoiseBase.hpp" #include template class NzPerlin4D : public NzNoiseBase @@ -35,6 +36,7 @@ template class NzPerlin4D : public NzNoiseBase typedef NzPerlin4D NzPerlin4Df; typedef NzPerlin4D NzPerlin4Dd; -#include +//#include +#include "Perlin4D.inl" #endif // PERLIN4D_H diff --git a/include/Nazara/Noise/Perlin4D.inl b/include/Nazara/Noise/Perlin4D.inl index 4b51c127d..5df9b6a43 100644 --- a/include/Nazara/Noise/Perlin4D.inl +++ b/include/Nazara/Noise/Perlin4D.inl @@ -2,9 +2,9 @@ // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include -#include +//#include +//#include +//#include template NzPerlin4D::NzPerlin4D() diff --git a/include/Nazara/Noise/Simplex2D.hpp b/include/Nazara/Noise/Simplex2D.hpp index 79434b471..8d2221c09 100644 --- a/include/Nazara/Noise/Simplex2D.hpp +++ b/include/Nazara/Noise/Simplex2D.hpp @@ -8,7 +8,8 @@ #define SIMPLEX2D_H #include -#include +//#include +#include "NoiseBase.hpp" #include @@ -37,7 +38,8 @@ template class NzSimplex2D : public NzNoiseBase typedef NzSimplex2D NzSimplex2Df; typedef NzSimplex2D NzSimplex2Dd; -#include +//#include +#include "Simplex2D.inl" #endif // SIMPLEX2D_H diff --git a/include/Nazara/Noise/Simplex2D.inl b/include/Nazara/Noise/Simplex2D.inl index 129501617..1b3ac9311 100644 --- a/include/Nazara/Noise/Simplex2D.inl +++ b/include/Nazara/Noise/Simplex2D.inl @@ -2,9 +2,9 @@ // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include -#include +//#include +//#include +//#include template NzSimplex2D::NzSimplex2D() diff --git a/src/Nazara/DynaTerrain/Debug/Leaks.cpp b/src/Nazara/DynaTerrain/Debug/Leaks.cpp new file mode 100644 index 000000000..13e396e67 --- /dev/null +++ b/src/Nazara/DynaTerrain/Debug/Leaks.cpp @@ -0,0 +1,29 @@ +// Copyright (C) 2012 AUTHORS +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#if NAZARA_MODULENAME_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG) +#include +#include + +void* operator new(std::size_t size) throw(std::bad_alloc) +{ + return NzMemoryManager::Allocate(size, false); +} + +void* operator new[](std::size_t size) throw(std::bad_alloc) +{ + return NzMemoryManager::Allocate(size, true); +} + +void operator delete(void* pointer) throw() +{ + NzMemoryManager::Free(pointer, false); +} + +void operator delete[](void* pointer) throw() +{ + NzMemoryManager::Free(pointer, true); +} +#endif diff --git a/src/Nazara/DynaTerrain/DynaTerrain.cpp b/src/Nazara/DynaTerrain/DynaTerrain.cpp new file mode 100644 index 000000000..f6cbd4530 --- /dev/null +++ b/src/Nazara/DynaTerrain/DynaTerrain.cpp @@ -0,0 +1,57 @@ +// Copyright (C) 2012 AUTHORS +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +NzModuleName::NzModuleName() +{ +} + +NzModuleName::~NzModuleName() +{ + if (s_initialized) + Uninitialize(); +} + +bool NzModuleName::Initialize() +{ + #if NAZARA_MODULENAME_SAFE + if (s_initialized) + { + NazaraError("ModuleName already initialized"); + return true; + } + #endif + + // Initialisation du module + + s_initialized = true; + + return true; +} + +void NzModuleName::Uninitialize() +{ + #if NAZARA_MODULENAME_SAFE + if (!s_initialized) + { + NazaraError("ModuleName not initialized"); + return; + } + #endif + + // Libération du module + + s_initialized = false; +} + +bool NzModuleName::IsInitialized() +{ + return s_initialized; +} + +bool NzModuleName::s_initialized = false; diff --git a/src/Nazara/DynaTerrain/Node.cpp b/src/Nazara/DynaTerrain/Node.cpp new file mode 100644 index 000000000..4b479dde0 --- /dev/null +++ b/src/Nazara/DynaTerrain/Node.cpp @@ -0,0 +1,317 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "Node.hpp" +#include "QuadTree.hpp" +#include +//#include +//#include +//#include +//#include + + +NzNode::NzNode(NzQuadTree* quad, NzNode* parent, const NzVector2f& center, const NzVector2f& size, nzDirection dir) +{ + m_direction = dir; + m_center = center; + m_size = size; + m_isLeaf = false; + m_patchMemoryAllocated = false; + + m_associatedQuadTree = quad; + + m_topLeftLeaf = nullptr; + m_topRightLeaf = nullptr; + m_bottomLeftLeaf = nullptr; + m_bottomRightLeaf = nullptr; + + if(parent == 0) + { + m_isRoot = true; + m_level = 0; + } + else + { + m_level = parent->GetLevel()+1; + m_parent = parent; + m_isRoot = false; + } +} + +NzNode::~NzNode() +{ + if(m_isLeaf) + m_associatedQuadTree->UnRegisterLeaf(this); + if(m_patchMemoryAllocated) + delete m_patch; +} + +void NzNode::Subdivide() +{ + m_isLeaf = false; + m_associatedQuadTree->UnRegisterLeaf(this); + + if(m_topLeftLeaf == nullptr) + { + m_topLeftLeaf = new NzNode(m_associatedQuadTree,this,NzVector2f(m_center.x-m_size.x/2.f, + m_center.y+m_size.y/2.f),m_size/2.f,TOPLEFT); + m_topLeftLeaf->m_isLeaf = true; + m_associatedQuadTree->RegisterLeaf(m_topLeftLeaf); + } + if(m_topRightLeaf == nullptr) + { + m_topRightLeaf = new NzNode(m_associatedQuadTree,this,NzVector2f(m_center.x+m_size.x/2.f, + m_center.y+m_size.y/2.f),m_size/2.f,TOPRIGHT); + m_topRightLeaf->m_isLeaf = true; + m_associatedQuadTree->RegisterLeaf(m_topRightLeaf); + } + if(m_bottomLeftLeaf = nullptr) + { + m_bottomLeftLeaf = new NzNode(m_associatedQuadTree,this,NzVector2f(m_center.x-m_size.x/2.f, + m_center.y-m_size.y/2.f),m_size/2.f,BOTTOMLEFT); + m_bottomLeftLeaf->m_isLeaf = true; + m_associatedQuadTree->RegisterLeaf(m_bottomLeftLeaf); + } + if(m_bottomRightLeaf = nullptr) + { + m_bottomRightLeaf = new NzNode(m_associatedQuadTree,this,NzVector2f(m_center.x+m_size.x/2.f, + m_center.y-m_size.y/2.f),m_size/2.f,BOTTOMRIGHT); + m_bottomRightLeaf->m_isLeaf = true; + m_associatedQuadTree->RegisterLeaf(m_bottomRightLeaf); + } + + +} + +void NzNode::Refine(bool eraseMemory) +{ + m_isLeaf = true; + m_associatedQuadTree->RegisterLeaf(this); + if(eraseMemory) + { + /* + delete m_topLeftLeaf; + delete m_topRightLeaf; + delete m_bottomLeftLeaf; + delete m_bottomRightLeaf; + + m_topLeftLeaf = nullptr; + m_topRightLeaf = nullptr; + m_bottomLeftLeaf = nullptr; + m_bottomRightLeaf = nullptr;*/ + + m_topLeftLeaf->DeletePatch(); + m_topRightLeaf->DeletePatch(); + m_bottomLeftLeaf->DeletePatch(); + m_bottomRightLeaf->DeletePatch(); + } + //else + //{ + m_topLeftLeaf->m_isLeaf = false; + m_topRightLeaf->m_isLeaf = false; + m_bottomLeftLeaf->m_isLeaf = false; + m_bottomRightLeaf->m_isLeaf = false; + + m_associatedQuadTree->UnRegisterLeaf(m_topLeftLeaf); + m_associatedQuadTree->UnRegisterLeaf(m_topRightLeaf); + m_associatedQuadTree->UnRegisterLeaf(m_bottomLeftLeaf); + m_associatedQuadTree->UnRegisterLeaf(m_bottomRightLeaf); + //} + +} + +void NzNode::CreatePatch(const NzVector2f& center, const NzVector2f& size) +{ + m_patchMemoryAllocated = true; + m_patch = new NzPatch(center,size); +} + +void NzNode::DeletePatch() +{ + m_patchMemoryAllocated = false; + delete m_patch; +} + +unsigned short int NzNode::GetLevel() const +{ + return m_level; +} + +bool NzNode::LocateNeighbor(nzDirection dir, NzNode* neighbor) +{ + NzNode* temp = m_parent; + std::stack treePath; + treePath.push(m_direction); + neighbor = nullptr; + + switch(dir) + { + case TOP: + //Part 1 + while(temp->m_direction != (BOTTOMLEFT || BOTTOMRIGHT || CENTER)) + { + treePath.push(temp->m_direction); + temp = temp->m_parent; + } + + //Part 2 + if(temp->m_direction == BOTTOMLEFT) + temp = temp->m_parent->m_topLeftLeaf; + else if(temp->m_direction == BOTTOMRIGHT) + temp = temp->m_parent->m_topRightLeaf; + else if(temp->m_direction == CENTER) + return false;//No Neighbor existing + + //Part 3 + while(!temp->IsLeaf()) + { + if(treePath.empty()) + { + //Le chemin de redescente est plus court que celui de montée (level[node départ] < level[node d'arrivée]) + break; + } + else + { + if(treePath.top() == TOPRIGHT) + temp = temp->m_bottomRightLeaf; + else if(treePath.top() == TOPLEFT) + temp = temp->m_bottomLeftLeaf; + else//D'uh ? + { + //Logger une erreur + return false; + } + treePath.pop(); + } + } + neighbor = temp; + return true; + break; + + case BOTTOM: + //Part 1 + while(temp->m_direction != (TOPLEFT || TOPRIGHT || CENTER)) + { + treePath.push(temp->m_direction); + temp = temp->m_parent; + } + + //Part 2 + if(temp->m_direction == TOPLEFT) + temp = temp->m_parent->m_bottomLeftLeaf; + else if(temp->m_direction == TOPRIGHT) + temp = temp->m_parent->m_bottomRightLeaf; + else if(temp->m_direction == CENTER) + return false;//No Neighbor existing + + //Part 3 + while(!temp->IsLeaf()) + { + if(treePath.empty()) + { + //Le chemin de redescente est plus court que celui de montée (level[node départ] < level[node d'arrivée]) + break; + } + else + { + if(treePath.top() == BOTTOMRIGHT) + temp = temp->m_topRightLeaf; + else if(treePath.top() == BOTTOMLEFT) + temp = temp->m_topLeftLeaf; + else//D'uh ? + { + //Logger une erreur + return false; + } + treePath.pop(); + } + } + neighbor = temp; + return true; + break; + + case LEFT: + //Part 1 + while(temp->m_direction != (TOPRIGHT || BOTTOMRIGHT || CENTER)) + { + treePath.push(temp->m_direction); + temp = temp->m_parent; + } + + //Part 2 + if(temp->m_direction == TOPRIGHT) + temp = temp->m_parent->m_topLeftLeaf; + else if(temp->m_direction == BOTTOMRIGHT) + temp = temp->m_parent->m_bottomLeftLeaf; + else if(temp->m_direction == CENTER) + return false;//No Neighbor existing + + //Part 3 + while(!temp->IsLeaf()) + { + if(treePath.top() == TOPLEFT) + temp = temp->m_topRightLeaf; + else if(treePath.top() == BOTTOMLEFT) + temp = temp->m_bottomRightLeaf; + else//D'uh ? + { + //Logger une erreur + return false; + } + treePath.pop(); + } + neighbor = temp; + return true; + break; + + case RIGHT: + //Part 1 + while(temp->m_direction != (TOPLEFT || BOTTOMLEFT || CENTER)) + { + treePath.push(temp->m_direction); + temp = temp->m_parent; + } + + //Part 2 + if(temp->m_direction == TOPLEFT) + temp = temp->m_parent->m_topRightLeaf; + else if(temp->m_direction == BOTTOMLEFT) + temp = temp->m_parent->m_bottomRightLeaf; + else if(temp->m_direction == CENTER) + return false;//No Neighbor existing + + //Part 3 + while(!temp->IsLeaf()) + { + if(treePath.top() == TOPRIGHT) + temp = temp->m_topLeftLeaf; + else if(treePath.top() == BOTTOMRIGHT) + temp = temp->m_bottomLeftLeaf; + else//D'uh ? + { + //Logger une erreur + return false; + } + treePath.pop(); + } + neighbor = temp; + return true; + break; + + default : + return false; + break; + } +} + +bool NzNode::IsLeaf() const +{ + return m_isLeaf; +} + +bool NzNode::IsRoot() const +{ + return m_isRoot; +} + diff --git a/src/Nazara/DynaTerrain/Patch.cpp b/src/Nazara/DynaTerrain/Patch.cpp new file mode 100644 index 000000000..305bd479a --- /dev/null +++ b/src/Nazara/DynaTerrain/Patch.cpp @@ -0,0 +1,47 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "Patch.hpp" +//#include +//#include +//#include +//#include + + + +NzPatch::NzPatch(NzVector2f center, NzVector2f size) +{ + m_center = center; + m_size = size; +} + +NzPatch::~NzPatch() +{ + //dtor +} + +NzVector2f NzPatch::GetCenter() const +{ + +} + +NzVector2f NzPatch::GetSize() const +{ + +} + +bool NzPatch::IntersectsCircle(const NzVector2f& center, double radius) +{ + +} + +bool NzPatch::IsContainedByCircle(const NzVector2f& center, double radius) +{ + +} + +NzPatch* NzPatch::LocatePatch(const NzVector2f& position) +{ + +} diff --git a/src/Nazara/DynaTerrain/QuadTree.cpp b/src/Nazara/DynaTerrain/QuadTree.cpp new file mode 100644 index 000000000..03f0d8d09 --- /dev/null +++ b/src/Nazara/DynaTerrain/QuadTree.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2012 Rémi Bèges +// This file is part of the "Nazara Engine". +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include "QuadTree.hpp" + +//#include +//#include +//#include +//#include + +NzQuadTree::NzQuadTree(const NzVector2f& terrainCenter, const NzVector2f& terrainSize) +{ + root = new NzNode(this,0,terrainCenter,terrainSize); +} + +void NzQuadTree::RegisterLeaf(NzNode* node) +{ + leaves.push_back(node); +} + +bool NzQuadTree::UnRegisterLeaf(NzNode* node) +{ + leaves.remove(node); +} + +NzNode* NzQuadTree::GetRootPtr() +{ + return root; +} + +NzQuadTree::~NzQuadTree() +{ + //dtor +} + diff --git a/src/Nazara/Noise/Noise.cpp b/src/Nazara/Noise/Noise.cpp index 151fdc0ed..c72266759 100644 --- a/src/Nazara/Noise/Noise.cpp +++ b/src/Nazara/Noise/Noise.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2012 Rémi "Overdrivr" Bèges (remi{dot}beges{at}gmail{dot}com) +// Copyright (C) 2012 AUTHORS // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp diff --git a/src/Nazara/Noise/NoiseBase.cpp b/src/Nazara/Noise/NoiseBase.cpp index 940a3ecf8..f864ad6ac 100644 --- a/src/Nazara/Noise/NoiseBase.cpp +++ b/src/Nazara/Noise/NoiseBase.cpp @@ -54,6 +54,42 @@ void NzNoiseBase::ShufflePermutationTable() perm[i] = perm[i & 255]; } +int NzNoiseBase::fastfloor(float n) +{ + if(n >= 0) + return static_cast(n); + else + return static_cast(n-1); +} + +int NzNoiseBase::JenkinsHash(int a, int b, int c) +{ + a = a-b; a = a - c; a = a^(static_cast(c) >> 13); + b = b-c; b = b - a; b = b^(a << 8); + c = c-a; c = c - b; c = c^(static_cast(b) >> 13); + a = a-b; a = a - c; a = a^(static_cast(c) >> 12); + b = b-c; b = b - a; b = b^(a << 16); + c = c-a; c = c - b; c = c^(static_cast(b) >> 5); + a = a-b; a = a - c; a = a^(static_cast(c) >> 3); + b = b-c; b = b - a; b = b^(a << 10); + c = c-a; c = c - b; c = c^(static_cast(b) >> 15); + return c; +} + +/* +//key = 64 bits +public long hash64shift(long key) +{ + key = (~key) + (key << 21); // key = (key << 21) - key - 1; + key = key ^ (key >>> 24); + key = (key + (key << 3)) + (key << 8); // key * 265 + key = key ^ (key >>> 14); + key = (key + (key << 2)) + (key << 4); // key * 21 + key = key ^ (key >>> 28); + key = key + (key << 31); + return key; +}*/ + NzNoiseBase::~NzNoiseBase() { //dtor diff --git a/src/Nazara/Noise/NoiseMachine.cpp b/src/Nazara/Noise/NoiseMachine.cpp index 8a1e2e697..2b2b42fc5 100644 --- a/src/Nazara/Noise/NoiseMachine.cpp +++ b/src/Nazara/Noise/NoiseMachine.cpp @@ -35,9 +35,6 @@ NzNoiseMachine::NzNoiseMachine(int seed) for(int j(0) ; j < 4 ; ++j) lookupTable4D[i][j] = lookupTemp4D[i][j]; - gradient1[0] = 1; - gradient1[1] = -1; - float unit = 1.0/sqrt(2); float grad2Temp[][2] = {{unit,unit},{-unit,unit},{unit,-unit},{-unit,-unit}, {1,0},{-1,0},{0,1},{0,-1}}; @@ -87,29 +84,6 @@ NzNoiseMachine::~NzNoiseMachine() //------------------------------ PERLIN ------------------------------ -float NzNoiseMachine::Get1DPerlinNoiseValue(float x, float res) -{ - nx = x/res; - x0 = static_cast(nx); - ii = x0 & 255; - - gi0 = perm[ii] % 2; - gi1 = perm[ii + 1] % 2; - - temp.x = nx-x0; - - s[0] = gradient1[gi0]*temp.x; - - temp.x = nx-(x0+1); - - t[0] = gradient1[gi1]*temp.x; - - tmp = nx-x0; - Cx = tmp * tmp * tmp * (tmp * (tmp * 6 - 15) + 10); - - return s[0] + Cx*(t[0]-s[0]); -} - float NzNoiseMachine::Get2DPerlinNoiseValue(float x, float y, float res) { nx = x/res; @@ -372,10 +346,10 @@ float NzNoiseMachine::Get4DPerlinNoiseValue(float x, float y, float z, float w, //------------------------------ SIMPLEX ------------------------------ -float NzNoiseMachine::Get2DSimplexNoiseValue(float x, float y, float resolution) +float NzNoiseMachine::Get2DSimplexNoiseValue(float x, float y, float res) { - x /= resolution; - y /= resolution; + x /= res; + y /= res; Origin.x = fastfloor(x + (x + y) * SkewCoeff2D); @@ -410,28 +384,17 @@ float NzNoiseMachine::Get2DSimplexNoiseValue(float x, float y, float resolution) ii = Origin.x & 255; jj = Origin.y & 255; - gi0 = perm[ii + perm[jj]] % 12; - gi1 = perm[ii + off1.x + perm[jj + off1.y]] % 12; - gi2 = perm[ii + 1 + perm[jj + 1]] % 12; + gi0 = perm[ii + perm[jj]] % 8; + gi1 = perm[ii + off1.x + perm[jj + off1.y]] % 8; + gi2 = perm[ii + 1 + perm[jj + 1]] % 8; - H[0].x = gradient3[gi0][0]; - H[0].y = gradient3[gi0][1]; + n1 = gradient2[gi0][0] * d1.x + gradient2[gi0][1] * d1.y; + n2 = gradient2[gi1][0] * d2.x + gradient2[gi1][1] * d2.y; + n3 = gradient2[gi2][0] * d3.x + gradient2[gi2][1] * d3.y; - H[1].x = gradient3[gi1][0]; - H[1].y = gradient3[gi1][1]; - - H[2].x = gradient3[gi2][0]; - H[2].y = gradient3[gi2][1]; - - n1 = H[0].x * d1.x + H[0].y * d1.y; - n2 = H[1].x * d2.x + H[1].y * d2.y; - n3 = H[2].x * d3.x + H[2].y * d3.y; - - lenght = 0.5; - - c1 = lenght - d1.x * d1.x - d1.y * d1.y; - c2 = lenght - d2.x * d2.x - d2.y * d2.y; - c3 = lenght - d3.x * d3.x - d3.y * d3.y; + c1 = 0.5 - d1.x * d1.x - d1.y * d1.y; + c2 = 0.5 - d2.x * d2.x - d2.y * d2.y; + c3 = 0.5 - d3.x * d3.x - d3.y * d3.y; if(c1 < 0) c1 = 0; @@ -447,11 +410,11 @@ float NzNoiseMachine::Get2DSimplexNoiseValue(float x, float y, float resolution) return (n1+n2+n3)*23.2; } -float NzNoiseMachine::Get3DSimplexNoiseValue(float x, float y, float z, float resolution) +float NzNoiseMachine::Get3DSimplexNoiseValue(float x, float y, float z, float res) { - x /= resolution; - y /= resolution; - z /= resolution; + x /= res; + y /= res; + z /= res; Origin.x = fastfloor(x + (x + y + z) * SkewCoeff3D); Origin.y = fastfloor(y + (x + y + z) * SkewCoeff3D); @@ -556,12 +519,10 @@ float NzNoiseMachine::Get3DSimplexNoiseValue(float x, float y, float z, float re n3 = gradient3[gi2][0] * d3.x + gradient3[gi2][1] * d3.y + gradient3[gi2][2] * d3.z; n4 = gradient3[gi3][0] * d4.x + gradient3[gi3][1] * d4.y + gradient3[gi3][2] * d4.z; - lenght = 0.6; - - c1 = lenght - d1.x * d1.x - d1.y * d1.y - d1.z * d1.z; - c2 = lenght - d2.x * d2.x - d2.y * d2.y - d2.z * d2.z; - c3 = lenght - d3.x * d3.x - d3.y * d3.y - d3.z * d3.z; - c4 = lenght - d4.x * d4.x - d4.y * d4.y - d4.z * d4.z; + c1 = 0.6 - d1.x * d1.x - d1.y * d1.y - d1.z * d1.z; + c2 = 0.6 - d2.x * d2.x - d2.y * d2.y - d2.z * d2.z; + c3 = 0.6 - d3.x * d3.x - d3.y * d3.y - d3.z * d3.z; + c4 = 0.6 - d4.x * d4.x - d4.y * d4.y - d4.z * d4.z; if(c1 < 0) c1 = 0; @@ -580,12 +541,12 @@ float NzNoiseMachine::Get3DSimplexNoiseValue(float x, float y, float z, float re return (n1+n2+n3+n4)*17.6995; } -float NzNoiseMachine::Get4DSimplexNoiseValue(float x, float y, float z, float w, float resolution) +float NzNoiseMachine::Get4DSimplexNoiseValue(float x, float y, float z, float w, float res) { - x /= resolution; - y /= resolution; - z /= resolution; - w /= resolution; + x /= res; + y /= res; + z /= res; + w /= res; Origin.x = fastfloor(x + (x + y + z + w) * SkewCoeff4D); Origin.y = fastfloor(y + (x + y + z + w) * SkewCoeff4D); @@ -667,13 +628,11 @@ float NzNoiseMachine::Get4DSimplexNoiseValue(float x, float y, float z, float w, n4 = gradient4[gi3][0]*d4.x + gradient4[gi3][1]*d4.y + gradient4[gi3][2]*d4.z + gradient4[gi3][3]*d4.w; n5 = gradient4[gi4][0]*d5.x + gradient4[gi4][1]*d5.y + gradient4[gi4][2]*d5.z + gradient4[gi4][3]*d5.w; - lenght = 0.6; - - c1 = lenght - d1.x*d1.x - d1.y*d1.y - d1.z*d1.z - d1.w*d1.w; - c2 = lenght - d2.x*d2.x - d2.y*d2.y - d2.z*d2.z - d2.w*d2.w; - c3 = lenght - d3.x*d3.x - d3.y*d3.y - d3.z*d3.z - d3.w*d3.w; - c4 = lenght - d4.x*d4.x - d4.y*d4.y - d4.z*d4.z - d4.w*d4.w; - c5 = lenght - d5.x*d5.x - d5.y*d5.y - d5.z*d5.z - d5.w*d5.w; + c1 = 0.6 - d1.x*d1.x - d1.y*d1.y - d1.z*d1.z - d1.w*d1.w; + c2 = 0.6 - d2.x*d2.x - d2.y*d2.y - d2.z*d2.z - d2.w*d2.w; + c3 = 0.6 - d3.x*d3.x - d3.y*d3.y - d3.z*d3.z - d3.w*d3.w; + c4 = 0.6 - d4.x*d4.x - d4.y*d4.y - d4.z*d4.z - d4.w*d4.w; + c5 = 0.6 - d5.x*d5.x - d5.y*d5.y - d5.z*d5.z - d5.w*d5.w; if(c1 < 0) c1 = 0; @@ -712,7 +671,7 @@ float NzNoiseMachine::Get3DCellNoiseValue(float x, float y, float z, float res) y0 = static_cast(y); z0 = static_cast(z); - return 0; + return (this->JenkinsHash(x0,y0,z0) & 255); } float NzNoiseMachine::Get4DCellNoiseValue(float x, float y, float z, float w, float res) { @@ -776,24 +735,6 @@ void NzNoiseMachine::RecomputeExponentArray() //------------------------------ FBM ------------------------------ -float NzNoiseMachine::Get1DFBMNoiseValue(float x, float res) -{ - value = 0.0; - RecomputeExponentArray(); - - for (int i(0); i < m_octaves; ++i) - { - value += Get1DPerlinNoiseValue(x,res) * exponent_array[i]; - x *= m_lacunarity; - } - remainder = m_octaves - (float)m_octaves; - - if(remainder != 0) - value += remainder * Get1DPerlinNoiseValue(x,res) * exponent_array[(int)m_octaves-1]; - - return value * m_sum; -} - float NzNoiseMachine::Get2DFBMNoiseValue(float x, float y, float res) { value = 0.0; @@ -908,11 +849,3 @@ float NzNoiseMachine::Get3DHybridMultiFractalNoiseValue(float x, float y, float return result; } - -int NzNoiseMachine::fastfloor(float n) -{ - if(n >= 0) - return static_cast(n); - else - return static_cast(n-1); -}