Browse Source

stopped chunk geometry rebuilding from spawning huge amounts of threads

testing
Michael Porter 2 years ago
parent
commit
fea5bf7854
4 changed files with 36 additions and 7 deletions
  1. 1
    0
      include/world.h
  2. BIN
      res/test.png
  3. 4
    4
      src/scenes/testscene.cpp
  4. 31
    3
      src/world.cpp

+ 1
- 0
include/world.h View File

@@ -67,6 +67,7 @@ public:
TerrainGen terrain;
VoxelInfo voxelInfo;
VoxelMath voxelMath;
bool rebuildThreadActive;
};

}

BIN
res/test.png View File


+ 4
- 4
src/scenes/testscene.cpp View File

@@ -40,7 +40,7 @@ void TestScene::init() {
tiles.buildTexture();
tiles.updateTextureAt(0, "res/stone.png");
tiles.updateTextureAt(1, "res/dirt.png");
tiles.updateTextureAt(2, "res/grass.png");
tiles.updateTextureAt(2, "res/test.png");

//define vertices
float vertices[] {
@@ -125,9 +125,9 @@ void TestScene::init() {
std::cout << std::endl;

int chunkCount = 1;
for (int i = 0; i < 8; i++) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
for (int k = 0; k < 32; k++) {
std::cout << "\rGenerating chunks (" << chunkCount << "/" << 8*8*8 << ")" << std::flush;
world.generateChunk(i,j,k);
chunkCount++;
@@ -135,7 +135,7 @@ void TestScene::init() {
}
}

world.forceGlobalGeometryUpdate();
//world.forceGlobalGeometryUpdate();

voxelType = 1;
}

+ 31
- 3
src/world.cpp View File

@@ -24,6 +24,7 @@

#include <GL/glew.h>
#include <GL/gl.h>

#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
@@ -36,6 +37,7 @@ World::World() {
voxelSize = 1.0f;
voxelInfo.linkedWorld = this;
voxelMath.linkedWorld = this;
rebuildThreadActive = false;
}

bool World::isVoxelSolid(const int& x, const int& y, const int& z) {
@@ -111,6 +113,17 @@ bool World::generateChunk(const int& x, const int& y, const int& z) {
bool chunkMade = makeChunk(x,y,z);
if (chunkMade) {
terrain.generateChunk(chunks[std::make_tuple(x,y,z)]);
//queue this chunk for geometry update
queueChunkUpdate(x,y,z);

//queue the neighboring 6 chunks. the queue functions throws them out
// if they don't exist
queueChunkUpdate(x+1,y,z);
queueChunkUpdate(x-1,y,z);
queueChunkUpdate(x,y+1,z);
queueChunkUpdate(x,y-1,z);
queueChunkUpdate(x,y,z+1);
queueChunkUpdate(x,y,z-1);
}
return chunkMade;
}
@@ -158,12 +171,28 @@ void World::draw() {
}

void World::update() {
while (!chunkUpdateQueue.empty()) {
if (!rebuildThreadActive && !chunkUpdateQueue.empty( )) {
auto updatefunc = [&]() {
while (!chunkUpdateQueue.empty()) {
auto& pos = chunkUpdateQueue.back();
auto posVec = glm::ivec3(std::get<0>(pos), std::get<1>(pos), std::get<2>(pos));
auto& mesh = mChunkMeshes.find(posVec)->second;
chunkUpdateQueue.pop_back();
mesh.rebuildChunkGeometry();
mChunkGeometryUpdateQueue.push_back(posVec);
}
rebuildThreadActive = false;
};
rebuildThreadActive = true;
std::thread(updatefunc).detach();

}
/*
while (!chunkUpdateQueue.empty()) {
auto chunk = chunks[chunkUpdateQueue.back()];
chunkUpdateQueue.pop_back();
std::thread(&ChunkRenderer::updateGeometry, &chunk->renderer).detach();
*/
auto& pos = chunkUpdateQueue.back();
auto posVec = glm::ivec3(std::get<0>(pos), std::get<1>(pos), std::get<2>(pos));
auto& mesh = mChunkMeshes.find(posVec)->second;
@@ -173,7 +202,6 @@ void World::update() {
mChunkGeometryUpdateQueue.push_back(posVec);
}

/*
for (auto& i : chunks) {
i.second->renderer.updateVertexData();
}

Loading…
Cancel
Save