dynamic chunk loader issues - dyre nead of refractor
This commit is contained in:
@@ -20,8 +20,10 @@ public:
|
||||
void MoveCamera(Uint8* state);
|
||||
// Mouse
|
||||
void HandleMouse(SDL_Event e);
|
||||
// Mouse Delta
|
||||
void MouseMoved(glm::vec2 mouseDelta);
|
||||
|
||||
// Updatable by
|
||||
float MouseSensitivity = 0.1f;
|
||||
float CameraSpeed = 2.0f;
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ Chunk::Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetNoise(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.5f) {
|
||||
if (pow((y / (float)CHUNK_HEIGHT), 1.1024f) + terrainGenerator->GetNoise(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.40f < 0.5f) {
|
||||
|
||||
Voxels.push_back((uint8_t)EBlockType::Grass);
|
||||
continue;
|
||||
@@ -149,9 +149,23 @@ void Chunk::Load() {
|
||||
|
||||
}
|
||||
|
||||
void Chunk::Unload() {
|
||||
|
||||
m_vertices.clear();
|
||||
m_uvs.clear();
|
||||
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glDeleteBuffers(1, &m_vbo);
|
||||
glDeleteVertexArrays(1, &m_vao);
|
||||
|
||||
Loaded = false;
|
||||
|
||||
}
|
||||
|
||||
void Chunk::UploadMesh() {
|
||||
|
||||
if (!MeshReady)
|
||||
if (!MeshReady || !Loaded)
|
||||
return;
|
||||
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
@@ -187,7 +201,7 @@ void Chunk::UploadMesh() {
|
||||
|
||||
void Chunk::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader) {
|
||||
|
||||
if (!Loaded)
|
||||
if (!MeshReady || !Loaded)
|
||||
return;
|
||||
|
||||
shader->Use();
|
||||
@@ -276,4 +290,6 @@ void Chunk::m_mesh() {
|
||||
|
||||
Chunk::~Chunk() {
|
||||
|
||||
Unload();
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public:
|
||||
Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator);
|
||||
|
||||
void Load();
|
||||
void Unload();
|
||||
|
||||
void UploadMesh();
|
||||
bool MeshReady = false;
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -30,7 +30,11 @@ public:
|
||||
void SetTextureMap(GLuint map);
|
||||
|
||||
// Takes world coordinates and gets a chunks coordinates
|
||||
glm::vec2 GetChunkCoords(glm::vec3 wordCoords);
|
||||
glm::vec3 GetChunkCoords(glm::vec3 wordCoords);
|
||||
|
||||
// Takes world coordinates and gets the chunk those coorinates
|
||||
// fall in
|
||||
glm::vec2 GetChunk(glm::vec3 worldCoords);
|
||||
|
||||
std::vector<std::shared_ptr<Chunk>> GetRenderableChunks();
|
||||
|
||||
@@ -59,10 +63,11 @@ private:
|
||||
// Indexed by chunk coorinates
|
||||
std::unordered_map<glm::vec2, std::shared_ptr<Chunk>> m_chunks;
|
||||
|
||||
std::mutex m_chunkUpdaterMutex;
|
||||
std::queue<glm::vec2> m_chunkUpdatesQueue;
|
||||
std::queue<glm::vec2> m_chunkLoaderQueue;
|
||||
|
||||
std::mutex m_chunkMutex;
|
||||
std::mutex m_chunkLoderMutex;
|
||||
std::queue<glm::vec2> m_chunkLoaderQueue;
|
||||
|
||||
// Generator
|
||||
std::shared_ptr<FastNoise> m_noiseGenerator;
|
||||
|
||||
Reference in New Issue
Block a user