From 78bd637581542576372d75cf7638f76381e933b4 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sat, 29 Jul 2017 23:26:45 +0100 Subject: [PATCH] mesh building stufff --- Source/World/Chunk/Chunk.cpp | 15 ++++++++++----- Source/World/Chunk/Chunk.h | 6 ++++-- Source/World/Chunk/ChunkMeshBuilder.cpp | 6 +++--- Source/World/Chunk/ChunkMeshBuilder.h | 6 +++--- Source/World/Chunk/ChunkSection.cpp | 8 +++++++- Source/World/Chunk/ChunkSection.h | 6 +++--- Source/World/World.cpp | 7 ++++--- 7 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Source/World/Chunk/Chunk.cpp b/Source/World/Chunk/Chunk.cpp index f5950466..234f248f 100644 --- a/Source/World/Chunk/Chunk.cpp +++ b/Source/World/Chunk/Chunk.cpp @@ -2,6 +2,7 @@ #include "../../Renderer/RenderMaster.h" #include "ChunkMeshBuilder.h" +#include "../../Util/Random.h" #include @@ -34,13 +35,16 @@ Chunk::Chunk(World& world, const sf::Vector2i& location) } } -void Chunk::makeAllMeshtemp() +bool Chunk::makeMesh() { for (auto& chunk : m_chunks) { - ChunkMeshBuilder builder(chunk); - builder.buildMesh(chunk.m_mesh); - chunk.m_mesh.bufferMesh(); + if (!chunk.hasMesh()) + { + ChunkMeshBuilder(chunk, chunk.m_mesh).buildMesh(); + chunk.m_mesh.bufferMesh(); + chunk.m_hasMesh = true; + } } } @@ -88,7 +92,8 @@ void Chunk::drawChunks(RenderMaster& renderer) { for (auto& chunk : m_chunks) { - renderer.drawChunk(chunk.m_mesh); + if (chunk.hasMesh()) + renderer.drawChunk(chunk.m_mesh); } } diff --git a/Source/World/Chunk/Chunk.h b/Source/World/Chunk/Chunk.h index 330f77b5..a2950694 100644 --- a/Source/World/Chunk/Chunk.h +++ b/Source/World/Chunk/Chunk.h @@ -11,7 +11,7 @@ class Chunk : public IChunk public: Chunk(World& world, const sf::Vector2i& location); - void makeAllMeshtemp(); + bool makeMesh(); void setBlock (int x, int y, int z, ChunkBlock block) override; ChunkBlock getBlock (int x, int y, int z) const override; @@ -19,13 +19,15 @@ class Chunk : public IChunk void drawChunks (RenderMaster& renderer); private: - bool outOfBound(int x, int y, int z) const ; + bool outOfBound(int x, int y, int z) const; std::vector m_chunks; sf::Vector2i m_location; World* m_pWorld; + bool m_isLoaded = false; + }; #endif // CHUNK_H_INCLUDED diff --git a/Source/World/Chunk/ChunkMeshBuilder.cpp b/Source/World/Chunk/ChunkMeshBuilder.cpp index 64bc75a1..9b97c872 100644 --- a/Source/World/Chunk/ChunkMeshBuilder.cpp +++ b/Source/World/Chunk/ChunkMeshBuilder.cpp @@ -62,8 +62,9 @@ namespace } -ChunkMeshBuilder::ChunkMeshBuilder(ChunkSection& chunk) +ChunkMeshBuilder::ChunkMeshBuilder(const ChunkSection& chunk, ChunkMesh& mesh) : m_pChunk (&chunk) +, m_pMesh (&mesh) { } struct AdjacentBlockPositions @@ -87,11 +88,10 @@ struct AdjacentBlockPositions }; int faces; -void ChunkMeshBuilder::buildMesh(ChunkMesh& mesh) +void ChunkMeshBuilder::buildMesh() { sf::Clock c; // std::cout << "Begin mesh build\n"; - m_pMesh = &mesh; AdjacentBlockPositions directions; diff --git a/Source/World/Chunk/ChunkMeshBuilder.h b/Source/World/Chunk/ChunkMeshBuilder.h index b11c62bb..8120a923 100644 --- a/Source/World/Chunk/ChunkMeshBuilder.h +++ b/Source/World/Chunk/ChunkMeshBuilder.h @@ -13,9 +13,9 @@ class BlockDataHolder; class ChunkMeshBuilder { public: - ChunkMeshBuilder(ChunkSection& chunk); + ChunkMeshBuilder(const ChunkSection& chunk, ChunkMesh& mesh); - void buildMesh(ChunkMesh& mesh); + void buildMesh(); private: void tryAddFaceToMesh(const std::vector& blockFace, @@ -26,7 +26,7 @@ class ChunkMeshBuilder bool shouldMakeFace (const sf::Vector3i& blockPosition, const BlockDataHolder& blockData); - ChunkSection* m_pChunk = nullptr; + const ChunkSection* m_pChunk = nullptr; ChunkMesh* m_pMesh = nullptr; const BlockDataHolder* m_pBlockData = nullptr; }; diff --git a/Source/World/Chunk/ChunkSection.cpp b/Source/World/Chunk/ChunkSection.cpp index 737e0f16..e1ff6eba 100644 --- a/Source/World/Chunk/ChunkSection.cpp +++ b/Source/World/Chunk/ChunkSection.cpp @@ -11,6 +11,7 @@ ChunkSection::ChunkSection(const sf::Vector3i& location, World& world) , m_pWorld (&world) { static_assert(sizeof(m_blocks) == CHUNK_VOLUME, "Size too big, yo"); + std::cout << sizeof(m_blocks) << " " << sizeof(*this) << std::endl; } void ChunkSection::setBlock(int x, int y, int z, ChunkBlock block) @@ -40,11 +41,16 @@ ChunkBlock ChunkSection::getBlock(int x, int y, int z) const return m_blocks[getIndex(x, y, z)]; } -const sf::Vector3i ChunkSection::getLocation() const +const sf::Vector3i ChunkSection::getLocation() const noexcept { return m_location; } +bool ChunkSection::hasMesh() const noexcept +{ + return m_hasMesh; +} + sf::Vector3i ChunkSection::toWorldPosition(int x, int y, int z) const { return diff --git a/Source/World/Chunk/ChunkSection.h b/Source/World/Chunk/ChunkSection.h index ae7087a0..80914b52 100644 --- a/Source/World/Chunk/ChunkSection.h +++ b/Source/World/Chunk/ChunkSection.h @@ -20,9 +20,9 @@ class ChunkSection : public IChunk void setBlock (int x, int y, int z, ChunkBlock block) override; ChunkBlock getBlock (int x, int y, int z) const override; - const sf::Vector3i getLocation() const; + const sf::Vector3i getLocation() const noexcept; - ///@TODO make private + bool hasMesh() const noexcept; private: @@ -31,12 +31,12 @@ class ChunkSection : public IChunk static bool outOfBounds (int value); static int getIndex (int x, int y, int z); - std::array m_blocks; ChunkMesh m_mesh; sf::Vector3i m_location; World* m_pWorld; + bool m_hasMesh = false; }; #endif // CHUNKSECTION_H_INCLUDED diff --git a/Source/World/World.cpp b/Source/World/World.cpp index 37634271..cb07f7e8 100644 --- a/Source/World/World.cpp +++ b/Source/World/World.cpp @@ -52,7 +52,9 @@ World::World() } for (auto& chunk : m_chunks) - chunk.makeAllMeshtemp(); + { + chunk.makeMesh(); + } } //world coords into chunk column coords @@ -84,7 +86,6 @@ void World::setBlock(int x, int y, int z, ChunkBlock block) void World::editBlock(int x, int y, int z, ChunkBlock block) { - auto bp = getBlockXZ(x, z); auto cp = getChunkXZ(x, z); if (isOutOfBounds(cp)) @@ -100,7 +101,7 @@ void World::renderWorld(RenderMaster& renderer) { for (auto& chunk : m_changedChunks) { - chunk->makeAllMeshtemp(); + chunk->makeMesh(); } m_changedChunks.clear();