dynamic chunk loader issues - dyre nead of refractor

This commit is contained in:
Ben Kyd
2019-11-14 15:20:22 +00:00
parent e690652b03
commit b2ac1f3757
5 changed files with 78 additions and 21 deletions

View File

@@ -1,5 +1,8 @@
#include "world.hpp"
#include <algorithm>
#include <iterator>
#include "chunk/chunk.hpp"
#include "../renderer/shader.hpp"
@@ -24,16 +27,17 @@ void World::LoadWorld() {
m_noiseGenerator = std::make_shared<FastNoise>();
m_noiseGenerator->SetSeed(rand());
m_noiseGenerator->SetNoiseType(FastNoise::Perlin);
m_noiseGenerator->SetNoiseType(FastNoise::ValueFractal);
m_noiseGenerator->SetFractalOctaves(8);
m_noiseGenerator->SetFractalOctaves(5);
for (int x = -4; x < 50; x++)
for (int y = -50; y < 4; y++) {
// Generate a 54x54 chunk world
// for (int x = -4; x < 50; x++)
// for (int y = -50; y < 4; y++) {
m_chunkLoaderQueue.push({ x, y });
// m_chunkLoaderQueue.push({ x, y });
}
// }
// Spawn generator threads
for (int i = 0; i < 6; i++) {
@@ -56,9 +60,17 @@ void World::SetTextureMap(GLuint map) {
}
glm::vec2 World::GetChunkCoords(glm::vec3 wordCoords) {
glm::vec3 World::GetChunkCoords(glm::vec3 worldCoords) {
return { wordCoords.x / CHUNK_WIDTH, wordCoords.z / CHUNK_DEPTH };
return { worldCoords.x / static_cast<float>(CHUNK_WIDTH),
worldCoords.y / static_cast<float>(CHUNK_HEIGHT),
worldCoords.z / static_cast<float>(CHUNK_DEPTH) };
}
glm::vec2 World::GetChunk(glm::vec3 worldCoords) {
return { static_cast<int>(worldCoords.x / CHUNK_WIDTH), static_cast<int>(worldCoords.z / CHUNK_DEPTH) };
}
@@ -71,12 +83,12 @@ std::vector<std::shared_ptr<Chunk>> World::GetRenderableChunks() {
// Should the chunk be rendererd ?
if (chunk.second->ShouldRender) {
m_chunkMutex.lock();
m_chunkLoderMutex.lock();
if (chunk.second->MeshReady)
chunk.second->UploadMesh();
m_chunkMutex.unlock();
m_chunkLoderMutex.unlock();
// If not, add it
chunks.push_back(chunk.second);
@@ -91,6 +103,21 @@ std::vector<std::shared_ptr<Chunk>> World::GetRenderableChunks() {
void World::Update(std::shared_ptr<Entity> player) {
glm::vec2 inChunk = GetChunk(player->Position);
if (m_chunks.find(inChunk) == m_chunks.end()) {
m_chunkLoderMutex.lock();
m_chunkLoaderQueue.push(inChunk);
m_chunkLoderMutex.unlock();
}
std::cout << "Position: " << player->Position.x << ":" << player->Position.y << ":" << player->Position.z << std::endl;
std::cout << "Chunk: " << inChunk.x << ":" << inChunk.y << std::endl << std::endl;
}
void World::Render(std::shared_ptr<Entity> player) {
@@ -118,30 +145,36 @@ World::~World() {
}
for (auto& chunk : m_chunks) {
chunk.second->Unload();
}
}
void World::m_loadChunks() {
while (m_generatorRunning) {
m_chunkMutex.lock();
m_chunkLoderMutex.lock();
glm::vec2 coords = m_chunkLoaderQueue.front();
m_chunkLoaderQueue.pop();
m_chunkMutex.unlock();
m_chunkLoderMutex.unlock();
std::shared_ptr<Chunk> loadingChunk = std::make_shared<Chunk>(coords.x, coords.y, m_noiseGenerator);
loadingChunk->ShouldRender = true;
std::cout << "Loaded chunk " << coords.x << ":" << coords.y << std::endl;
m_chunkMutex.lock();
m_chunkLoderMutex.lock();
m_chunks[coords] = loadingChunk;
m_chunks[coords]->ShouldRender = true;
m_chunkMutex.unlock();
m_chunkLoderMutex.unlock();
while (m_chunkLoaderQueue.empty()) {