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