]> git.localhorst.tv Git - blank.git/blob - src/world.hpp
prepare block models for rotation
[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 NullShape 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         size_t Size() const { return types.size(); }
73
74         BlockType *operator [](int id) { return &types[id]; }
75         const BlockType *Get(int id) const { return &types[id]; }
76
77 private:
78         std::vector<BlockType> types;
79
80 };
81
82
83 /// single 1x1x1 cube
84 struct Block {
85
86         const BlockType *type;
87
88         constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
89         : type(t) { }
90
91 };
92
93
94 /// cube of size 16 (256 tiles, 4096 blocks)
95 class Chunk {
96
97 public:
98         Chunk();
99
100         Chunk(Chunk &&);
101         Chunk &operator =(Chunk &&);
102
103         static constexpr int Width() { return 16; }
104         static constexpr int Height() { return 16; }
105         static constexpr int Depth() { return 16; }
106         static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
107         static constexpr int Size() { return Width() * Height() * Depth(); }
108
109         static constexpr bool InBounds(const glm::vec3 &pos) {
110                 return
111                         pos.x >= 0 && pos.x < Width() &&
112                         pos.y >= 0 && pos.y < Height() &&
113                         pos.z >= 0 && pos.z < Depth();
114         }
115         static constexpr int ToIndex(const glm::vec3 &pos) {
116                 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
117         }
118         static constexpr bool InBounds(int idx) {
119                 return idx >= 0 && idx < Size();
120         }
121         static glm::vec3 ToCoords(int idx) {
122                 return glm::vec3(
123                         0.5f + idx % Width(),
124                         0.5f + (idx / Width()) % Height(),
125                         0.5f + idx / (Width() * Height())
126                 );
127         }
128
129         void Invalidate() { dirty = true; }
130
131         Block &BlockAt(int index) { return blocks[index]; }
132         const Block &BlockAt(int index) const { return blocks[index]; }
133         Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
134         const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
135
136         bool Intersection(
137                 const Ray &,
138                 const glm::mat4 &M,
139                 int *blkid = nullptr,
140                 float *dist = nullptr,
141                 glm::vec3 *normal = nullptr) const;
142
143         void Position(const glm::vec3 &);
144         const glm::vec3 &Position() const { return position; }
145         const glm::mat4 &Transform() const { return transform; }
146
147         void Draw();
148
149 private:
150         int VertexCount() const;
151         void Update();
152
153 private:
154         std::vector<Block> blocks;
155         Model model;
156         glm::vec3 position;
157         glm::mat4 transform;
158         bool dirty;
159
160 };
161
162
163 class World {
164
165 public:
166         World();
167
168         void Generate();
169
170         bool Intersection(
171                 const Ray &,
172                 const glm::mat4 &M,
173                 Chunk **chunk = nullptr,
174                 int *blkid = nullptr,
175                 float *dist = nullptr,
176                 glm::vec3 *normal = nullptr);
177
178         BlockTypeRegistry &BlockTypes() { return blockType; }
179         std::list<Chunk> &LoadedChunks() { return chunks; }
180
181         Chunk &Next(const Chunk &, const glm::vec3 &dir);
182
183 private:
184         Chunk &Generate(const glm::vec3 &);
185
186 private:
187         BlockTypeRegistry blockType;
188         CuboidShape blockShape;
189         StairShape stairShape;
190         CuboidShape slabShape;
191
192         std::list<Chunk> chunks;
193
194 };
195
196 }
197
198 #endif