Rewrite Map Generation using LibNoise!

master
aurailus 2019-04-01 23:03:36 -07:00
parent 7ba75bb805
commit 6fe900eb29
20 changed files with 118 additions and 2494 deletions

View File

@ -17,6 +17,7 @@ include_directories (
lib/gzip-hpp-master/include
lib/catch
lib/simplex
lib/libnoise
)
#Include all of the dynamic libraries
@ -29,6 +30,7 @@ set (ZEUS_LIBRARIES
lua #sol2
dl z #gzip
enet #enet
noise #libnoise
)
#Include 'Packages', I don't understand how this works

View File

@ -1,8 +1,6 @@
set(ZEUS_SRC_FILES
client/engine/graphics/Mesh.cpp
client/engine/graphics/Mesh.h
generic/helpers/PerlinNoise.cpp
generic/helpers/PerlinNoise.h
client/engine/Entity.cpp
client/engine/Entity.h
client/engine/graphics/Shader.cpp
@ -98,9 +96,6 @@ set(ZEUS_SRC_FILES
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
client/engine/Frustum.cpp
@ -114,6 +109,11 @@ set(ZEUS_SRC_FILES
server/world/WorldGenStream.cpp
server/world/WorldGenStream.h
client/game/localworld/WorldInterpolationStream.cpp
client/game/localworld/WorldInterpolationStream.h generic/helpers/Vec3Compare.h generic/helpers/ChunkVec.h client/game/localworld/MeshGenStream.cpp client/game/localworld/MeshGenStream.h generic/helpers/VecUtils.h)
client/game/localworld/WorldInterpolationStream.h
generic/helpers/Vec3Compare.h
generic/helpers/ChunkVec.h
client/game/localworld/MeshGenStream.cpp
client/game/localworld/MeshGenStream.h
generic/helpers/VecUtils.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -13,7 +13,6 @@
#include <vec3.hpp>
#include <gtc/type_ptr.hpp>
#include "../../../generic/helpers/PerlinNoise.h"
#include "../../../generic/helpers/ArrayTrans3D.h"
#include "../../../generic/blocks/BlockAtlas.h"
#include "../../../generic/blocks/BlockChunk.h"

View File

@ -1,74 +1,45 @@
#include <cmath>
//
// Created by aurailus on 28/01/19.
//
#include "MapGen.h"
#include "../../client/engine/Timer.h"
#include "../noise/NoiseSample.h"
MapGen::MapGen(unsigned int seed) : sampler(seed) {
MapGen::MapGen(unsigned int seed) {
this->seed = seed;
p_feature = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_3D,
.NOISE_H_FACTOR = 60,
.NOISE_V_FACTOR = 100,
.SAMPLE_H_PRECISION = 4,
.SAMPLE_V_PRECISION = 4,
.NOISE_MULTIPLIER = 2
};
terrainFlatBase.SetFrequency(0.15);
terrainFlatBase.SetPersistence(0.4);
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
};
terrainFlat.SetSourceModule(0, terrainFlatBase);
terrainFlat.SetScale(0.125);
terrainFlat.SetBias(-0.75);
p_density = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 2000,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 100
};
terrainType.SetFrequency(0.05);
terrainType.SetPersistence(0.25);
p_density_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 = 50
};
terrainMountains.SetFrequency(0.1);
p_density_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 = 25
};
terrainFinal.SetSourceModule(0, terrainFlat);
terrainFinal.SetSourceModule(1, terrainMountains);
p_flora_feature = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 100,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 2,
.SAMPLE_V_PRECISION = 2,
.NOISE_MULTIPLIER = 1
};
terrainFinal.SetControlModule(terrainType);
terrainFinal.SetBounds(0.0, 1000.0);
terrainFinal.SetEdgeFalloff(0.1);
p_flora_smaller = NoiseParams {
.PERLIN_TYPE = NoiseParams::PERLIN_2D,
.NOISE_H_FACTOR = 25,
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 4,
.SAMPLE_V_PRECISION = 4,
.NOISE_MULTIPLIER = 1
};
floraNoise.SetFrequency(2);
floraNoise.SetOctaveCount(4);
floraTurbulence.SetSourceModule(0, floraNoise);
floraTurbulence.SetFrequency(4.0);
floraTurbulence.SetPower(0.125);
floraFinal.SetSourceModule(0, floraTurbulence);
floraFinal.SetScale(3);
floraFinal.SetBias(1);
}
BlockChunk* MapGen::generate(glm::vec3 pos) {
@ -129,28 +100,19 @@ void MapGen::getElevation(MapGenJob &job) {
}
void MapGen::getDensityMap(MapGenJob &job) {
auto density_sample = sampler.sample(job.pos, p_density);
auto density_variation_sample = sampler.sample(job.pos, p_density_variation);
auto density_variation_smaller_sample = sampler.sample(job.pos, p_density_variation_smaller);
auto feature_sample = sampler.sample(job.pos, p_feature);
auto feature_scale_sample = sampler.sample(job.pos, p_feature_scale);
auto terrain_2d_sample = NoiseSample::getSample(&terrainFinal, job.pos, 4, 1, true);
glm::vec3 lp;
for (int m = 0; m < 4096; m++) {
ArrayTrans3D::indAssignVec(m, lp);
job.density[m] = density_sample.get(lp)
+ density_variation_sample.get(lp)
+ density_variation_smaller_sample.get(lp)
+ ((float)pow(feature_sample.get(lp) + 0.5, 2.0) - 0.5f) * 15
- ((job.pos.y * 16 + lp.y));
job.density[m] = terrain_2d_sample.get(lp) * 24.0f - (lp.y + job.pos.y * 16);
}
}
void MapGen::fillChunk(MapGenJob &job) {
auto flora_sample = sampler.sample(job.pos, p_flora_feature);
auto flora_smaller_sample = sampler.sample(job.pos, p_flora_smaller);
auto flora_2d_sample = NoiseSample::getSample(&floraFinal, job.pos, 16, 1, true);
glm::vec3 lp;
@ -158,16 +120,15 @@ void MapGen::fillChunk(MapGenJob &job) {
ArrayTrans3D::indAssignVec(m, lp);
int d = job.depth[m];
double grassType = (flora_sample.get(lp) + 0.5) * (flora_smaller_sample.get(lp) + 0.5);
int grassType = min((int)std::floor(flora_2d_sample.get(lp)), 5);
int tallGrass = (int)floor(grassType * 5.0 - 1);
if (tallGrass > 0) tallGrass += 5;
else tallGrass = 0;
if (grassType > 0) grassType += 5;
else grassType = 0;
job.blocks[m] = d == 0 ? 0
: d == 1 ? tallGrass
: d == 1 ? grassType
: d == 2 ? 1
: d <= 5 ? 2
: d <= 3 ? 2
: 3;
}
}

View File

@ -11,8 +11,10 @@
#include "MapGenJob.h"
#include "../blocks/BlockChunk.h"
#include "../noise/NoiseParams.h"
#include "../noise/NoiseSampler.h"
#include <noise/noise.h>
using namespace noise;
class MapGen {
public:
@ -24,16 +26,17 @@ private:
void fillChunk(MapGenJob &j);
NoiseSampler sampler;
unsigned int seed;
NoiseParams p_feature;
NoiseParams p_feature_scale;
NoiseParams p_density;
NoiseParams p_density_variation;
NoiseParams p_density_variation_smaller;
module::RidgedMulti terrainMountains;
module::Billow terrainFlatBase;
module::ScaleBias terrainFlat;
module::Perlin terrainType;
module::Select terrainFinal;
NoiseParams p_flora_feature;
NoiseParams p_flora_smaller;
module::Perlin floraNoise;
module::Turbulence floraTurbulence;
module::ScaleBias floraFinal;
};

View File

@ -1,94 +0,0 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#include "PerlinNoise.h"
// THIS IS A DIRECT TRANSLATION TO C++11 FROM THE REFERENCE
// JAVA IMPLEMENTATION OF THE IMPROVED PERLIN FUNCTION (see http://mrl.nyu.edu/~perlin/noise/)
// THE ORIGINAL JAVA IMPLEMENTATION IS COPYRIGHT 2002 KEN PERLIN
// I ADDED AN EXTRA METHOD THAT GENERATES A NEW PERMUTATION VECTOR (THIS IS NOT PRESENT IN THE ORIGINAL IMPLEMENTATION)
// Initialize with the reference values for the permutation vector
PerlinNoise::PerlinNoise() {
// Initialize the permutation vector with the reference values
p = {
151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,
8,99,37,240,21,10,23,190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,
35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,
134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,
55,46,245,40,244,102,143,54, 65,25,63,161,1,216,80,73,209,76,132,187,208, 89,
18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,
250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,
189,28,42,223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167,
43,172,9,129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,
97,228,251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,
107,49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 };
// Duplicate the permutation vector
p.insert(p.end(), p.begin(), p.end());
}
// Generate a new permutation vector based on the value of seed
PerlinNoise::PerlinNoise(unsigned int seed) {
p.resize(256);
// Fill p with values from 0 to 255
std::iota(p.begin(), p.end(), 0);
// Initialize a random engine with seed
std::default_random_engine engine(seed);
// Suffle using the above random engine
std::shuffle(p.begin(), p.end(), engine);
// Duplicate the permutation vector
p.insert(p.end(), p.begin(), p.end());
}
double PerlinNoise::noise(double x, double y, double z) {
// Find the unit cube that contains the point
int X = (int) floor(x) & 255;
int Y = (int) floor(y) & 255;
int Z = (int) floor(z) & 255;
// Find relative x, y,z of point in cube
x -= floor(x);
y -= floor(y);
z -= floor(z);
// Compute fade curves for each of x, y, z
double u = fade(x);
double v = fade(y);
double w = fade(z);
// Hash coordinates of the 8 cube corners
int A = p[X] + Y;
int AA = p[A] + Z;
int AB = p[A + 1] + Z;
int B = p[X + 1] + Y;
int BA = p[B] + Z;
int BB = p[B + 1] + Z;
// Add blended results from 8 corners of cube
double res = lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z), grad(p[BA], x-1, y, z)), lerp(u, grad(p[AB], x, y-1, z), grad(p[BB], x-1, y-1, z))), lerp(v, lerp(u, grad(p[AA+1], x, y, z-1), grad(p[BA+1], x-1, y, z-1)), lerp(u, grad(p[AB+1], x, y-1, z-1), grad(p[BB+1], x-1, y-1, z-1))));
return (res + 1.0)/2.0;
}
double PerlinNoise::fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
double PerlinNoise::lerp(double t, double a, double b) {
return a + t * (b - a);
}
double PerlinNoise::grad(int hash, double x, double y, double z) {
int h = hash & 15;
// Convert lower 4 bits of hash into 12 gradient directions
double u = h < 8 ? x : y,
v = h < 4 ? y : h == 12 || h == 14 ? x : z;
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
#pragma clang diagnostic pop

View File

@ -1,37 +0,0 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
// THIS CLASS IS A TRANSLATION TO C++11 FROM THE REFERENCE
// JAVA IMPLEMENTATION OF THE IMPROVED PERLIN FUNCTION (see http://mrl.nyu.edu/~perlin/noise/)
// THE ORIGINAL JAVA IMPLEMENTATION IS COPYRIGHT 2002 KEN PERLIN
// I ADDED AN EXTRA METHOD THAT GENERATES A NEW PERMUTATION VECTOR (THIS IS NOT PRESENT IN THE ORIGINAL IMPLEMENTATION)
#ifndef PERLINNOISE_H
#define PERLINNOISE_H
#include <vector>
#include <cmath>
#include <random>
#include <algorithm>
#include <numeric>
class PerlinNoise {
// The permutation vector
std::vector<int> p;
public:
// Initialize with the reference values for the permutation vector
PerlinNoise();
// Generate a new permutation vector based on the value of seed
explicit PerlinNoise(unsigned int seed);
// Get a noise value, for 2D images z can have any value
double noise(double x, double y, double z);
private:
double fade(double t);
double lerp(double t, double a, double b);
double grad(int hash, double x, double y, double z);
};
#endif
#pragma clang diagnostic pop

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +0,0 @@
//
// 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

@ -72,4 +72,26 @@ float NoiseSample::get(glm::vec3& pos) {
p000, p100, p001, p101, xFac, zFac
);
}
}
}
NoiseSample NoiseSample::getSample(noise::module::Module *module, glm::vec3 chunkPos, int hPrecision, int vPrecision, bool flat) {
NoiseSample s(hPrecision, vPrecision);
float offsetH = 16.0f / hPrecision;
float offsetV = 16.0f / vPrecision;
for (int i = 0; i <= hPrecision; i++) {
for (int j = 0; j <= vPrecision; j++) {
for (int k = 0; k <= hPrecision; k++) {
double xCoord = (chunkPos.x * 16 + offsetH * i) / 16.0;
double yCoord = (flat) ? 0 : (chunkPos.y * 16 + offsetV * j) / 16.0;
double zCoord = (chunkPos.z * 16 + offsetH * k) / 16.0;
s.set(glm::vec3(i, j, k), (float)module->GetValue(xCoord, yCoord, zCoord));
}
}
}
return std::move(s);
}

View File

@ -8,9 +8,12 @@
#include <vector>
#include <vec3.hpp>
#include <iostream>
#include <noise/noise.h>
class NoiseSample {
public:
static NoiseSample getSample(noise::module::Module *module, glm::vec3 chunkPos, int hPrecision = 8, int vPrecision = 8, bool flat = false);
NoiseSample(int hPrecision, int vPrecision);
void set(glm::vec3 pos, float value);

View File

@ -1,34 +0,0 @@
//
// Created by aurailus on 15/02/19.
//
#include "NoiseSampler.h"
NoiseSampler::NoiseSampler(unsigned int seed) {
this->seed = seed;
}
NoiseSample NoiseSampler::sample(glm::vec3 pos, NoiseParams &params) {
Simplex::seed(seed);
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;
s.set(glm::vec3(i, j, k), (Simplex::noise(glm::vec3(xCoord, yCoord, zCoord)) * 0.5f) * params.NOISE_MULTIPLIER);
}
}
}
return std::move(s);
}

View File

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

View File

@ -12,7 +12,7 @@
class ServerPlayer {
public:
const static int ACTIVE_RANGE = 20;
const static int ACTIVE_RANGE = 25;
explicit ServerPlayer(ServerPeer* peer);

View File

@ -2,6 +2,7 @@
// Created by aurailus on 05/03/19.
//
#include <algorithm>
#include "World.h"
#include "../../generic/network/PacketChannel.h"
@ -23,6 +24,7 @@ void World::addPlayer(ServerPlayer *player) {
}
std::sort(toGenerate.begin(), toGenerate.end(), [&](glm::vec3 a, glm::vec3 b) {
using namespace std;
return max(max(abs(a.x - pos.x), abs(a.y - pos.y)), abs(a.z - pos.z)) <
max(max(abs(b.x - pos.x), abs(b.y - pos.y)), abs(b.z - pos.z));
});

View File

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

View File

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

View File

@ -21,18 +21,18 @@ TEST_CASE("Blockchunks", "[networking]") {
for (int i = 0; i < 20; i++) {
auto b = getRandomChunk();
SECTION("BlockChunk RLE Encoding") {
auto rle = b->rleEncode();
auto b2 = new BlockChunk();
b2->rleDecode(rle);
for (int j = 0; j < 4096; j++) {
REQUIRE(b2->getBlock(j) == b->getBlock(j));
}
delete b2;
}
// SECTION("BlockChunk RLE Encoding") {
// auto rle = b->rleEncode();
//
// auto b2 = new BlockChunk();
// b2->rleDecode(rle);
//
// for (int j = 0; j < 4096; j++) {
// REQUIRE(b2->getBlock(j) == b->getBlock(j));
// }
//
// delete b2;
// }
auto gzip = b->serialize();

18
test/tests/LibNoise.cpp Normal file
View File

@ -0,0 +1,18 @@
//
// Created by aurailus on 22/02/19.
//
#include <catch.hpp>
#include <iostream>
#include <noise/noise.h>
TEST_CASE("Noise Sanity Check", "noise") {
using namespace noise;
module::Perlin myModule;
double value = myModule.GetValue(1.25, 0.75, 0.5);
double diff = fabs(value - 0.686347);
REQUIRE(diff < 0.1);
}

View File

@ -1,27 +0,0 @@
//
// 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
}