diff options
| author | Mel <einebeere@gmail.com> | 2022-10-18 19:09:36 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-18 19:09:36 +0200 |
| commit | 0464a83dfaebaa75d6e2d3b7431e84ebd83fccfd (patch) | |
| tree | ccc0cce73cd94d7990a1f45c3e213ea1e8cd61f8 /src | |
| parent | ebad1227aaafbd962756155732f0ec39085f7d51 (diff) | |
| download | meowcraft-0464a83dfaebaa75d6e2d3b7431e84ebd83fccfd.tar.zst meowcraft-0464a83dfaebaa75d6e2d3b7431e84ebd83fccfd.zip | |
Hide inward-facing block faces
Diffstat (limited to 'src')
| -rw-r--r-- | src/World/Chunk.cpp | 50 | ||||
| -rw-r--r-- | src/World/Chunk.hpp | 6 |
2 files changed, 53 insertions, 3 deletions
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index 73aab3d..1d541f7 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -21,6 +21,10 @@ Mesh Chunk::mesh() { } for (auto side: BlockSide::all()) { + if (!is_face_visible(x, y, z, side)) { + continue; + } + auto side_tex_coords = Chunk::face_tex_coords(type, side); auto side_positions = side.face(); @@ -41,6 +45,49 @@ Mesh Chunk::mesh() { return {positions, tex_coords, indices}; } +bool Chunk::is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side) { + Vector<3, int32_t> offset{}; + switch (side) { + case BlockSide::Front: + offset[2] = 1; + break; + case BlockSide::Back: + offset[2] = -1; + break; + case BlockSide::Top: + offset[1] = 1; + break; + case BlockSide::Bottom: + offset[1] = -1; + break; + case BlockSide::Left: + offset[0] = -1; + break; + case BlockSide::Right: + offset[0] = 1; + break; + } + + Vector<3, uint32_t> neighbor_pos{ + x + offset.x(), y + offset.y(), z + offset.z(), + }; + + if ( + neighbor_pos.x() >= CHUNK_WIDTH || neighbor_pos.x() < 0 || + neighbor_pos.y() >= CHUNK_HEIGHT || neighbor_pos.y() < 0 || + neighbor_pos.z() >= CHUNK_WIDTH || neighbor_pos.z() < 0 + ) { + return true; + } + + auto neighbor = m_blocks[neighbor_pos.x()][neighbor_pos.y()][neighbor_pos.z()]; + if (neighbor.type == BlockType::Air) { + return true; + } + + return false; +} + std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) { switch (type) { case BlockType::Dirt: @@ -67,6 +114,7 @@ std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) } default: return {}; - }} + } +} } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index eafc613..0a5f269 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -5,8 +5,8 @@ #include "../Mesh.hpp" #include "BlockSide.hpp" -#define CHUNK_WIDTH 8 -#define CHUNK_HEIGHT 8 +#define CHUNK_WIDTH 16 +#define CHUNK_HEIGHT 32 namespace MC { @@ -18,6 +18,8 @@ public: Mesh mesh(); private: + bool is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side); + static std::array<Vector<2>, 4> face_tex_coords(BlockType type, BlockSide side); struct BlockData { |
