Forgot to commit some stuff (noise+dynaterrain) from previous commit

This commit is contained in:
Remi Beges
2012-05-31 15:35:52 +02:00
parent 9008c36802
commit 4d975fa67e
21 changed files with 589 additions and 206 deletions

View File

@@ -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 <Nazara/ModuleName/Config.hpp>
#if NAZARA_MODULENAME_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <new>
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

View File

@@ -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 <Nazara/ModuleName/ModuleName.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/ModuleName/Config.hpp>
#include <Nazara/ModuleName/Debug.hpp>
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;

View File

@@ -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 <stack>
//#include <Nazara/DynaTerrain/NoiseBase.hpp>
//#include <Nazara/DynaTerrain/Error.hpp>
//#include <Nazara/DynaTerrain/Config.hpp>
//#include <Nazara/DynaTerrain/Debug.hpp>
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<nzDirection> 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;
}

View File

@@ -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 <Nazara/DynaTerrain/NoiseBase.hpp>
//#include <Nazara/DynaTerrain/Error.hpp>
//#include <Nazara/DynaTerrain/Config.hpp>
//#include <Nazara/DynaTerrain/Debug.hpp>
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)
{
}

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2012 Rémi Bèges
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include "QuadTree.hpp"
//#include <Nazara/DynaTerrain/NoiseBase.hpp>
//#include <Nazara/DynaTerrain/Error.hpp>
//#include <Nazara/DynaTerrain/Config.hpp>
//#include <Nazara/DynaTerrain/Debug.hpp>
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
}

View File

@@ -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

View File

@@ -54,6 +54,42 @@ void NzNoiseBase::ShufflePermutationTable()
perm[i] = perm[i & 255];
}
int NzNoiseBase::fastfloor(float n)
{
if(n >= 0)
return static_cast<int>(n);
else
return static_cast<int>(n-1);
}
int NzNoiseBase::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;
}
/*
//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

View File

@@ -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<int>(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<int>(y);
z0 = static_cast<int>(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<int>(n);
else
return static_cast<int>(n-1);
}