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