dynamic chunk loader issues - dyre nead of refractor
This commit is contained in:
@@ -20,8 +20,10 @@ public:
|
|||||||
void MoveCamera(Uint8* state);
|
void MoveCamera(Uint8* state);
|
||||||
// Mouse
|
// Mouse
|
||||||
void HandleMouse(SDL_Event e);
|
void HandleMouse(SDL_Event e);
|
||||||
|
// Mouse Delta
|
||||||
void MouseMoved(glm::vec2 mouseDelta);
|
void MouseMoved(glm::vec2 mouseDelta);
|
||||||
|
|
||||||
|
// Updatable by
|
||||||
float MouseSensitivity = 0.1f;
|
float MouseSensitivity = 0.1f;
|
||||||
float CameraSpeed = 2.0f;
|
float CameraSpeed = 2.0f;
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Chunk::Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator) {
|
|||||||
continue;
|
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);
|
Voxels.push_back((uint8_t)EBlockType::Grass);
|
||||||
continue;
|
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() {
|
void Chunk::UploadMesh() {
|
||||||
|
|
||||||
if (!MeshReady)
|
if (!MeshReady || !Loaded)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
glGenVertexArrays(1, &m_vao);
|
glGenVertexArrays(1, &m_vao);
|
||||||
@@ -187,7 +201,7 @@ void Chunk::UploadMesh() {
|
|||||||
|
|
||||||
void Chunk::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader) {
|
void Chunk::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader) {
|
||||||
|
|
||||||
if (!Loaded)
|
if (!MeshReady || !Loaded)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
shader->Use();
|
shader->Use();
|
||||||
@@ -276,4 +290,6 @@ void Chunk::m_mesh() {
|
|||||||
|
|
||||||
Chunk::~Chunk() {
|
Chunk::~Chunk() {
|
||||||
|
|
||||||
|
Unload();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public:
|
|||||||
Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator);
|
Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator);
|
||||||
|
|
||||||
void Load();
|
void Load();
|
||||||
|
void Unload();
|
||||||
|
|
||||||
void UploadMesh();
|
void UploadMesh();
|
||||||
bool MeshReady = false;
|
bool MeshReady = false;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#include "world.hpp"
|
#include "world.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
#include "chunk/chunk.hpp"
|
#include "chunk/chunk.hpp"
|
||||||
|
|
||||||
#include "../renderer/shader.hpp"
|
#include "../renderer/shader.hpp"
|
||||||
@@ -24,16 +27,17 @@ void World::LoadWorld() {
|
|||||||
m_noiseGenerator = std::make_shared<FastNoise>();
|
m_noiseGenerator = std::make_shared<FastNoise>();
|
||||||
m_noiseGenerator->SetSeed(rand());
|
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++)
|
// Generate a 54x54 chunk world
|
||||||
for (int y = -50; y < 4; y++) {
|
// 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
|
// Spawn generator threads
|
||||||
for (int i = 0; i < 6; i++) {
|
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 ?
|
// Should the chunk be rendererd ?
|
||||||
if (chunk.second->ShouldRender) {
|
if (chunk.second->ShouldRender) {
|
||||||
|
|
||||||
m_chunkMutex.lock();
|
m_chunkLoderMutex.lock();
|
||||||
|
|
||||||
if (chunk.second->MeshReady)
|
if (chunk.second->MeshReady)
|
||||||
chunk.second->UploadMesh();
|
chunk.second->UploadMesh();
|
||||||
|
|
||||||
m_chunkMutex.unlock();
|
m_chunkLoderMutex.unlock();
|
||||||
|
|
||||||
// If not, add it
|
// If not, add it
|
||||||
chunks.push_back(chunk.second);
|
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) {
|
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) {
|
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() {
|
void World::m_loadChunks() {
|
||||||
|
|
||||||
while (m_generatorRunning) {
|
while (m_generatorRunning) {
|
||||||
|
|
||||||
m_chunkMutex.lock();
|
m_chunkLoderMutex.lock();
|
||||||
|
|
||||||
glm::vec2 coords = m_chunkLoaderQueue.front();
|
glm::vec2 coords = m_chunkLoaderQueue.front();
|
||||||
m_chunkLoaderQueue.pop();
|
m_chunkLoaderQueue.pop();
|
||||||
|
|
||||||
m_chunkMutex.unlock();
|
m_chunkLoderMutex.unlock();
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<Chunk> loadingChunk = std::make_shared<Chunk>(coords.x, coords.y, m_noiseGenerator);
|
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;
|
std::cout << "Loaded chunk " << coords.x << ":" << coords.y << std::endl;
|
||||||
|
|
||||||
|
|
||||||
m_chunkMutex.lock();
|
m_chunkLoderMutex.lock();
|
||||||
|
|
||||||
m_chunks[coords] = loadingChunk;
|
m_chunks[coords] = loadingChunk;
|
||||||
m_chunks[coords]->ShouldRender = true;
|
|
||||||
|
|
||||||
m_chunkMutex.unlock();
|
m_chunkLoderMutex.unlock();
|
||||||
|
|
||||||
|
|
||||||
while (m_chunkLoaderQueue.empty()) {
|
while (m_chunkLoaderQueue.empty()) {
|
||||||
|
|||||||
@@ -30,7 +30,11 @@ public:
|
|||||||
void SetTextureMap(GLuint map);
|
void SetTextureMap(GLuint map);
|
||||||
|
|
||||||
// Takes world coordinates and gets a chunks coordinates
|
// 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();
|
std::vector<std::shared_ptr<Chunk>> GetRenderableChunks();
|
||||||
|
|
||||||
@@ -59,10 +63,11 @@ private:
|
|||||||
// Indexed by chunk coorinates
|
// Indexed by chunk coorinates
|
||||||
std::unordered_map<glm::vec2, std::shared_ptr<Chunk>> m_chunks;
|
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_chunkUpdatesQueue;
|
||||||
std::queue<glm::vec2> m_chunkLoaderQueue;
|
|
||||||
|
|
||||||
std::mutex m_chunkMutex;
|
std::mutex m_chunkLoderMutex;
|
||||||
|
std::queue<glm::vec2> m_chunkLoaderQueue;
|
||||||
|
|
||||||
// Generator
|
// Generator
|
||||||
std::shared_ptr<FastNoise> m_noiseGenerator;
|
std::shared_ptr<FastNoise> m_noiseGenerator;
|
||||||
|
|||||||
Reference in New Issue
Block a user