New Map Generation

* Interpolation Class
* noise folder with NoiseParams, NoiseSampler, and NoiseSample classes
* MapGenJobs are in a seperate file
* Rewrite of MapGen class.
This commit is contained in:
aurailus 2019-03-01 17:16:02 -08:00
parent 7fb6bdc8ee
commit 012bea39e9
15 changed files with 375 additions and 113 deletions

View File

@ -90,6 +90,17 @@ set(ZEUS_SRC_FILES
server/ServerState.h
server/ConnectionList.cpp
server/ConnectionList.h
server/ServerPeer.h generic/network/PacketType.h generic/network/PacketType.h generic/network/PacketChannel.h client/game/gameworld/PlayerEntity.cpp client/game/gameworld/PlayerEntity.h client/engine/graphics/Histogram.cpp client/engine/graphics/Histogram.h client/engine/graphics/RectEntity.cpp client/engine/graphics/RectEntity.h)
server/ServerPeer.h
generic/network/PacketType.h
generic/network/PacketType.h
generic/network/PacketChannel.h
client/game/gameworld/PlayerEntity.cpp
client/game/gameworld/PlayerEntity.h
client/engine/graphics/Histogram.cpp
client/engine/graphics/Histogram.h
client/engine/graphics/RectEntity.cpp
client/engine/graphics/RectEntity.h
generic/gen/MapGenJob.h
generic/helpers/Interpolation.h generic/noise/NoiseParams.h generic/noise/NoiseSampler.cpp generic/noise/NoiseSampler.h generic/noise/NoiseSample.cpp generic/noise/NoiseSample.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -9,7 +9,7 @@
World::World(BlockAtlas *atlas) {
blockAtlas = atlas;
mapGen = new MapGen(0);
mapGen = new MapGen(1337);
for (int i = 0; i < GEN_THREADS; i++) {
genThreads.push_back(new ChunkThreadDef(mapGen));

View File

@ -96,15 +96,15 @@ private:
void handleChunkGenQueue();
void handleMeshGenQueue();
const int GEN_THREADS = 6;
const int GEN_QUEUE_SIZE = 16;
const int GEN_THREADS = 8;
const int GEN_QUEUE_SIZE = 8;
const int GEN_FINISHED_SIZE = GEN_THREADS * GEN_QUEUE_SIZE;
std::unordered_set<glm::vec3, vec3cmp> pendingGen;
std::vector<ChunkThreadDef*> genThreads;
std::vector<ChunkThreadData*> finishedGen;
const int MESH_THREADS = 6;
const int MESH_THREADS = 8;
const int MESH_QUEUE_SIZE = 8;
const int MESH_FINISHED_SIZE = GEN_THREADS * GEN_QUEUE_SIZE;

View File

@ -4,84 +4,88 @@
#include "MapGen.h"
void MapGen::init(unsigned int seed) {
}
MapGen::MapGen(unsigned int seed) : sampler(seed) {
float lerp(float p0, float p1, float factor) {
return p0 + factor * (p1 - p0);
}
p_feature = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_3D,
.NOISE_H_FACTOR = 30,
.NOISE_V_FACTOR = 200,
.SAMPLE_H_PRECISION = 4,
.SAMPLE_V_PRECISION = 4,
.NOISE_MULTIPLIER = 1
};
MapGen::noise_points MapGen::getNoisePoints(glm::vec3 pos, int hScale, int vScale) {
MapGen::noise_points points;
p_feature_scale = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 100,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 1
};
float offsetH = 16.0f / hScale;
float offsetV = 16.0f / vScale;
p_elevation = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 2000,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 200
};
for (int i = 0; i <= hScale; i++) {
std::vector<std::vector<float>> slice;
for (int j = 0; j <= vScale; j++) {
std::vector<float> row;
for (int k = 0; k <= hScale; k++) {
row.push_back(noiseAt(
pos.x * 16 + offsetH * i,
pos.y * 16 + offsetV * j,
pos.z * 16 + offsetH * k));
}
slice.push_back(row);
}
points.push_back(slice);
}
p_elevation_variation = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 300,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 100
};
return std::move(points);
}
float MapGen::noiseAt(float x, float y, float z) {
return (float)p.noise(x / BASE_H_FACTOR, y / BASE_V_FACTOR, z / BASE_H_FACTOR);
p_elevation_variation_smaller = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 100,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 2,
.SAMPLE_V_PRECISION = 2,
.NOISE_MULTIPLIER = 50
};
}
BlockChunk* MapGen::generate(glm::vec3 pos) {
MapGenJob j(pos);
MapGen::noise_points points = getNoisePoints(pos, BASE_H_PRECISION, BASE_V_PRECISION);
glm::vec3 lp(0, 0, 0);
float offsetH = 16.0f / BASE_H_PRECISION;
float offsetV = 16.0f / BASE_V_PRECISION;
for (int i = 0; i < 4096; i++) {
ArrayTrans3D::indAssignVec(i, lp);
auto xBase = (int)floor(lp.x / offsetH);
auto yBase = (int)floor(lp.y / offsetV);
auto zBase = (int)floor(lp.z / offsetH);
auto p000 = points[xBase][yBase][zBase];
auto p100 = points[xBase + 1][yBase][zBase];
auto p001 = points[xBase][yBase][zBase + 1];
auto p101 = points[xBase + 1][yBase][zBase + 1];
auto p010 = points[xBase][yBase + 1][zBase];
auto p110 = points[xBase + 1][yBase + 1][zBase];
auto p011 = points[xBase][yBase + 1][zBase + 1];
auto p111 = points[xBase + 1][yBase + 1][zBase + 1];
float xFac = ((int)lp.x % (int)offsetH) / offsetH;
float yFac = ((int)lp.y % (int)offsetV) / offsetV;
float zFac = ((int)lp.z % (int)offsetH) / offsetH;
float density =
lerp(lerp(lerp(p000, p100, xFac), lerp(p001, p101, xFac), zFac),
lerp(lerp(p010, p110, xFac), lerp(p011, p111, xFac), zFac), yFac);
float hills = (float)(p.noise((pos.x * 16 + lp.x) / HILLS_H_FACTOR, 0, (pos.x * 16 + lp.z) / HILLS_H_FACTOR) + 1) / 2.0f;
hills *= HILLS_EFFECT_MULT;
density -= ((pos.y * 16 + lp.y) / (HILLS_BASE_EFFECT * hills));
j.blocks[i] = density > 0.5;
}
buildElevation(j);
fillChunk(j);
return new BlockChunk(j.blocks);
}
}
void MapGen::buildElevation(MapGenJob &j) {
auto elevation_sample = sampler.sample(j.pos, p_elevation);
auto elevation_variation_sample = sampler.sample(j.pos, p_elevation_variation);
auto elevation_variation_smaller_sample = sampler.sample(j.pos, p_elevation_variation_smaller);
auto feature_sample = sampler.sample(j.pos, p_feature);
auto feature_scale_sample = sampler.sample(j.pos, p_feature_scale);
glm::vec3 lp;
for (int m = 0; m < 4096; m++) {
ArrayTrans3D::indAssignVec(m, lp);
j.density[m] = elevation_sample.get(lp)
+ elevation_variation_sample.get(lp)
+ elevation_variation_smaller_sample.get(lp)
+ ((float)pow(feature_sample.get(lp) + 0.5, 2.0) - 0.5f) * 50 * (feature_scale_sample.get(lp) + 0.5f)
- ((j.pos.y * 16 + lp.y));
}
}
void MapGen::fillChunk(MapGenJob &j) {
glm::vec3 lp;
for (int m = 0; m < 4096; m++) {
ArrayTrans3D::indAssignVec(m, lp);
j.blocks[m] = j.density[m] > 0 ? 3 : 0;
}
}

View File

@ -8,52 +8,28 @@
#include <vec3.hpp>
#include <vector>
#include "MapGenJob.h"
#include "../blocks/BlockChunk.h"
#include "../helpers/PerlinNoise.h"
#include "../noise/NoiseParams.h"
#include "../noise/NoiseSampler.h"
class MapGen {
public:
MapGen() : p(1) { init(1); }
explicit MapGen(unsigned int seed) : p(seed) { init(seed); }
explicit MapGen(unsigned int seed);
BlockChunk* generate(glm::vec3 pos);
private:
//Base 3D Perlin Noise
const float BASE_H_FACTOR = 22.0f;
const float BASE_V_FACTOR = 44.0f;
const int BASE_H_PRECISION = 2;
const int BASE_V_PRECISION = 4;
void buildElevation(MapGenJob &j);
void fillChunk(MapGenJob &j);
//Hillyness Factors
const float HILLS_H_FACTOR = 250.0f;
const float HILLS_BASE_EFFECT = 120.0f;
const float HILLS_EFFECT_MULT = 0.2f;
NoiseSampler sampler;
typedef std::vector<std::vector<std::vector<float>>> noise_points;
float noiseAt(float x, float y, float z);
noise_points getNoisePoints(glm::vec3 pos, int hScale, int vScale);
struct MapGenJob {
std::vector<int>
elevation,
biome,
blocks;
glm::vec3 pos = glm::vec3(0, 0, 0);
explicit MapGenJob(glm::vec3 pos) {
this->pos = pos;
elevation.reserve(4096);
biome.reserve(4096);
blocks = std::vector<int>(4096);
}
};
void init(unsigned int seed);
PerlinNoise p;
NoiseParams p_feature;
NoiseParams p_feature_scale;
NoiseParams p_elevation;
NoiseParams p_elevation_variation;
NoiseParams p_elevation_variation_smaller;
};
#endif //ZEUS_MAPGEN_H
#endif //ZEUS_MAPGEN_H

View File

@ -0,0 +1,27 @@
//
// Created by aurailus on 13/02/19.
//
#ifndef ZEUS_MAPGENJOB_H
#define ZEUS_MAPGENJOB_H
#include <vec3.hpp>
#include <vector>
struct MapGenJob {
std::vector<int> blocks;
std::vector<float> density;
glm::vec3 pos {};
explicit MapGenJob(glm::vec3 pos) {
this->pos = pos;
blocks = std::vector<int>(4096);
density = std::vector<float>(4096);
}
};
#endif //ZEUS_MAPGENJOB_H

View File

@ -0,0 +1,29 @@
//
// Created by aurailus on 15/02/19.
//
#ifndef ZEUS_INTERPOLATION_H
#define ZEUS_INTERPOLATION_H
class Interpolation {
public:
static float lerp(float p0, float p1, float factor) {
return p0 + factor * (p1 - p0);
}
static float bilerp(float x0z0, float x1z0, float x0z1, float x1z1, float xfactor, float zfactor) {
return lerp(lerp(x0z0, x1z0, xfactor), lerp(x0z1, x1z1, xfactor), zfactor);
}
static float trilerp(float p000, float p100, float p001, float p101,
float p010, float p110, float p011, float p111,
float xfactor, float zfactor, float yfactor) {
return lerp(lerp(lerp(p000, p100, xfactor), lerp(p001, p101, xfactor), zfactor),
lerp(lerp(p010, p110, xfactor), lerp(p011, p111, xfactor), zfactor), yfactor);
}
};
#endif //ZEUS_INTERPOLATION_H

View File

@ -0,0 +1,26 @@
//
// Created by aurailus on 15/02/19.
//
#ifndef ZEUS_NOISEPARAMS_H
#define ZEUS_NOISEPARAMS_H
struct NoiseParams {
bool PERLIN_TYPE;
float NOISE_H_FACTOR;
float NOISE_V_FACTOR;
int SAMPLE_H_PRECISION;
int SAMPLE_V_PRECISION;
float NOISE_MULTIPLIER;
//Enum
const static bool PERLIN_3D = true;
const static bool PERLIN_2D = false;
};
#endif //ZEUS_NOISEPARAMS_H

View File

@ -0,0 +1,76 @@
//
// Created by aurailus on 15/02/19.
//
#include <cmath>
#include "NoiseSample.h"
#include "../helpers/Interpolation.h"
NoiseSample::NoiseSample(int hPrecision, int vPrecision) {
this->hPrecision = hPrecision;
this->vPrecision = vPrecision;
data = std::vector<std::vector<std::vector<float>>>();
data.reserve((unsigned int)hPrecision + 1);
for (int i = 0; i <= hPrecision; i++) {
std::vector<std::vector<float>> subdata;
subdata.reserve((unsigned int)vPrecision + 1);
for (int j = 0; j <= vPrecision; j++) {
subdata.emplace_back((unsigned int)hPrecision + 1);
}
data.push_back(subdata);
}
}
void NoiseSample::set(glm::vec3 pos, float value) {
if (pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x > hPrecision || pos.y > vPrecision || pos.z > hPrecision) {
std::cerr << "Invalid index [1]" << std::endl;
return;
}
data[(int)pos.x][(int)pos.y][(int)pos.z] = value;
}
float NoiseSample::get(glm::vec3 pos) {
if (pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= 16 || pos.y >= 16|| pos.z >= 16) {
std::cerr << "Invalid index [2]" << std::endl;
return 0;
}
float offsetH = 16.0f / hPrecision;
float offsetV = 16.0f / vPrecision;
auto xBase = (int)std::floor(pos.x / offsetH);
auto yBase = (int)std::floor(pos.y / offsetV);
auto zBase = (int)std::floor(pos.z / offsetH);
float xFac = ((int) pos.x % (int) offsetH) / offsetH;
float yFac = ((int) pos.y % (int) offsetV) / offsetV;
float zFac = ((int) pos.z % (int) offsetH) / offsetH;
auto p000 = data[xBase][yBase][zBase];
auto p100 = data[xBase + 1][yBase][zBase];
auto p001 = data[xBase][yBase][zBase + 1];
auto p101 = data[xBase + 1][yBase][zBase + 1];
if (vPrecision > 1) {
auto p010 = data[xBase][yBase + 1][zBase];
auto p110 = data[xBase + 1][yBase + 1][zBase];
auto p011 = data[xBase][yBase + 1][zBase + 1];
auto p111 = data[xBase + 1][yBase + 1][zBase + 1];
return Interpolation::trilerp(
p000, p100, p001, p101,
p010, p110, p011, p111,
xFac, zFac, yFac
);
}
else {
return Interpolation::bilerp(
p000, p100, p001, p101, xFac, zFac
);
}
}

View File

@ -0,0 +1,27 @@
//
// Created by aurailus on 15/02/19.
//
#ifndef ZEUS_NOISESAMPLE_H
#define ZEUS_NOISESAMPLE_H
#include <vector>
#include <vec3.hpp>
#include <iostream>
class NoiseSample {
public:
NoiseSample(int hPrecision, int vPrecision);
void set(glm::vec3 pos, float value);
float get(glm::vec3 pos);
private:
std::vector<std::vector<std::vector<float>>> data;
int hPrecision;
int vPrecision;
};
#endif //ZEUS_NOISESAMPLE_H

View File

@ -0,0 +1,33 @@
//
// Created by aurailus on 15/02/19.
//
#include "NoiseSampler.h"
NoiseSampler::NoiseSampler(unsigned int seed) : noise(seed) {
this->seed = seed;
}
NoiseSample NoiseSampler::sample(glm::vec3 pos, NoiseParams &params) {
if (!params.PERLIN_TYPE) params.SAMPLE_V_PRECISION = 1;
NoiseSample s(params.SAMPLE_H_PRECISION, params.SAMPLE_V_PRECISION);
float offsetH = 16.0f / params.SAMPLE_H_PRECISION;
float offsetV = 16.0f / params.SAMPLE_V_PRECISION;
for (int i = 0; i <= params.SAMPLE_H_PRECISION; i++) {
for (int j = 0; j <= params.SAMPLE_V_PRECISION; j++) {
for (int k = 0; k <= params.SAMPLE_H_PRECISION; k++) {
float xCoord = (pos.x * 16 + offsetH * i) / params.NOISE_H_FACTOR;
float yCoord = (params.PERLIN_TYPE) ? (pos.y * 16 + offsetV * j) / params.NOISE_V_FACTOR : 0;
float zCoord = (pos.z * 16 + offsetH * k) / params.NOISE_H_FACTOR;
//Normalize result to -0.5 <= r <= 0.5
s.set(glm::vec3(i, j, k), (float)(noise.noise(xCoord, yCoord, zCoord) - 0.5f) * params.NOISE_MULTIPLIER);
}
}
}
return std::move(s);
}

View File

@ -0,0 +1,25 @@
//
// Created by aurailus on 15/02/19.
//
#ifndef ZEUS_NOISESAMPLER_H
#define ZEUS_NOISESAMPLER_H
#include "../helpers/PerlinNoise.h"
#include "NoiseSample.h"
#include "NoiseParams.h"
class NoiseSampler {
public:
explicit NoiseSampler(unsigned int seed);
NoiseSample sample(glm::vec3 pos, NoiseParams &params);
private:
PerlinNoise noise;
unsigned int seed;
};
#endif //ZEUS_NOISESAMPLER_H

View File

@ -2,6 +2,6 @@ set(ZEUS_TEST_FILES
tests/BlockChunk.cpp
tests/NetHandler.cpp
tests/Serializer.cpp
tests/Packet.cpp)
tests/PerlinNoise.cpp)
add_library (zeusTest ${ZEUS_TEST_FILES})

View File

@ -16,5 +16,6 @@ TEST_CASE("Sanity Check", "[core]") {
#include "tests/NetHandler.cpp"
#include "tests/Serializer.cpp"
#include "tests/Packet.cpp"
#include "tests/PerlinNoise.cpp"
#pragma clang diagnostic pop

View File

@ -0,0 +1,27 @@
//
// Created by aurailus on 22/02/19.
//
#include <catch.hpp>
#include <iostream>
#include "../../src/generic/helpers/PerlinNoise.h"
TEST_CASE("Perlin Range Check") {
PerlinNoise p;
double min = 0.5, max = 0.5;
for (int i = 0; i < 100000; i++) {
auto v = p.noise(i / 12.0f, i / 0.23f, i / 18.0f);
if (v > max) max = v;
if (v < min) min = v;
}
printf("Perlin Noise Exhibited Values:\nMin: %f, Max: %f\n", min, max);
REQUIRE(min < 0.1); //Require Min is around 0
REQUIRE(min >= 0); //Require Min is not below 0
REQUIRE(max > 0.9); //Require Max is around 1
REQUIRE(min <= 1); //Require Max is not above 1
}