1
0
Fork 0

Added TwoHeights shape generator.

This is a faster shape generator that can generate overhangs and has biome awareness.
This commit is contained in:
Mattes D 2014-11-23 18:16:20 +01:00
parent 9f4342434b
commit 478bbad5ed
5 changed files with 162 additions and 8 deletions

View File

@ -31,8 +31,10 @@ SET (SRCS
StructGen.cpp
TestRailsGen.cpp
Trees.cpp
TwoHeights.cpp
UnderwaterBaseGen.cpp
VillageGen.cpp)
VillageGen.cpp
)
SET (HDRS
BioGen.h
@ -65,8 +67,10 @@ SET (HDRS
StructGen.h
TestRailsGen.h
Trees.h
TwoHeights.h
UnderwaterBaseGen.h
VillageGen.h)
VillageGen.h
)
if(NOT MSVC)
add_library(Generating ${SRCS} ${HDRS})

View File

@ -170,7 +170,11 @@ public:
m_BiomeGen(a_BiomeGen)
{
}
// cTerrainHeightGen overrides:
virtual void GenHeightMap(int a_ChunkX, int a_ChunkZ, cChunkDef::HeightMap & a_HeightMap) override;
virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
protected:
typedef cChunkDef::BiomeMap BiomeNeighbors[3][3];
@ -187,11 +191,8 @@ protected:
float m_BaseHeight;
} ;
static const sGenParam m_GenParam[256];
// cTerrainHeightGen overrides:
virtual void GenHeightMap(int a_ChunkX, int a_ChunkZ, cChunkDef::HeightMap & a_HeightMap) override;
virtual void InitializeHeightGen(cIniFile & a_IniFile) override;
NOISE_DATATYPE GetHeightAt(int a_RelX, int a_RelZ, int a_ChunkX, int a_ChunkZ, const BiomeNeighbors & a_BiomeNeighbors);
} ;

View File

@ -9,6 +9,7 @@
#include "DistortedHeightmap.h"
#include "EndGen.h"
#include "Noise3DGenerator.h"
#include "TwoHeights.h"
@ -121,6 +122,10 @@ cTerrainShapeGenPtr cTerrainShapeGen::CreateShapeGen(cIniFile & a_IniFile, cBiom
{
res = std::make_shared<cNoise3DComposable>(a_Seed);
}
else if (NoCaseCompare(shapeGenName, "TwoHeights") == 0)
{
res = CreateShapeGenTwoHeights(a_Seed, a_BiomeGen);
}
else
{
// No match found, force-set the default and retry

View File

@ -0,0 +1,121 @@
// TwoHeights.cpp
// Implements the cTwoHeights class representing the terrain shape generator using two switched heightmaps
#include "Globals.h"
#include "TwoHeights.h"
#include "../Noise/InterpolNoise.h"
#include "HeiGen.h"
#include "../LinearUpscale.h"
#include "../IniFile.h"
class cTwoHeights:
public cTerrainShapeGen
{
typedef cTerrainShapeGen super;
public:
cTwoHeights(int a_Seed, cBiomeGenPtr a_BiomeGen):
m_Seed(a_Seed),
m_Choice(a_Seed),
m_HeightA(a_Seed + 1, a_BiomeGen),
m_HeightB(a_Seed + 2, a_BiomeGen)
{
}
// cTerrainShapeGen override:
virtual void GenShape(int a_ChunkX, int a_ChunkZ, cChunkDesc::Shape & a_Shape) override
{
// Generate the two heightmaps:
cChunkDef::HeightMap heightsA;
cChunkDef::HeightMap heightsB;
m_HeightA.GenHeightMap(a_ChunkX, a_ChunkZ, heightsA);
m_HeightB.GenHeightMap(a_ChunkX, a_ChunkZ, heightsB);
// Generate the choice noise:
NOISE_DATATYPE smallChoice[33 * 5 * 5];
NOISE_DATATYPE workspace[33 * 5 * 5];
NOISE_DATATYPE startX = 0;
NOISE_DATATYPE endX = 256 * m_FrequencyY;
NOISE_DATATYPE startY = a_ChunkX * cChunkDef::Width * m_FrequencyX;
NOISE_DATATYPE endY = (a_ChunkX * cChunkDef::Width + cChunkDef::Width + 1) * m_FrequencyX;
NOISE_DATATYPE startZ = a_ChunkZ * cChunkDef::Width * m_FrequencyZ;
NOISE_DATATYPE endZ = (a_ChunkZ * cChunkDef::Width + cChunkDef::Width + 1) * m_FrequencyZ;
m_Choice.Generate3D(smallChoice, 33, 5, 5, startX, endX, startY, endY, startZ, endZ, workspace);
NOISE_DATATYPE choice[257 * 17 * 17];
LinearUpscale3DArray(smallChoice, 33, 5, 5, choice, 8, 4, 4);
// Generate the shape:
int idxShape = 0;
for (int z = 0; z < cChunkDef::Width; z++)
{
for (int x = 0; x < cChunkDef::Width; x++)
{
int idxChoice = 257 * 17 * z + 257 * x;
NOISE_DATATYPE heightA = static_cast<NOISE_DATATYPE>(cChunkDef::GetHeight(heightsA, x, z));
NOISE_DATATYPE heightB = static_cast<NOISE_DATATYPE>(cChunkDef::GetHeight(heightsB, x, z));
for (int y = 0; y < cChunkDef::Height; y++)
{
int height = static_cast<int>(ClampedLerp(heightA, heightB, choice[idxChoice++]));
a_Shape[idxShape++] = (y < height) ? 1 : 0;
}
} // for x
} // for z
}
virtual void InitializeShapeGen(cIniFile & a_IniFile)
{
m_FrequencyX = static_cast<NOISE_DATATYPE>(a_IniFile.GetValueSetF("Generator", "TwoHeightsFrequencyX", 40));
m_FrequencyY = static_cast<NOISE_DATATYPE>(a_IniFile.GetValueSetF("Generator", "TwoHeightsFrequencyY", 40));
m_FrequencyZ = static_cast<NOISE_DATATYPE>(a_IniFile.GetValueSetF("Generator", "TwoHeightsFrequencyZ", 40));
// Initialize the two underlying height generators from an empty INI file:
cIniFile empty;
m_HeightA.InitializeHeightGen(empty);
m_HeightB.InitializeHeightGen(empty);
// Add the choice octaves:
NOISE_DATATYPE freq = 0.001f;
NOISE_DATATYPE ampl = 1;
for (int i = 0; i < 4; i++)
{
m_Choice.AddOctave(freq, ampl);
freq = freq * 2;
ampl = ampl / 2;
}
}
protected:
int m_Seed;
/** The noise used to decide between the two heightmaps. */
cOctavedNoise<cInterpolNoise<Interp5Deg>> m_Choice;
/** The first height generator. */
cHeiGenBiomal m_HeightA;
/** The second height generator. */
cHeiGenBiomal m_HeightB;
/** The base frequencies for m_Choice in each of the world axis directions. */
NOISE_DATATYPE m_FrequencyX, m_FrequencyY, m_FrequencyZ;
};
cTerrainShapeGenPtr CreateShapeGenTwoHeights(int a_Seed, cBiomeGenPtr a_BiomeGen)
{
return std::make_shared<cTwoHeights>(a_Seed, a_BiomeGen);
}

View File

@ -0,0 +1,23 @@
// TwoHeights.h
// Declares the function to create a new instance of the cTwoHeights terrain shape generator
#pragma once
#include "ComposableGenerator.h"
/** Creates and returns a new instance of the cTwoHeights terrain shape generator.
The instance must be Initialize()-d before it is used. */
extern cTerrainShapeGenPtr CreateShapeGenTwoHeights(int a_Seed, cBiomeGenPtr a_BiomeGen);