]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
don't add obstructed blocks to meshes
[blank.git] / src / chunk.hpp
1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
3
4 #include "block.hpp"
5 #include "geometry.hpp"
6 #include "model.hpp"
7
8 #include <list>
9 #include <vector>
10 #include <glm/glm.hpp>
11
12
13 namespace blank {
14
15 /// cube of size 16 (256 tiles, 4096 blocks)
16 class Chunk {
17
18 public:
19         using Pos = glm::tvec3<int>;
20
21 public:
22         explicit Chunk(const BlockTypeRegistry &);
23
24         Chunk(Chunk &&);
25         Chunk &operator =(Chunk &&);
26
27         static constexpr int Width() { return 16; }
28         static constexpr int Height() { return 16; }
29         static constexpr int Depth() { return 16; }
30         static Pos Extent() { return { Width(), Height(), Depth() }; }
31         static constexpr int Size() { return Width() * Height() * Depth(); }
32
33         static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
34
35         static constexpr bool InBounds(const glm::vec3 &pos) {
36                 return
37                         pos.x >= 0 && pos.x < Width() &&
38                         pos.y >= 0 && pos.y < Height() &&
39                         pos.z >= 0 && pos.z < Depth();
40         }
41         static constexpr int ToIndex(const glm::vec3 &pos) {
42                 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
43         }
44         static constexpr bool InBounds(int idx) {
45                 return idx >= 0 && idx < Size();
46         }
47         static Block::Pos ToCoords(int idx) {
48                 return Block::Pos(
49                         0.5f + (idx % Width()),
50                         0.5f + ((idx / Width()) % Height()),
51                         0.5f + (idx / (Width() * Height()))
52                 );
53         }
54
55         static constexpr bool IsBorder(int idx) {
56                 return
57                         idx < Width() * Height() ||
58                         (idx / Width()) % Height() == 0 ||
59                         (idx / Width()) % Height() == Height() - 1 ||
60                         (idx / (Width() * Height())) == Depth() - 1;
61         }
62
63         // check if block at given index is completely enclosed (and therefore invisible)
64         bool Obstructed(int idx) const;
65
66         void Allocate();
67         void Invalidate() { dirty = true; }
68
69         Block &BlockAt(int index) { return blocks[index]; }
70         const Block &BlockAt(int index) const { return blocks[index]; }
71         Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
72         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
73
74         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
75
76         bool Intersection(
77                 const Ray &,
78                 const glm::mat4 &M,
79                 int *blkid = nullptr,
80                 float *dist = nullptr,
81                 glm::vec3 *normal = nullptr) const;
82
83         void Position(const Pos &);
84         const Pos &Position() const { return position; }
85         glm::mat4 Transform(const Pos &offset) const;
86
87         void CheckUpdate();
88         void Draw();
89
90 private:
91         void Update();
92
93 private:
94         const BlockTypeRegistry *types;
95         std::vector<Block> blocks;
96         Model model;
97         Pos position;
98         bool dirty;
99
100 };
101
102
103 class Generator;
104
105 class ChunkLoader {
106
107 public:
108         ChunkLoader(const BlockTypeRegistry &, const Generator &);
109
110         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
111
112         std::list<Chunk> &Loaded() { return loaded; }
113
114         Chunk *Loaded(const Chunk::Pos &);
115         bool Queued(const Chunk::Pos &);
116         bool Known(const Chunk::Pos &);
117         Chunk &ForceLoad(const Chunk::Pos &);
118
119         void Rebase(const Chunk::Pos &);
120         void Update();
121
122 private:
123         Chunk::Pos base;
124
125         const BlockTypeRegistry &reg;
126         const Generator &gen;
127
128         std::list<Chunk> loaded;
129         std::list<Chunk::Pos> to_generate;
130         std::list<Chunk> to_free;
131
132         int load_dist;
133         int unload_dist;
134
135 };
136
137 }
138
139 #endif