summary refs log tree commit diff
path: root/src/World/Chunk.cpp
blob: 9642fa93974489540e9f220df0ab34142210edf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Chunk.hpp"
#include "BlockSide.hpp"

namespace MC::World {

void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockData data) {
    m_blocks.at(pos(x, y, z)) = data;
}

Chunk::BlockData Chunk::get(uint32_t x, uint32_t y, uint32_t z) const {
    return m_blocks.at(pos(x, y, z));
}

GFX::Mesh Chunk::mesh() {
    std::vector<Vector<3>> positions{};
    std::vector<Vector<3>> normals{};
    std::vector<Vector<2>> tex_coords{};
    std::vector<uint32_t> indices{};

    for (int x = 0; x < Width; x++) {
        for (int y = 0; y < Height; y++) {
            for (int z = 0; z < Width; z++) {
                auto type = get(x, y, z).type;
                if (type == BlockType::Air) {
                    continue;
                }

                for (auto side: BlockSide::all()) {
                    if (!is_face_visible(x, y, z, side)) {
                        continue;
                    }

                    auto side_positions = side.face();
                    auto side_normals = face_normals(side);
                    auto side_tex_coords = face_tex_coords(type, side);

                    for (auto& position : side_positions) {
                        position = position + Vector<3>{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)};
                    }

                    uint32_t s = positions.size();

                    positions.insert(positions.end(), side_positions.begin(), side_positions.end());
                    normals.insert(normals.end(), side_normals.begin(), side_normals.end());
                    tex_coords.insert(tex_coords.end(), side_tex_coords.begin(), side_tex_coords.end());
                    indices.insert(indices.end(), {s, s + 1, s + 3, s + 1, s + 2, s + 3});
                }
            }
        }
    }

    return {
        {positions, normals, tex_coords},
        indices,
    };
}

Vector<3> Chunk::position() {
    return m_position;
}

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() >= Width || neighbor_pos.x() < 0 ||
        neighbor_pos.y() >= Height || neighbor_pos.y() < 0 ||
        neighbor_pos.z() >= Width || neighbor_pos.z() < 0
    ) {
        return true;
    }

    auto neighbor = get(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) {
    uint8_t atlas_width = 4;
    uint8_t atlas_height = 4;

    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 block_coords(1, 0);
        case BlockType::Grass:
            switch (side) {
            case BlockSide::Front:
            case BlockSide::Back:
            case BlockSide::Left:
            case BlockSide::Right:
                return block_coords(2, 0);
            case BlockSide::Bottom:
                return block_coords(1, 0);
            case BlockSide::Top:
                return block_coords(0, 0);
            }
        case BlockType::Stone:
            return block_coords(3, 0);
        case BlockType::Sand:
            return block_coords(0, 1);
        case BlockType::Water:
            return block_coords(1, 1);
        case BlockType::Snow:
            switch (side) {
            case BlockSide::Front:
            case BlockSide::Back:
            case BlockSide::Left:
            case BlockSide::Right:
                return block_coords(3, 1);
            case BlockSide::Bottom:
                return block_coords(1, 0);
            case BlockSide::Top:
                return block_coords(2, 1);
            }
        case BlockType::Air:
            return {};
    }
}

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),
    };

    return {normal, normal, normal, normal};
}

uint64_t Chunk::pos(uint32_t x, uint32_t y, uint32_t z) {
    return x + Width * y + Width * Height * z;
}

}