Skip to content

Commit

Permalink
mesh building stufff
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Jul 29, 2017
1 parent f33d2d5 commit 78bd637
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
15 changes: 10 additions & 5 deletions Source/World/Chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../../Renderer/RenderMaster.h"
#include "ChunkMeshBuilder.h"
#include "../../Util/Random.h"

#include <iostream>

Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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);
}
}

6 changes: 4 additions & 2 deletions Source/World/Chunk/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ 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;

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<ChunkSection> m_chunks;
sf::Vector2i m_location;

World* m_pWorld;

bool m_isLoaded = false;

};

#endif // CHUNK_H_INCLUDED
6 changes: 3 additions & 3 deletions Source/World/Chunk/ChunkMeshBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ namespace

}

ChunkMeshBuilder::ChunkMeshBuilder(ChunkSection& chunk)
ChunkMeshBuilder::ChunkMeshBuilder(const ChunkSection& chunk, ChunkMesh& mesh)
: m_pChunk (&chunk)
, m_pMesh (&mesh)
{ }

struct AdjacentBlockPositions
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions Source/World/Chunk/ChunkMeshBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GLfloat>& blockFace,
Expand All @@ -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;
};
Expand Down
8 changes: 7 additions & 1 deletion Source/World/Chunk/ChunkSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Source/World/Chunk/ChunkSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -31,12 +31,12 @@ class ChunkSection : public IChunk
static bool outOfBounds (int value);
static int getIndex (int x, int y, int z);


std::array<ChunkBlock, CHUNK_VOLUME> m_blocks;
ChunkMesh m_mesh;
sf::Vector3i m_location;

World* m_pWorld;
bool m_hasMesh = false;
};

#endif // CHUNKSECTION_H_INCLUDED
7 changes: 4 additions & 3 deletions Source/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ World::World()
}

for (auto& chunk : m_chunks)
chunk.makeAllMeshtemp();
{
chunk.makeMesh();
}
}

//world coords into chunk column coords
Expand Down Expand Up @@ -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))
Expand All @@ -100,7 +101,7 @@ void World::renderWorld(RenderMaster& renderer)
{
for (auto& chunk : m_changedChunks)
{
chunk->makeAllMeshtemp();
chunk->makeMesh();
}
m_changedChunks.clear();

Expand Down

0 comments on commit 78bd637

Please sign in to comment.