summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Generating/Noise3DGenerator.cpp61
-rw-r--r--src/Generating/Noise3DGenerator.h3
-rw-r--r--src/Noise/InterpolNoise.h187
3 files changed, 219 insertions, 32 deletions
diff --git a/src/Generating/Noise3DGenerator.cpp b/src/Generating/Noise3DGenerator.cpp
index d051948c4..78b739d32 100644
--- a/src/Generating/Noise3DGenerator.cpp
+++ b/src/Generating/Noise3DGenerator.cpp
@@ -69,6 +69,15 @@ static class cInterpolNoiseSpeedTest
public:
cInterpolNoiseSpeedTest(void)
{
+ TestSpeed2D();
+ TestSpeed3D();
+ printf("InterpolNoise speed comparison finished.\n");
+ }
+
+
+ /** Compare the speed of the 3D InterpolNoise vs 3D CubicNoise. */
+ void TestSpeed3D(void)
+ {
printf("Evaluating 3D noise performance...\n");
static const int SIZE_X = 128;
static const int SIZE_Y = 128;
@@ -99,6 +108,38 @@ public:
printf("3D noise performance comparison finished.\n");
}
+
+ /** Compare the speed of the 2D InterpolNoise vs 2D CubicNoise. */
+ void TestSpeed2D(void)
+ {
+ printf("Evaluating 2D noise performance...\n");
+ static const int SIZE_X = 128;
+ static const int SIZE_Y = 128;
+ static const NOISE_DATATYPE MUL = 80;
+ std::unique_ptr<NOISE_DATATYPE[]> arr(new NOISE_DATATYPE[SIZE_X * SIZE_Y]);
+ cTimer timer;
+
+ // Test the cInterpolNoise:
+ cInterpolNoise<Interp5Deg> interpNoise(1);
+ long long start = timer.GetNowTime();
+ for (int i = 0; i < 500; i++)
+ {
+ interpNoise.Generate2D(arr.get(), SIZE_X, SIZE_Y, MUL * i, MUL * i + MUL, 0, MUL);
+ }
+ long long end = timer.GetNowTime();
+ printf("InterpolNoise took %.02f sec\n", static_cast<float>(end - start) / 1000);
+
+ // Test the cCubicNoise:
+ cCubicNoise cubicNoise(1);
+ start = timer.GetNowTime();
+ for (int i = 0; i < 500; i++)
+ {
+ cubicNoise.Generate2D(arr.get(), SIZE_X, SIZE_Y, MUL * i, MUL * i + MUL, 0, MUL);
+ }
+ end = timer.GetNowTime();
+ printf("CubicNoise took %.02f sec\n", static_cast<float>(end - start) / 1000);
+ printf("2D noise performance comparison finished.\n");
+ }
} g_InterpolNoiseSpeedTest;
#endif
@@ -120,6 +161,12 @@ cNoise3DGenerator::cNoise3DGenerator(cChunkGenerator & a_ChunkGenerator) :
m_Perlin.AddOctave(8, 0.125);
m_Perlin.AddOctave(16, 0.0625);
+ m_Cubic.AddOctave(1, 1);
+ m_Cubic.AddOctave(2, 0.5);
+ m_Cubic.AddOctave(4, 0.25);
+ m_Cubic.AddOctave(8, 0.125);
+ m_Cubic.AddOctave(16, 0.0625);
+
#if 0
// DEBUG: Test the noise generation:
// NOTE: In order to be able to run MCS with this code, you need to increase the default thread stack size
@@ -201,8 +248,8 @@ void cNoise3DGenerator::Initialize(cIniFile & a_IniFile)
{
// Params:
m_SeaLevel = a_IniFile.GetValueSetI("Generator", "Noise3DSeaLevel", 62);
- m_HeightAmplification = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DHeightAmplification", 0);
- m_MidPoint = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DMidPoint", 75);
+ m_HeightAmplification = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DHeightAmplification", 0.1);
+ m_MidPoint = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DMidPoint", 68);
m_FrequencyX = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DFrequencyX", 8);
m_FrequencyY = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DFrequencyY", 8);
m_FrequencyZ = (NOISE_DATATYPE)a_IniFile.GetValueSetF("Generator", "Noise3DFrequencyZ", 8);
@@ -280,23 +327,23 @@ void cNoise3DGenerator::GenerateNoiseArray(int a_ChunkX, int a_ChunkZ, NOISE_DAT
// Precalculate a "height" array:
NOISE_DATATYPE Height[DIM_X * DIM_Z]; // Output for the cubic noise heightmap ("source")
- m_Cubic.Generate2D(Height, DIM_X, DIM_Z, StartX / 25, EndX / 25, StartZ / 25, EndZ / 25);
+ m_Cubic.Generate2D(Height, DIM_X, DIM_Z, StartX / 5, EndX / 5, StartZ / 5, EndZ / 5);
for (size_t i = 0; i < ARRAYCOUNT(Height); i++)
{
- Height[i] = std::abs(Height[i]) * m_HeightAmplification + 1;
+ Height[i] = Height[i] * m_HeightAmplification;
}
// Modify the noise by height data:
for (int y = 0; y < DIM_Y; y++)
{
- NOISE_DATATYPE AddHeight = (y * UPSCALE_Y - m_MidPoint) / 20;
- AddHeight *= AddHeight * AddHeight;
+ NOISE_DATATYPE AddHeight = (y * UPSCALE_Y - m_MidPoint) / 30;
+ // AddHeight *= AddHeight * AddHeight;
for (int z = 0; z < DIM_Z; z++)
{
NOISE_DATATYPE * CurRow = &(NoiseO[y * DIM_X + z * DIM_X * DIM_Y]);
for (int x = 0; x < DIM_X; x++)
{
- CurRow[x] += AddHeight / Height[x + DIM_X * z];
+ CurRow[x] += AddHeight + Height[x + DIM_X * z];
}
}
}
diff --git a/src/Generating/Noise3DGenerator.h b/src/Generating/Noise3DGenerator.h
index d198c5498..07767ba84 100644
--- a/src/Generating/Noise3DGenerator.h
+++ b/src/Generating/Noise3DGenerator.h
@@ -47,7 +47,8 @@ protected:
/** The base 3D noise source for the actual composition */
cOctavedNoise<cInterp5DegNoise> m_Perlin;
- cCubicNoise m_Cubic; // The noise used for heightmap directing
+ /** The noise used for heightmap directing. */
+ cOctavedNoise<cInterp5DegNoise> m_Cubic;
int m_SeaLevel;
NOISE_DATATYPE m_HeightAmplification;
diff --git a/src/Noise/InterpolNoise.h b/src/Noise/InterpolNoise.h
index 69e7194c9..683b54563 100644
--- a/src/Noise/InterpolNoise.h
+++ b/src/Noise/InterpolNoise.h
@@ -18,6 +18,134 @@
////////////////////////////////////////////////////////////////////////////////
+// cInterpolCell2D:
+
+template <typename T>
+class cInterpolCell2D
+{
+public:
+ cInterpolCell2D(
+ const cNoise & a_Noise, ///< Noise to use for generating the random values
+ 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
+ const NOISE_DATATYPE * a_FracX, ///< Pointer to the array that stores the X fractional values
+ const NOISE_DATATYPE * a_FracY ///< Pointer to the attay that stores the Y fractional values
+ ):
+ m_Noise(a_Noise),
+ m_WorkRnds(&m_Workspace1),
+ m_CurFloorX(0),
+ m_CurFloorY(0),
+ m_Array(a_Array),
+ m_SizeX(a_SizeX),
+ m_SizeY(a_SizeY),
+ m_FracX(a_FracX),
+ m_FracY(a_FracY)
+ {
+ }
+
+
+ /** Generates part of the output noise array using the current m_WorkRnds[] values */
+ void Generate(
+ int a_FromX, int a_ToX,
+ int a_FromY, int a_ToY
+ )
+ {
+ for (int y = a_FromY; y < a_ToY; y++)
+ {
+ NOISE_DATATYPE Interp[2];
+ NOISE_DATATYPE FracY = T::coeff(m_FracY[y]);
+ Interp[0] = Lerp((*m_WorkRnds)[0][0], (*m_WorkRnds)[0][1], FracY);
+ Interp[1] = Lerp((*m_WorkRnds)[1][0], (*m_WorkRnds)[1][1], FracY);
+ int idx = y * m_SizeX + a_FromX;
+ for (int x = a_FromX; x < a_ToX; x++)
+ {
+ m_Array[idx++] = Lerp(Interp[0], Interp[1], T::coeff(m_FracX[x]));
+ } // for x
+ } // for y
+ }
+
+
+ /** Initializes m_WorkRnds[] with the specified values of the noise at the specified integral coords. */
+ void InitWorkRnds(int a_FloorX, int a_FloorY)
+ {
+ m_CurFloorX = a_FloorX;
+ m_CurFloorY = a_FloorY;
+ (*m_WorkRnds)[0][0] = m_Noise.IntNoise2D(m_CurFloorX, m_CurFloorY);
+ (*m_WorkRnds)[0][1] = m_Noise.IntNoise2D(m_CurFloorX, m_CurFloorY + 1);
+ (*m_WorkRnds)[1][0] = m_Noise.IntNoise2D(m_CurFloorX + 1, m_CurFloorY);
+ (*m_WorkRnds)[1][1] = m_Noise.IntNoise2D(m_CurFloorX + 1, m_CurFloorY + 1);
+ }
+
+
+ /** Updates m_WorkRnds[] for the new integral coords */
+ void Move(int a_NewFloorX, int a_NewFloorY)
+ {
+ // Swap the doublebuffer:
+ int OldFloorX = m_CurFloorX;
+ int OldFloorY = m_CurFloorY;
+ Workspace * OldWorkRnds = m_WorkRnds;
+ m_WorkRnds = (m_WorkRnds == &m_Workspace1) ? &m_Workspace2 : &m_Workspace1;
+
+ // Reuse as much of the old workspace as possible:
+ // TODO: Try out if simply calculating all 4 elements each time is faster than this monster loop
+ int DiffX = OldFloorX - a_NewFloorX;
+ int DiffY = OldFloorY - a_NewFloorY;
+ for (int x = 0; x < 2; x++)
+ {
+ int cx = a_NewFloorX + x;
+ int OldX = x - DiffX; // Where would this X be in the old grid?
+ for (int y = 0; y < 2; y++)
+ {
+ int cy = a_NewFloorY + y;
+ int OldY = y - DiffY; // Where would this Y be in the old grid?
+ if ((OldX >= 0) && (OldX < 2) && (OldY >= 0) && (OldY < 2))
+ {
+ (*m_WorkRnds)[x][y] = (*OldWorkRnds)[OldX][OldY];
+ }
+ else
+ {
+ (*m_WorkRnds)[x][y] = (NOISE_DATATYPE)m_Noise.IntNoise2D(cx, cy);
+ }
+ }
+ }
+ m_CurFloorX = a_NewFloorX;
+ m_CurFloorY = a_NewFloorY;
+ }
+
+protected:
+ typedef NOISE_DATATYPE Workspace[2][2];
+
+ /** The noise used for generating the values at integral coords. */
+ const cNoise & m_Noise;
+
+ /** The current random values; points to either m_Workspace1 or m_Workspace2 (doublebuffering) */
+ Workspace * m_WorkRnds;
+
+ /** Buffer 1 for workspace doublebuffering, used in Move() */
+ Workspace m_Workspace1;
+
+ /** Buffer 2 for workspace doublebuffering, used in Move() */
+ Workspace m_Workspace2;
+
+ /** Coords of the currently calculated m_WorkRnds[]. */
+ int m_CurFloorX, m_CurFloorY;
+
+ /** The output array to generate into. */
+ NOISE_DATATYPE * m_Array;
+
+ /** Dimensions of the output array. */
+ int m_SizeX, m_SizeY;
+
+ /** Arrays holding the fractional values of the coords in each direction. */
+ const NOISE_DATATYPE * m_FracX;
+ const NOISE_DATATYPE * m_FracY;
+} ;
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
// cInterpolCell3D:
/** Holds a cache of the last calculated integral noise values and interpolates between them en masse.
@@ -212,33 +340,44 @@ public:
NOISE_DATATYPE a_StartY, NOISE_DATATYPE a_EndY ///< Noise-space coords of the array in the Y direction
) const
{
- // Check params:
- ASSERT(a_SizeX > 1);
- ASSERT(a_SizeY > 1);
-
- // Generate the noise:
- size_t idx = 0;
- for (int y = 0; y < a_SizeY; y++)
+ ASSERT(a_SizeX > 0);
+ ASSERT(a_SizeY > 0);
+ ASSERT(a_SizeX < MAX_SIZE);
+ ASSERT(a_SizeY < MAX_SIZE);
+ ASSERT(a_StartX < a_EndX);
+ ASSERT(a_StartY < a_EndY);
+
+ // Calculate the integral and fractional parts of each coord:
+ int FloorX[MAX_SIZE];
+ int FloorY[MAX_SIZE];
+ NOISE_DATATYPE FracX[MAX_SIZE];
+ NOISE_DATATYPE FracY[MAX_SIZE];
+ int SameX[MAX_SIZE];
+ int SameY[MAX_SIZE];
+ int NumSameX, NumSameY;
+ CalcFloorFrac(a_SizeX, a_StartX, a_EndX, FloorX, FracX, SameX, NumSameX);
+ CalcFloorFrac(a_SizeY, a_StartY, a_EndY, FloorY, FracY, SameY, NumSameY);
+
+ cInterpolCell2D<T> Cell(m_Noise, a_Array, a_SizeX, a_SizeY, FracX, FracY);
+
+ Cell.InitWorkRnds(FloorX[0], FloorY[0]);
+
+ // Calculate query values using Cell:
+ int FromY = 0;
+ for (int y = 0; y < NumSameY; y++)
{
- NOISE_DATATYPE ratioY = static_cast<NOISE_DATATYPE>(y) / (a_SizeY - 1);
- NOISE_DATATYPE noiseY = Lerp(a_StartY, a_EndY, ratioY);
- int noiseYInt = FAST_FLOOR(noiseY);
- NOISE_DATATYPE ratioY = typename T(noiseY - static_cast<NOISE_DATATYPE>(noiseYInt));
- for (int x = 0; x < a_SizeX; x++)
+ int ToY = FromY + SameY[y];
+ int FromX = 0;
+ int CurFloorY = FloorY[FromY];
+ for (int x = 0; x < NumSameX; x++)
{
- NOISE_DATATYPE ratioX = static_cast<NOISE_DATATYPE>(x) / (a_SizeX - 1);
- NOISE_DATATYPE noiseX = Lerp(a_StartX, a_EndX, ratioX);
- int noiseXInt = FAST_FLOOR(noiseX);
- NOISE_DATATYPE ratioX = typename T(noiseX - static_cast<NOISE_DATATYPE>(noiseXInt));
-
- NOISE_DATATYPE valx0y0 = m_Noise.IntNoise2D(noiseXInt, noiseYInt);
- NOISE_DATATYPE valx1y0 = m_Noise.IntNoise2D(noiseXInt + 1, noiseYInt);
- NOISE_DATATYPE valx0y1 = m_Noise.IntNoise2D(noiseXInt, noiseYInt + 1);
- NOISE_DATATYPE valx1y1 = m_Noise.IntNoise2D(noiseXInt + 1, noiseYInt + 1);
- NOISE_DATATYPE valx0 = Lerp(valx0y0, valx0y1, ratioY);
- NOISE_DATATYPE valx1 = Lerp(valx1y1, valx1y1, ratioY);
- a_Array[idx++] = Lerp(valx0, valx1, ratioX);
+ int ToX = FromX + SameX[x];
+ Cell.Generate(FromX, ToX, FromY, ToY);
+ Cell.Move(FloorX[ToX], CurFloorY);
+ FromX = ToX;
} // for x
+ Cell.Move(FloorX[0], FloorY[ToY]);
+ FromY = ToY;
} // for y
}