Replace Perlin noise with Simplex Noise, Optimize NoiseSampler class.

master
aurailus 2019-03-19 23:53:24 -07:00
parent 4a195a96cb
commit 5d87863cb8
26 changed files with 82 additions and 70 deletions

View File

@ -16,6 +16,7 @@ include_directories (
lib/sol
lib/gzip-hpp-master/include
lib/catch
lib/simplex
)
#Include all of the dynamic libraries

View File

@ -14,13 +14,14 @@ out vec2 fragTex;
void main() {
gl_Position = projection * view * model * vec4(pos, 1.0);
// color = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);
vec4 myColor = vec4(1, 1, 1, 0) * (0.8 + abs(nor.x) * 0.15);
myColor += nor.y * 0.15;
myColor += 0.2;
myColor.a = 1;
float factor = min(gl_Position.length / 320, 1);
color = myColor;
fragTex = tex;
}

View File

@ -30,8 +30,8 @@ set(ZEUS_SRC_FILES
generic/blocks/BlockModel.h
generic/blocks/TextureAtlas.cpp
generic/blocks/TextureAtlas.h
client/game/gameworld/LocalWorld.cpp
client/game/gameworld/LocalWorld.h
client/game/localworld/LocalWorld.cpp
client/game/localworld/LocalWorld.h
generic/blocks/BlockChunk.cpp
generic/blocks/BlockChunk.h
generic/helpers/ArrayTrans3D.h
@ -52,8 +52,8 @@ set(ZEUS_SRC_FILES
client/engine/graphics/HudText.h
client/graphics/gui/DebugGui.cpp
client/graphics/gui/DebugGui.h
client/game/gameworld/Player.cpp
client/game/gameworld/Player.h
client/game/Player.cpp
client/game/Player.h
client/engine/Ray.cpp
client/engine/Ray.h
client/lua/l_register_blockmodel.cpp
@ -76,8 +76,8 @@ set(ZEUS_SRC_FILES
server/ServerPlayer.h
client/network/ServerConnection.cpp
client/network/ServerConnection.h
client/game/gameworld/WorldThreadDefs.cpp
client/game/gameworld/WorldThreadDefs.h
client/game/localworld/WorldThreadDefs.cpp
client/game/localworld/WorldThreadDefs.h
generic/gen/MapGen.cpp
generic/gen/MapGen.h
generic/network/NetHandler.cpp
@ -92,8 +92,8 @@ set(ZEUS_SRC_FILES
server/ConnectionList.h
server/ServerPeer.h
generic/network/PacketChannel.h
client/game/gameworld/PlayerEntity.cpp
client/game/gameworld/PlayerEntity.h
client/game/entity/PlayerEntity.cpp
client/game/entity/PlayerEntity.h
client/engine/graphics/Histogram.cpp
client/engine/graphics/Histogram.h
client/engine/graphics/RectEntity.cpp
@ -110,6 +110,12 @@ set(ZEUS_SRC_FILES
client/engine/FrustumPlane.cpp
client/engine/FrustumPlane.h
client/engine/FrustumAABB.cpp
client/engine/FrustumAABB.h server/world/World.cpp server/world/World.h server/world/WorldGenStream.cpp server/world/WorldGenStream.h)
client/engine/FrustumAABB.h
server/world/World.cpp
server/world/World.h
server/world/WorldGenStream.cpp
server/world/WorldGenStream.h
client/game/localworld/WorldInterpolationStream.cpp
client/game/localworld/WorldInterpolationStream.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -9,7 +9,7 @@
#include <cmath>
#include <glm.hpp>
#include "../game/gameworld/Player.h"
#include "../game/Player.h"
class Ray {
public:

View File

@ -30,16 +30,6 @@ GameScene::GameScene(ClientState* state) :
//The scene requires the blockAtlas for meshing and handling inputs.
world = new LocalWorld(blockAtlas);
// int SIZE = 16;
// int SIZEV = 8;
// for (int i = -SIZE; i < SIZE; i++) {
// for (int j = -SIZE; j < SIZEV; j++) {
// for (int k = -SIZE; k < SIZE; k++) {
// world->genNewChunk(glm::vec3(i, j, k));
// }
// }
// }
player = new Player();
player->create(world, state->renderer->getCamera());

View File

@ -13,14 +13,14 @@
#include "../lua/LuaParser.h"
#include "gameworld/LocalWorld.h"
#include "gameworld/Player.h"
#include "localworld/LocalWorld.h"
#include "Player.h"
#include "../network/ServerConnection.h"
#include "../../generic/blocks/TextureAtlas.h"
#include "../../generic/blocks/BlockAtlas.h"
#include "gameworld/PlayerEntity.h"
#include "entity/PlayerEntity.h"
class GameScene : public Scene {
public:
@ -38,7 +38,7 @@ public:
TextureAtlas* textureAtlas;
BlockAtlas* blockAtlas;
//Entities to be drawn with gameworld shaders
//Entities to be drawn with entity shaders
std::vector<Entity*> entities;
std::vector<PlayerEntity*> playerEntities;

View File

@ -8,9 +8,9 @@
#include <iostream>
#include "LocalWorld.h"
#include "../../engine/Camera.h"
#include "../../engine/Timer.h"
#include "localworld/LocalWorld.h"
#include "../engine/Camera.h"
#include "../engine/Timer.h"
class Player {
public:

View File

@ -5,7 +5,6 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#include <syscall.h>
#include "LocalWorld.h"
LocalWorld::LocalWorld(BlockAtlas *atlas) {

View File

@ -12,8 +12,8 @@
#include "../engine/Timer.h"
#include "../../generic/network/Packet.h"
#include "../../generic/network/NetHandler.h"
#include "../game/gameworld/Player.h"
#include "../game/gameworld/PlayerEntity.h"
#include "../game/Player.h"
#include "../game/entity/PlayerEntity.h"
class ServerConnection {
public:

View File

@ -3,6 +3,7 @@
//
#include "MapGen.h"
#include "../../client/engine/Timer.h"
MapGen::MapGen(unsigned int seed) : sampler(seed) {
@ -30,7 +31,7 @@ MapGen::MapGen(unsigned int seed) : sampler(seed) {
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 200
.NOISE_MULTIPLIER = 100
};
p_elevation_variation = NoiseParams {
@ -39,7 +40,7 @@ MapGen::MapGen(unsigned int seed) : sampler(seed) {
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 1,
.SAMPLE_V_PRECISION = 1,
.NOISE_MULTIPLIER = 100
.NOISE_MULTIPLIER = 50
};
p_elevation_variation_smaller = NoiseParams {
@ -48,16 +49,20 @@ MapGen::MapGen(unsigned int seed) : sampler(seed) {
.NOISE_V_FACTOR = 0,
.SAMPLE_H_PRECISION = 2,
.SAMPLE_V_PRECISION = 2,
.NOISE_MULTIPLIER = 50
.NOISE_MULTIPLIER = 25
};
}
BlockChunk* MapGen::generate(glm::vec3 pos) {
Timer t("Chunk Gen");
MapGenJob j(pos);
buildElevation(j);
fillChunk(j);
t.printElapsedMs();
return new BlockChunk(j.blocks, pos);
}
@ -74,10 +79,10 @@ void MapGen::buildElevation(MapGenJob &j) {
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) * 30 * (feature_scale_sample.get(lp) + 0.5f)
- ((j.pos.y * 16 + lp.y));
+ elevation_variation_sample.get(lp)
+ elevation_variation_smaller_sample.get(lp)
+ ((float)pow(feature_sample.get(lp) + 0.5, 2.0) - 0.5f) * 15 * (feature_scale_sample.get(lp) + 0.5f)
- ((j.pos.y * 16 + lp.y));
}
}
@ -86,6 +91,6 @@ void MapGen::fillChunk(MapGenJob &j) {
for (int m = 0; m < 4096; m++) {
ArrayTrans3D::indAssignVec(m, lp);
j.blocks[m] = j.density[m] > 0 ? 3 : 0;
j.blocks[m] = j.density[m] > 0 ? 1 : 0;
}
}

View File

@ -8,20 +8,29 @@
class Interpolation {
public:
static float lerp(float p0, float p1, float factor) {
static inline 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 inline float bilerp(float x0z0, float x1z0, float x0z1, float x1z1, float xfactor, float zfactor) {
auto p0 = (x0z0 + xfactor * (x1z0 - x0z0));
auto p1 = (x0z1 + xfactor * (x1z1 - x0z1));
return p0 + zfactor * (p1 - p0);
}
static float trilerp(float p000, float p100, float p001, float p101,
static inline 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);
auto p00 = (p000 + xfactor * (p100 - p000));
auto p10 = (p001 + xfactor * (p101 - p001));
auto p01 = (p010 + xfactor * (p110 - p010));
auto p11 = (p011 + xfactor * (p111 - p011));
auto bl = (p00 + zfactor * (p10 - p00));
auto tl = (p01 + zfactor * (p11 - p01));
return bl + yfactor * (tl - bl);
}
};

View File

@ -34,22 +34,21 @@ void NoiseSample::set(glm::vec3 pos, float value) {
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 NoiseSample::get(glm::vec3& pos) {
int xInt = (int)pos.x;
int yInt = (int)pos.y;
int zInt = (int)pos.z;
float offsetH = 16.0f / hPrecision;
float offsetV = 16.0f / vPrecision;
int offsetH = (int)(16.0f / hPrecision);
int offsetV = (int)(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);
auto xBase = xInt / offsetH;
auto yBase = yInt / offsetV;
auto zBase = zInt / 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;
float xFac = (xInt % offsetH) / (16.0f / hPrecision);
float yFac = (yInt % offsetV) / (16.0f / vPrecision);
float zFac = (zInt % offsetH) / (16.0f / hPrecision);
auto p000 = data[xBase][yBase][zBase];
auto p100 = data[xBase + 1][yBase][zBase];

View File

@ -14,7 +14,7 @@ public:
NoiseSample(int hPrecision, int vPrecision);
void set(glm::vec3 pos, float value);
float get(glm::vec3 pos);
float get(glm::vec3& pos);
private:
std::vector<std::vector<std::vector<float>>> data;

View File

@ -4,11 +4,13 @@
#include "NoiseSampler.h"
NoiseSampler::NoiseSampler(unsigned int seed) : noise(seed) {
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);
@ -23,8 +25,7 @@ NoiseSample NoiseSampler::sample(glm::vec3 pos, NoiseParams &params) {
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);
s.set(glm::vec3(i, j, k), (Simplex::noise(glm::vec3(xCoord, yCoord, zCoord)) * 0.5f) * params.NOISE_MULTIPLIER);
}
}
}

View File

@ -7,6 +7,7 @@
#include "../helpers/PerlinNoise.h"
#include "../helpers/Simplex.h"
#include "NoiseSample.h"
#include "NoiseParams.h"
@ -17,7 +18,6 @@ public:
NoiseSample sample(glm::vec3 pos, NoiseParams &params);
private:
PerlinNoise noise;
unsigned int seed;
};

View File

@ -29,7 +29,7 @@ public:
private:
bool alive = true;
World world;
World world {55};
NetHandler handler;
ConnectionList connections;

View File

@ -23,8 +23,8 @@ void World::addPlayer(ServerPlayer *player) {
}
std::sort(toGenerate.begin(), toGenerate.end(), [&](glm::vec3 a, glm::vec3 b) {
return min(min(abs(a.x - pos.x), abs(a.y - pos.y)), abs(a.z - pos.z)) <
min(min(abs(b.x - pos.x), abs(b.y - pos.y)), abs(b.z - pos.z));
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));
});
for (glm::vec3 tPos : toGenerate) {

View File

@ -14,7 +14,7 @@
class World {
public:
World() = default;
World(unsigned int seed) : genStream(seed) {};
void addPlayer(ServerPlayer* player);
void update();

View File

@ -7,7 +7,7 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
WorldGenStream::WorldGenStream() : gen(0) {
WorldGenStream::WorldGenStream(unsigned int seed) : gen(seed) {
queuedTasks.reserve((unsigned long) TOTAL_QUEUE_SIZE);
threads.reserve(THREADS);

View File

@ -17,10 +17,11 @@
class WorldGenStream {
public:
static const int THREAD_QUEUE_SIZE = 32;
static const int THREADS = 8;
static const int THREADS = 4;
static const int TOTAL_QUEUE_SIZE = THREADS * THREAD_QUEUE_SIZE;
WorldGenStream();
WorldGenStream() : gen(0) {};
explicit WorldGenStream(unsigned int seed);
~WorldGenStream();
//Attempt to add `pos` to the pre-thread queue.