summary refs log tree commit diff
path: root/src/World/Chunk.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-31 06:24:34 +0100
committerMel <einebeere@gmail.com>2022-10-31 06:24:34 +0100
commit23b0bc4d1ddc9fad3c32e8257497ddd13ac6a155 (patch)
tree16ac3e0fa2964f026c857daa4ff6f0ca11520d46 /src/World/Chunk.cpp
parent1b2b0069c9b7ad73c6cc6663b020d0fd894f4567 (diff)
downloadmeowcraft-23b0bc4d1ddc9fad3c32e8257497ddd13ac6a155.tar.zst
meowcraft-23b0bc4d1ddc9fad3c32e8257497ddd13ac6a155.zip
Proto biomes and new blocks
Diffstat (limited to 'src/World/Chunk.cpp')
-rw-r--r--src/World/Chunk.cpp59
1 files changed, 37 insertions, 22 deletions
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp
index cd6d4f6..08f08bc 100644
--- a/src/World/Chunk.cpp
+++ b/src/World/Chunk.cpp
@@ -13,9 +13,9 @@ GFX::Mesh Chunk::mesh() {
     std::vector<Vector<2>> tex_coords{};
     std::vector<uint32_t> indices{};
 
-    for (int x = 0; x < CHUNK_WIDTH; x++) {
-        for (int y = 0; y < CHUNK_HEIGHT; y++) {
-            for (int z = 0; z < CHUNK_WIDTH; z++) {
+    for (int x = 0; x < Chunk::Width; x++) {
+        for (int y = 0; y < Chunk::Height; y++) {
+            for (int z = 0; z < Chunk::Width; z++) {
                 auto type = m_blocks[x][y][z].type;
                 if (type == BlockType::Air) {
                     continue;
@@ -83,9 +83,9 @@ bool Chunk::is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side)
     };
 
     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
+        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;
     }
@@ -99,30 +99,45 @@ bool Chunk::is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side)
 }
 
 std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) {
+    uint8_t atlas_width = 2;
+    uint8_t atlas_height = 3;
+
+    float width_step = 1.0f / atlas_width;
+    float height_step = 1.0f / atlas_height;
+
+    auto block_coords = [=](uint8_t x, uint8_t y) {
+        auto t = y * height_step;
+        auto l = x * width_step;
+        auto b = t + height_step;
+        auto r = l + width_step;
+
+        return std::array<Vector<2>, 4>{{
+            {l, b}, {r, b}, {r, t}, {l, t},
+        }};
+    };
+
     switch (type) {
         case BlockType::Dirt:
-            return {{
-                {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f},
-            }};
+            return block_coords(1, 0);
         case BlockType::Grass:
             switch (side) {
                 case BlockSide::Front:
                 case BlockSide::Back:
                 case BlockSide::Left:
                 case BlockSide::Right:
-                    return {{
-                        {0.5f, 1.0f}, {0.0f, 1.0f},  {0.0f, 0.5f}, {0.5f, 0.5f},
-                    }};
+                    return block_coords(0, 1);
                 case BlockSide::Top:
-                    return {{
-                        {0.0f, 0.0f}, {0.5f, 0.0f}, {0.5f, 0.5f}, {0.0f, 0.5f},
-                    }};
+                    return block_coords(0, 0);
                 case BlockSide::Bottom:
-                    return {{
-                        {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f},
-                    }};
+                    return block_coords(1, 0);
             }
-        default:
+        case BlockType::Stone:
+            return block_coords(1, 1);
+        case BlockType::Sand:
+            return block_coords(0, 2);
+        case BlockType::Water:
+            return block_coords(1, 2);
+        case BlockType::Air:
             return {};
     }
 }
@@ -131,9 +146,9 @@ std::array<Vector<3>, 4> Chunk::face_normals(BlockSide side) {
     auto is_side = [=](BlockSide s) -> float { return s == side; };
 
     Vector<3> normal = {
-            is_side(BlockSide::Right) - is_side(BlockSide::Left),
-            is_side(BlockSide::Top) - is_side(BlockSide::Bottom),
-            is_side(BlockSide::Front) - is_side(BlockSide::Back),
+        is_side(BlockSide::Right) - is_side(BlockSide::Left),
+        is_side(BlockSide::Top) - is_side(BlockSide::Bottom),
+        is_side(BlockSide::Front) - is_side(BlockSide::Back),
     };
 
     return {normal, normal, normal, normal};