World generator performance improved

This commit is contained in:
MihailRis
2023-11-15 01:51:37 +03:00
parent 91c06537eb
commit 061b322e39
2 changed files with 64 additions and 40 deletions
+1 -4
View File
@@ -57,12 +57,9 @@ Engine::Engine(const EngineSettings& settings_) {
World* world = new World("world-1", "world/", 42, settings); World* world = new World("world-1", "world/", 42, settings);
Player* player = new Player(playerPosition, 4.0f, camera); Player* player = new Player(playerPosition, 4.0f, camera);
level = world->loadLevel(player, settings); level = world->loadLevel(player, settings);
std::cout << "-- initializing finished" << std::endl;
Audio::initialize(); Audio::initialize();
gui = new GUI(); gui = new GUI();
std::cout << "-- initializing finished" << std::endl;
} }
void Engine::updateTimers() { void Engine::updateTimers() {
+63 -36
View File
@@ -3,14 +3,48 @@
#include "Chunk.h" #include "Chunk.h"
#include <iostream> #include <iostream>
#include <time.h>
#include <stdexcept>
#include <math.h> #include <math.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/noise.hpp> #include <glm/gtc/noise.hpp>
#define FNL_IMPL #define FNL_IMPL
#include "../maths/FastNoiseLite.h" #include "../maths/FastNoiseLite.h"
#include <time.h>
#include "../definitions.h" #include "../definitions.h"
#include "../maths/voxmaths.h"
class Heightmap {
int x, z;
int w, d;
float* heights;
public:
Heightmap(int x, int z, int w, int d)
: x(x), z(z), w(w), d(d) {
heights = new float[w * d];
}
~Heightmap() {
delete[] heights;
}
inline float get(int x, int z) {
x -= this->x;
z -= this->z;
if (x < 0 || z < 0 || x >= w || z >= d) {
throw std::runtime_error("out of heightmap");
}
return heights[z * w + x];
}
inline void set(int x, int z, float value) {
x -= this->x;
z -= this->z;
if (x < 0 || z < 0 || x >= w || z >= d) {
throw std::runtime_error("out of heightmap");
}
heights[z * w + x] = value;
}
};
class PseudoRandom { class PseudoRandom {
unsigned short seed; unsigned short seed;
@@ -42,6 +76,7 @@ public:
float calc_height(fnl_state *noise, int real_x, int real_z){ float calc_height(fnl_state *noise, int real_x, int real_z){
float height = 0; float height = 0;
height += fnlGetNoise3D(noise, real_x*0.0125f*8-125567,real_z*0.0125f*8+3546, 0.0f); height += fnlGetNoise3D(noise, real_x*0.0125f*8-125567,real_z*0.0125f*8+3546, 0.0f);
height += fnlGetNoise3D(noise, real_x*0.025f*8+4647,real_z*0.025f*8-3436, 0.0f)*0.5f; height += fnlGetNoise3D(noise, real_x*0.025f*8+4647,real_z*0.025f*8-3436, 0.0f)*0.5f;
height += fnlGetNoise3D(noise, real_x*0.05f*8-834176,real_z*0.05f*8+23678, 0.0f)*0.25f; height += fnlGetNoise3D(noise, real_x*0.05f*8-834176,real_z*0.05f*8+23678, 0.0f)*0.25f;
@@ -51,45 +86,35 @@ float calc_height(fnl_state *noise, int real_x, int real_z){
0.0f)*0.1f; 0.0f)*0.1f;
height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f; height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f;
height += fnlGetNoise3D(noise, real_x*0.4f*8+4565,real_z*0.4f*8+46456, 0.0f)*0.0625f; height += fnlGetNoise3D(noise, real_x*0.4f*8+4565,real_z*0.4f*8+46456, 0.0f)*0.0625f;
// height += fnlGetNoise3D(noise, real_x*8,real_z*8, 0.0f)*0.03f*(fnlGetNoise3D(noise, -real_x*0.0125f*8-1000,real_z*0.0125f*8+2000, 0.0f)/2+0.5f);
height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f; height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f;
height += 1.0f; height += 1.0f;
height *= 64.0f; height *= 64.0f;
return height; return height;
} }
float calc_height_faster(fnl_state *noise, int real_x, int real_z){ int generate_tree(fnl_state *noise,
float height = 0; PseudoRandom* random,
height += fnlGetNoise3D(noise, real_x*0.0125f*8-125567,real_z*0.0125f*8+3546, 0.0f); Heightmap& heights,
height += fnlGetNoise3D(noise, real_x*0.025f*8+4647,real_z*0.025f*8-3436, 0.0f)*0.5f; int real_x,
height += fnlGetNoise3D(noise, real_x*0.05f*8-834176,real_z*0.05f*8+23678, 0.0f)*0.25f; int real_y,
height += fnlGetNoise3D(noise, int real_z,
real_x*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8-23557,real_z*0.1f*8-6568, 0.0f)*50, int tileSize){
real_z*0.2f*8 + fnlGetNoise3D(noise, real_x*0.1f*8+4363,real_z*0.1f*8+4456, 0.0f)*50, const int tileX = floordiv(real_x, tileSize);
0.0f)*0.1f; const int tileZ = floordiv(real_z, tileSize);
height += fnlGetNoise3D(noise, real_x*0.1f*8-3465,real_z*0.1f*8+4534, 0.0f)*0.125f;
height *= fnlGetNoise3D(noise, real_x*0.0125f*8+1000,real_z*0.0125f*8+1000, 0.0f)/2+0.5f;
height += 1.0f;
height *= 64.0f;
return height;
}
int generate_tree(fnl_state *noise, PseudoRandom* random, float* heights, int real_x, int real_y, int real_z, int tileSize){
const int tileX = floor((double)real_x/(double)tileSize);
const int tileZ = floor((double)real_z/(double)tileSize);
random->setSeed(tileX*4325261+tileZ*12160951+tileSize*9431111); random->setSeed(tileX*4325261+tileZ*12160951+tileSize*9431111);
bool gentree = fnlGetNoise3D(noise, tileX*3.0f+633, 0.0, tileZ*3.0f) > -0.1f && (random->rand() % 10) < 7; bool gentree = fnlGetNoise3D(noise, tileX*3.0f+633, 0.0, tileZ*3.0f) > -0.1f && (random->rand() % 10) < 7;
if (!gentree) if (!gentree)
return 0; return 0;
const int randomX = (random->rand() % (tileSize/2)) - tileSize/4; int randomX = (random->rand() % (tileSize/2)) - tileSize/4;
const int randomZ = (random->rand() % (tileSize/2)) - tileSize/4; int randomZ = (random->rand() % (tileSize/2)) - tileSize/4;
int centerX = tileX * tileSize + tileSize/2 + randomX; int centerX = tileX * tileSize + tileSize/2 + randomX;
int centerZ = tileZ * tileSize + tileSize/2 + randomZ; int centerZ = tileZ * tileSize + tileSize/2 + randomZ;
// int height = (int)(heights[centerX*CHUNK_W+centerZ]); int height = (int)(heights.get(centerX, centerZ));
int height = (int)calc_height_faster(noise, centerX, centerZ); if (height < 57)
if ((height < 57)/* || (fnlGetNoise3D(noise, real_x*0.025f,real_z*0.025f, 0.0f)*0.5f > 0.5)*/)
return 0; return 0;
int lx = real_x - centerX; int lx = real_x - centerX;
int radius = random->rand() % 4 + 2; int radius = random->rand() % 4 + 2;
@@ -103,28 +128,34 @@ int generate_tree(fnl_state *noise, PseudoRandom* random, float* heights, int re
} }
void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
const int treesTile = 12;
fnl_state noise = fnlCreateState(); fnl_state noise = fnlCreateState();
noise.noise_type = FNL_NOISE_OPENSIMPLEX2; noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
noise.seed = seed * 60617077 % 25896307; noise.seed = seed * 60617077 % 25896307;
PseudoRandom randomtree; PseudoRandom randomtree;
PseudoRandom randomgrass; PseudoRandom randomgrass;
float heights[CHUNK_VOL]; int padding = 8;
Heightmap heights(cx * CHUNK_W - padding,
cz * CHUNK_D - padding,
CHUNK_W + padding * 2,
CHUNK_D + padding * 2);
for (int z = 0; z < CHUNK_D; z++){ for (int z = -padding; z < CHUNK_D+padding; z++){
for (int x = 0; x < CHUNK_W; x++){ for (int x = -padding; x < CHUNK_W+padding; x++){
int real_x = x + cx * CHUNK_W; int real_x = x + cx * CHUNK_W;
int real_z = z + cz * CHUNK_D; int real_z = z + cz * CHUNK_D;
float height = calc_height(&noise, real_x, real_z); float height = calc_height(&noise, real_x, real_z);
heights[z*CHUNK_W+x] = height; heights.set(real_x, real_z, height);
} }
} }
for (int z = 0; z < CHUNK_D; z++){ for (int z = 0; z < CHUNK_D; z++){
int real_z = z + cz * CHUNK_D; int real_z = z + cz * CHUNK_D;
for (int x = 0; x < CHUNK_W; x++){ for (int x = 0; x < CHUNK_W; x++){
int real_x = x + cx * CHUNK_W; int real_x = x + cx * CHUNK_W;
float height = heights[z*CHUNK_W+x]; float height = heights.get(real_x, real_z);
for (int y = 0; y < CHUNK_H; y++){ for (int y = 0; y < CHUNK_H; y++){
int real_y = y; int real_y = y;
@@ -137,14 +168,10 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
} else if (real_y < height){ } else if (real_y < height){
id = BLOCK_DIRT; id = BLOCK_DIRT;
} else { } else {
int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 12); int tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, treesTile);
if (tree) { if (tree) {
id = tree; id = tree;
states = BLOCK_DIR_Y; states = BLOCK_DIR_Y;
// } else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 19))){
// id = tree;
// } else if ((tree = generate_tree(&noise, &randomtree, heights, real_x, real_y, real_z, 23))){
// id = tree;
} }
} }
if ( ((height - (1.5 - 0.2 * pow(height - 54, 4))) < real_y) && (real_y < height)){ if ( ((height - (1.5 - 0.2 * pow(height - 54, 4))) < real_y) && (real_y < height)){