1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
5 #include "geometry.hpp"
10 #include <glm/glm.hpp>
15 /// attributes of a type of block
23 constexpr explicit BlockType(
25 const glm::vec3 &color = { 1, 1, 1 })
26 : id(-1), visible(v), color(color) { }
28 static const BlockType DEFAULT;
33 std::vector<glm::vec3> &vertices,
34 std::vector<glm::vec3> &colors,
35 std::vector<glm::vec3> &normals
38 void FillModel(const glm::vec3 &pos, Model &m) const {
39 FillVBO(pos, m.vertices, m.colors, m.normals);
45 class BlockTypeRegistry {
51 int Add(const BlockType &);
53 BlockType *operator [](int id) { return &types[id]; }
54 const BlockType *Get(int id) const { return &types[id]; }
57 std::vector<BlockType> types;
65 const BlockType *type;
67 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
73 /// cube of size 16 (256 tiles, 4096 blocks)
80 Chunk &operator =(Chunk &&);
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(); }
88 static constexpr bool InBounds(const glm::vec3 &pos) {
90 pos.x >= 0 && pos.x < Width() &&
91 pos.y >= 0 && pos.y < Height() &&
92 pos.z >= 0 && pos.z < Depth();
94 static constexpr int ToIndex(const glm::vec3 &pos) {
95 return pos.x + pos.y * Width() + pos.z * Width() * Height();
97 static constexpr bool InBounds(int idx) {
98 return idx >= 0 && idx < Size();
100 static glm::vec3 ToCoords(int idx) {
103 (idx / Width()) % Height(),
104 idx / (Width() * Height())
108 void Invalidate() { dirty = true; }
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)); }
118 int *blkid = nullptr,
119 float *dist = nullptr,
120 glm::vec3 *normal = nullptr) const;
122 void Position(const glm::vec3 &);
123 const glm::vec3 &Position() const { return position; }
124 const glm::mat4 &Transform() const { return transform; }
129 int VertexCount() const;
133 std::vector<Block> blocks;
152 Chunk **chunk = nullptr,
153 int *blkid = nullptr,
154 float *dist = nullptr,
155 glm::vec3 *normal = nullptr);
157 BlockTypeRegistry &BlockTypes() { return blockType; }
158 std::list<Chunk> &LoadedChunks() { return chunks; }
160 Chunk &Next(const Chunk &, const glm::vec3 &dir);
163 Chunk &Generate(const glm::vec3 &);
166 BlockTypeRegistry blockType;
167 std::list<Chunk> chunks;