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