]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
use one vao per model
[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 <vector>
9 #include <glm/glm.hpp>
10
11
12 namespace blank {
13
14 /// cube of size 16 (256 tiles, 4096 blocks)
15 class Chunk {
16
17 public:
18         using Pos = glm::tvec3<int>;
19
20 public:
21         explicit Chunk(const BlockTypeRegistry &);
22
23         Chunk(Chunk &&);
24         Chunk &operator =(Chunk &&);
25
26         static constexpr int Width() { return 16; }
27         static constexpr int Height() { return 16; }
28         static constexpr int Depth() { return 16; }
29         static Pos Extent() { return { Width(), Height(), Depth() }; }
30         static constexpr int Size() { return Width() * Height() * Depth(); }
31
32         static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
33
34         static constexpr bool InBounds(const glm::vec3 &pos) {
35                 return
36                         pos.x >= 0 && pos.x < Width() &&
37                         pos.y >= 0 && pos.y < Height() &&
38                         pos.z >= 0 && pos.z < Depth();
39         }
40         static constexpr int ToIndex(const glm::vec3 &pos) {
41                 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
42         }
43         static constexpr bool InBounds(int idx) {
44                 return idx >= 0 && idx < Size();
45         }
46         static Block::Pos ToCoords(int idx) {
47                 return Block::Pos(
48                         0.5f + (idx % Width()),
49                         0.5f + ((idx / Width()) % Height()),
50                         0.5f + (idx / (Width() * Height()))
51                 );
52         }
53
54         void Allocate();
55         void Invalidate() { dirty = true; }
56
57         Block &BlockAt(int index) { return blocks[index]; }
58         const Block &BlockAt(int index) const { return blocks[index]; }
59         Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
60         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
61
62         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
63
64         bool Intersection(
65                 const Ray &,
66                 const glm::mat4 &M,
67                 int *blkid = nullptr,
68                 float *dist = nullptr,
69                 glm::vec3 *normal = nullptr) const;
70
71         void Position(const Pos &);
72         const Pos &Position() const { return position; }
73         glm::mat4 Transform(const Pos &offset) const;
74
75         void CheckUpdate();
76         void Draw();
77
78 private:
79         void Update();
80
81 private:
82         const BlockTypeRegistry *types;
83         std::vector<Block> blocks;
84         Model model;
85         Pos position;
86         bool dirty;
87
88 };
89
90 }
91
92 #endif