Refactored cRidgedNoise into a separate template.
This allows us to make the ridges out of any noise and to combine the cRidgedNoise with cOctavedNoise.
This commit is contained in:
parent
8c54fc0f7d
commit
f683872f54
@ -124,6 +124,7 @@ SET (HDRS
|
||||
OctavedNoise.h
|
||||
ProbabDistrib.h
|
||||
RankManager.h
|
||||
RidgedNoise.h
|
||||
RCONServer.h
|
||||
Root.h
|
||||
Scoreboard.h
|
||||
|
171
src/Noise.cpp
171
src/Noise.cpp
@ -1044,179 +1044,10 @@ NOISE_DATATYPE cImprovedNoise::GetValueAt(int a_X, int a_Y, int a_Z)
|
||||
int A = m_Perm[a_X] + a_Y;
|
||||
int AA = m_Perm[A] + a_Z;
|
||||
|
||||
return Grad(m_Perm[AA], 0, 0, 0);
|
||||
return Grad(m_Perm[AA], 1, 1, 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// cRidgedMultiNoise:
|
||||
|
||||
cRidgedMultiNoise::cRidgedMultiNoise(void) :
|
||||
m_Seed(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
cRidgedMultiNoise::cRidgedMultiNoise(int a_Seed) :
|
||||
m_Seed(a_Seed)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cRidgedMultiNoise::SetSeed(int a_Seed)
|
||||
{
|
||||
m_Seed = a_Seed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cRidgedMultiNoise::AddOctave(float a_Frequency, float a_Amplitude)
|
||||
{
|
||||
m_Octaves.push_back(cOctave(m_Seed * ((int)m_Octaves.size() + 4) * 4 + 1024, a_Frequency, a_Amplitude));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cRidgedMultiNoise::Generate2D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y]
|
||||
int a_SizeX, int a_SizeY, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, ///< Noise-space coords of the array in the Y direction
|
||||
NOISE_DATATYPE * a_Workspace ///< Workspace that this function can use and trash
|
||||
) const
|
||||
{
|
||||
if (m_Octaves.empty())
|
||||
{
|
||||
// No work to be done
|
||||
ASSERT(!"RidgedMulti: No octaves to generate!");
|
||||
return;
|
||||
}
|
||||
|
||||
bool ShouldFreeWorkspace = (a_Workspace == nullptr);
|
||||
int ArrayCount = a_SizeX * a_SizeY;
|
||||
if (ShouldFreeWorkspace)
|
||||
{
|
||||
a_Workspace = new NOISE_DATATYPE[ArrayCount];
|
||||
}
|
||||
|
||||
// Generate the first octave directly into array:
|
||||
const cOctave & FirstOctave = m_Octaves.front();
|
||||
|
||||
FirstOctave.m_Noise.Generate2D(
|
||||
a_Workspace, a_SizeX, a_SizeY,
|
||||
a_StartX * FirstOctave.m_Frequency, a_EndX * FirstOctave.m_Frequency,
|
||||
a_StartY * FirstOctave.m_Frequency, a_EndY * FirstOctave.m_Frequency
|
||||
);
|
||||
NOISE_DATATYPE Amplitude = FirstOctave.m_Amplitude;
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] = fabs(a_Workspace[i] * Amplitude);
|
||||
}
|
||||
|
||||
// Add each octave:
|
||||
for (cOctaves::const_iterator itr = m_Octaves.begin() + 1, end = m_Octaves.end(); itr != end; ++itr)
|
||||
{
|
||||
// Generate cubic noise for the octave:
|
||||
itr->m_Noise.Generate2D(
|
||||
a_Workspace, a_SizeX, a_SizeY,
|
||||
a_StartX * itr->m_Frequency, a_EndX * itr->m_Frequency,
|
||||
a_StartY * itr->m_Frequency, a_EndY * itr->m_Frequency
|
||||
);
|
||||
// Add the cubic noise into the output:
|
||||
NOISE_DATATYPE Amplitude = itr->m_Amplitude;
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] += fabs(a_Workspace[i] * Amplitude);
|
||||
}
|
||||
}
|
||||
|
||||
if (ShouldFreeWorkspace)
|
||||
{
|
||||
delete[] a_Workspace;
|
||||
a_Workspace = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cRidgedMultiNoise::Generate3D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y + a_SizeX * a_SizeY * z]
|
||||
int a_SizeX, int a_SizeY, int a_SizeZ, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, ///< Noise-space coords of the array in the Y direction
|
||||
NOISE_DATATYPE a_StartZ, NOISE_DATATYPE a_EndZ, ///< Noise-space coords of the array in the Z direction
|
||||
NOISE_DATATYPE * a_Workspace ///< Workspace that this function can use and trash
|
||||
) const
|
||||
{
|
||||
if (m_Octaves.empty())
|
||||
{
|
||||
// No work to be done
|
||||
ASSERT(!"RidgedMulti: No octaves to generate!");
|
||||
return;
|
||||
}
|
||||
|
||||
bool ShouldFreeWorkspace = (a_Workspace == nullptr);
|
||||
int ArrayCount = a_SizeX * a_SizeY * a_SizeZ;
|
||||
if (ShouldFreeWorkspace)
|
||||
{
|
||||
a_Workspace = new NOISE_DATATYPE[ArrayCount];
|
||||
}
|
||||
|
||||
// Generate the first octave directly into array:
|
||||
const cOctave & FirstOctave = m_Octaves.front();
|
||||
|
||||
FirstOctave.m_Noise.Generate3D(
|
||||
a_Workspace, a_SizeX, a_SizeY, a_SizeZ,
|
||||
a_StartX * FirstOctave.m_Frequency, a_EndX * FirstOctave.m_Frequency,
|
||||
a_StartY * FirstOctave.m_Frequency, a_EndY * FirstOctave.m_Frequency,
|
||||
a_StartZ * FirstOctave.m_Frequency, a_EndZ * FirstOctave.m_Frequency
|
||||
);
|
||||
NOISE_DATATYPE Amplitude = FirstOctave.m_Amplitude;
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] = a_Workspace[i] * Amplitude;
|
||||
}
|
||||
|
||||
// Add each octave:
|
||||
for (cOctaves::const_iterator itr = m_Octaves.begin() + 1, end = m_Octaves.end(); itr != end; ++itr)
|
||||
{
|
||||
// Generate cubic noise for the octave:
|
||||
itr->m_Noise.Generate3D(
|
||||
a_Workspace, a_SizeX, a_SizeY, a_SizeZ,
|
||||
a_StartX * itr->m_Frequency, a_EndX * itr->m_Frequency,
|
||||
a_StartY * itr->m_Frequency, a_EndY * itr->m_Frequency,
|
||||
a_StartZ * itr->m_Frequency, a_EndZ * itr->m_Frequency
|
||||
);
|
||||
// Add the cubic noise into the output:
|
||||
NOISE_DATATYPE Amplitude = itr->m_Amplitude;
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] += a_Workspace[i] * Amplitude;
|
||||
}
|
||||
}
|
||||
|
||||
if (ShouldFreeWorkspace)
|
||||
{
|
||||
delete[] a_Workspace;
|
||||
a_Workspace = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
66
src/Noise.h
66
src/Noise.h
@ -11,6 +11,7 @@
|
||||
typedef float NOISE_DATATYPE;
|
||||
|
||||
#include "OctavedNoise.h"
|
||||
#include "RidgedNoise.h"
|
||||
|
||||
|
||||
|
||||
@ -186,70 +187,7 @@ protected:
|
||||
|
||||
|
||||
typedef cOctavedNoise<cCubicNoise> cPerlinNoise;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class cRidgedMultiNoise
|
||||
{
|
||||
public:
|
||||
cRidgedMultiNoise(void);
|
||||
cRidgedMultiNoise(int a_Seed);
|
||||
|
||||
|
||||
void SetSeed(int a_Seed);
|
||||
|
||||
void AddOctave(NOISE_DATATYPE a_Frequency, NOISE_DATATYPE a_Amplitude);
|
||||
|
||||
void Generate1D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into
|
||||
int a_SizeX, ///< Count of the array
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array
|
||||
NOISE_DATATYPE * a_Workspace = nullptr ///< Workspace that this function can use and trash
|
||||
) const;
|
||||
|
||||
|
||||
void Generate2D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y]
|
||||
int a_SizeX, int a_SizeY, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, ///< Noise-space coords of the array in the Y direction
|
||||
NOISE_DATATYPE * a_Workspace = nullptr ///< Workspace that this function can use and trash
|
||||
) const;
|
||||
|
||||
|
||||
void Generate3D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y + a_SizeX * a_SizeY * z]
|
||||
int a_SizeX, int a_SizeY, int a_SizeZ, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, ///< Noise-space coords of the array in the Y direction
|
||||
NOISE_DATATYPE a_StartZ, NOISE_DATATYPE a_EndZ, ///< Noise-space coords of the array in the Z direction
|
||||
NOISE_DATATYPE * a_Workspace = nullptr ///< Workspace that this function can use and trash
|
||||
) const;
|
||||
|
||||
protected:
|
||||
class cOctave
|
||||
{
|
||||
public:
|
||||
cCubicNoise m_Noise;
|
||||
|
||||
NOISE_DATATYPE m_Frequency; // Coord multiplier
|
||||
NOISE_DATATYPE m_Amplitude; // Value multiplier
|
||||
|
||||
cOctave(int a_Seed, NOISE_DATATYPE a_Frequency, NOISE_DATATYPE a_Amplitude) :
|
||||
m_Noise(a_Seed),
|
||||
m_Frequency(a_Frequency),
|
||||
m_Amplitude(a_Amplitude)
|
||||
{
|
||||
}
|
||||
} ;
|
||||
|
||||
typedef std::vector<cOctave> cOctaves;
|
||||
|
||||
int m_Seed;
|
||||
cOctaves m_Octaves;
|
||||
} ;
|
||||
typedef cOctavedNoise<cRidgedNoise<cCubicNoise>> cRidgedMultiNoise;
|
||||
|
||||
|
||||
|
||||
|
@ -32,7 +32,7 @@ public:
|
||||
oct->SetSeed(a_Seed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** */
|
||||
void AddOctave(NOISE_DATATYPE a_Frequency, NOISE_DATATYPE a_Amplitude)
|
||||
@ -56,7 +56,7 @@ public:
|
||||
ASSERT(!"Perlin: No octaves to generate!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Allocate the workspace on the heap, if it wasn't given:
|
||||
std::unique_ptr<NOISE_DATATYPE[]> workspaceHeap;
|
||||
if (a_Workspace == nullptr)
|
||||
@ -78,7 +78,7 @@ public:
|
||||
{
|
||||
a_Array[i] = a_Workspace[i] * Amplitude;
|
||||
}
|
||||
|
||||
|
||||
// Add each octave:
|
||||
for (auto itr = m_Octaves.cbegin() + 1, end = m_Octaves.cend(); itr != end; ++itr)
|
||||
{
|
||||
@ -96,8 +96,8 @@ public:
|
||||
}
|
||||
} // for itr - m_Octaves[]
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** Fills a 3D array with the values of the noise. */
|
||||
void Generate3D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y + a_SizeX * a_SizeY * z]
|
||||
@ -114,7 +114,7 @@ public:
|
||||
ASSERT(!"Perlin: No octaves to generate!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Allocate the workspace on the heap, if it wasn't given:
|
||||
std::unique_ptr<NOISE_DATATYPE[]> workspaceHeap;
|
||||
if (a_Workspace == nullptr)
|
||||
@ -137,7 +137,7 @@ public:
|
||||
{
|
||||
a_Array[i] = a_Workspace[i] * Amplitude;
|
||||
}
|
||||
|
||||
|
||||
// Add each octave:
|
||||
for (auto itr = m_Octaves.cbegin() + 1, end = m_Octaves.cend(); itr != end; ++itr)
|
||||
{
|
||||
@ -169,7 +169,7 @@ protected:
|
||||
|
||||
/** Value multiplier. */
|
||||
NOISE_DATATYPE m_Amplitude;
|
||||
|
||||
|
||||
cOctave(int a_Seed, NOISE_DATATYPE a_Frequency, NOISE_DATATYPE a_Amplitude) :
|
||||
m_Noise(a_Seed),
|
||||
m_Frequency(a_Frequency),
|
||||
@ -181,7 +181,7 @@ protected:
|
||||
|
||||
|
||||
/** The seed used by the underlying generators. */
|
||||
int m_Seed;
|
||||
int m_Seed;
|
||||
|
||||
/** The octaves that compose this noise. */
|
||||
cOctaves m_Octaves;
|
||||
|
91
src/RidgedNoise.h
Normal file
91
src/RidgedNoise.h
Normal file
@ -0,0 +1,91 @@
|
||||
|
||||
// RidgedNoise.h
|
||||
|
||||
// Implements the cRidgedNoise template class that generates ridged noise based on another noise provider.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename N>
|
||||
class cRidgedNoise
|
||||
{
|
||||
public:
|
||||
/** Creates a new instance with the seed set to 0. */
|
||||
cRidgedNoise(void):
|
||||
m_Noise(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/** Creates a new instance with the specified seed. */
|
||||
cRidgedNoise(int a_Seed):
|
||||
m_Noise(a_Seed)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/** Sets the seed for the underlying noise. */
|
||||
void SetSeed(int a_Seed)
|
||||
{
|
||||
m_Noise.SetSeed(a_Seed);
|
||||
}
|
||||
|
||||
|
||||
/** Fills a 2D array with the values of the noise. */
|
||||
void Generate2D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y]
|
||||
int a_SizeX, int a_SizeY, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY ///< Noise-space coords of the array in the Y direction
|
||||
) const
|
||||
{
|
||||
int ArrayCount = a_SizeX * a_SizeY;
|
||||
m_Noise.Generate2D(
|
||||
a_Array, a_SizeX, a_SizeY,
|
||||
a_StartX, a_EndX,
|
||||
a_StartY, a_EndY
|
||||
);
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] = fabs(a_Array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Fills a 3D array with the values of the noise. */
|
||||
void Generate3D(
|
||||
NOISE_DATATYPE * a_Array, ///< Array to generate into [x + a_SizeX * y + a_SizeX * a_SizeY * z]
|
||||
int a_SizeX, int a_SizeY, int a_SizeZ, ///< Count of the array, in each direction
|
||||
NOISE_DATATYPE a_StartX, NOISE_DATATYPE a_EndX, ///< Noise-space coords of the array in the X direction
|
||||
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY, ///< Noise-space coords of the array in the Y direction
|
||||
NOISE_DATATYPE a_StartZ, NOISE_DATATYPE a_EndZ ///< Noise-space coords of the array in the Z direction
|
||||
) const
|
||||
{
|
||||
int ArrayCount = a_SizeX * a_SizeY * a_SizeZ;
|
||||
m_Noise.Generate2D(
|
||||
a_Array, a_SizeX, a_SizeY, a_SizeZ,
|
||||
a_StartX, a_EndX,
|
||||
a_StartY, a_EndY,
|
||||
a_StartZ, a_EndZ
|
||||
);
|
||||
for (int i = 0; i < ArrayCount; i++)
|
||||
{
|
||||
a_Array[i] = fabs(a_Array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
N m_Noise;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user