]> git.localhorst.tv Git - blank.git/blob - src/world.hpp
world class for multiple chunks
[blank.git] / src / world.hpp
1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
3
4 #include "model.hpp"
5 #include "geometry.hpp"
6
7 #include <list>
8 #include <vector>
9 #include <GL/glew.h>
10 #include <glm/glm.hpp>
11
12
13 namespace blank {
14
15 /// attributes of a type of block
16 struct BlockType {
17
18         int id;
19
20         bool visible;
21         glm::vec3 color;
22
23         constexpr explicit BlockType(
24                 bool v = false,
25                 const glm::vec3 &color = { 1, 1, 1 })
26         : id(-1), visible(v), color(color) { }
27
28         static const BlockType DEFAULT;
29
30
31         void FillVBO(
32                 const glm::vec3 &pos,
33                 std::vector<glm::vec3> &vertices,
34                 std::vector<glm::vec3> &colors,
35                 std::vector<glm::vec3> &normals
36         ) const;
37
38         void FillModel(const glm::vec3 &pos, Model &m) const {
39                 FillVBO(pos, m.vertices, m.colors, m.normals);
40         }
41
42 };
43
44
45 class BlockTypeRegistry {
46
47 public:
48         BlockTypeRegistry();
49
50 public:
51         int Add(const BlockType &);
52
53         BlockType *operator [](int id) { return &types[id]; }
54         const BlockType *Get(int id) const { return &types[id]; }
55
56 private:
57         std::vector<BlockType> types;
58
59 };
60
61
62 /// single 1x1x1 cube
63 struct Block {
64
65         const BlockType *type;
66
67         constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
68         : type(t) { }
69
70 };
71
72
73 /// cube of size 16 (256 tiles, 4096 blocks)
74 class Chunk {
75
76 public:
77         Chunk();
78
79         Chunk(Chunk &&);
80         Chunk &operator =(Chunk &&);
81
82         static constexpr int Width() { return 16; }
83         static constexpr int Height() { return 16; }
84         static constexpr int Depth() { return 16; }
85         static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
86         static constexpr int Size() { return Width() * Height() * Depth(); }
87
88         static constexpr bool InBounds(const glm::vec3 &pos) {
89                 return
90                         pos.x >= 0 && pos.x < Width() &&
91                         pos.y >= 0 && pos.y < Height() &&
92                         pos.z >= 0 && pos.z < Depth();
93         }
94         static constexpr int ToIndex(const glm::vec3 &pos) {
95                 return pos.x + pos.y * Width() + pos.z * Width() * Height();
96         }
97         static constexpr bool InBounds(int idx) {
98                 return idx >= 0 && idx < Size();
99         }
100         static glm::vec3 ToCoords(int idx) {
101                 return glm::vec3(
102                         idx % Width(),
103                         (idx / Width()) % Height(),
104                         idx / (Width() * Height())
105                 );
106         }
107
108         void Invalidate() { dirty = true; }
109
110         Block &BlockAt(int index) { return blocks[index]; }
111         const Block &BlockAt(int index) const { return blocks[index]; }
112         Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
113         const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
114
115         bool Intersection(
116                 const Ray &,
117                 const glm::mat4 &M,
118                 int *blkid = nullptr,
119                 float *dist = nullptr,
120                 glm::vec3 *normal = nullptr) const;
121
122         void Position(const glm::vec3 &);
123         const glm::vec3 &Position() const { return position; }
124         const glm::mat4 &Transform() const { return transform; }
125
126         void Draw();
127
128 private:
129         int VertexCount() const;
130         void Update();
131
132 private:
133         std::vector<Block> blocks;
134         Model model;
135         glm::vec3 position;
136         glm::mat4 transform;
137         bool dirty;
138
139 };
140
141
142 class World {
143
144 public:
145         World();
146
147         void Generate();
148
149         bool Intersection(
150                 const Ray &,
151                 const glm::mat4 &M,
152                 Chunk **chunk = nullptr,
153                 int *blkid = nullptr,
154                 float *dist = nullptr,
155                 glm::vec3 *normal = nullptr);
156
157         BlockTypeRegistry &BlockTypes() { return blockType; }
158         std::list<Chunk> &LoadedChunks() { return chunks; }
159
160         Chunk &Next(const Chunk &, const glm::vec3 &dir);
161
162 private:
163         Chunk &Generate(const glm::vec3 &);
164
165 private:
166         BlockTypeRegistry blockType;
167         std::list<Chunk> chunks;
168
169 };
170
171 }
172
173 #endif