summary refs log tree commit diff
path: root/src/World/Generation/ChunkMeshing.hpp
blob: 75023f5d78ea5be9402b98a6ed56d9075bcb1918 (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
#pragma once

#include "../../GFX/Mesh.hpp"
#include "../Chunk.hpp"

namespace MC::World::Generation::ChunkMeshing {

struct ChunkNeighbors {
    Chunk *north, *east, *south, *west;
    Chunk *north_east, *south_east, *south_west, *north_west;
};

struct ChunkMesh { GFX::Mesh land_mesh, water_mesh; };
ChunkMesh mesh_chunk(Chunk& chunk, const ChunkNeighbors& neighbors);

namespace Detail {

template <typename Decisions>
GFX::Mesh create_mesh(Chunk& chunk, const ChunkNeighbors& neighbors) {
    std::vector<Vector<3, F32>> positions{};
    std::vector<Vector<3, F32>> normals{};
    std::vector<Vector<2, F32>> tex_coords{};
    std::vector<F32> ambient_occlusion_values{};
    std::vector<U32> 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++) {
                auto block = chunk.get(x, y, z);
                if (Decisions::should_ignore_block(block))
                    continue;

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

                    auto side_positions = Decisions::face_positions(side, x, y, z);
                    auto side_normals = Decisions::face_normals(side);
                    auto side_tex_coords = Decisions::face_tex_coords(block.type, side);
                    auto side_ao = Decisions::face_ao_values(chunk, neighbors, x, y, z, side);

                    U32 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());
                    ambient_occlusion_values.insert(ambient_occlusion_values.end(), side_ao.begin(), side_ao.end());
                    indices.insert(indices.end(), {s + 0, s + 1, s + 2, s + 2, s + 3, s + 0 });
                }
            }
        }
    }

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

class DefaultMeshDecisions {
public:
    static std::array<Vector<3, F32>, 4> face_positions(BlockSide side, U32 x, U32 y, U32 z);
    static std::array<Vector<2, F32>, 4> face_tex_coords(BlockType type, BlockSide side);
    static std::array<Vector<3, F32>, 4> face_normals(BlockSide side);
    static std::array<F32, 4> face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side);

    static Chunk::BlockData get_block_wrapping(const Chunk& chunk, const ChunkNeighbors& neighbors, Vector<3, I32> pos);
    static Vector<3, I32> get_face_normal(BlockSide side);

    static Bool is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side);
    static Bool should_ignore_block(Chunk::BlockData block);
};

class WaterMeshDecisions final : public DefaultMeshDecisions {
public:
    static Bool is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side);
    static Bool should_ignore_block(Chunk::BlockData block);
};

}
}