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
22 glm::vec3 outline_color;
24 constexpr explicit BlockType(
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) { }
30 static const BlockType DEFAULT;
35 std::vector<glm::vec3> &vertices,
36 std::vector<glm::vec3> &colors,
37 std::vector<glm::vec3> &normals
40 void FillModel(const glm::vec3 &pos, Model &m) const {
41 FillVBO(pos, m.vertices, m.colors, m.normals);
47 std::vector<glm::vec3> &vertices,
48 std::vector<glm::vec3> &colors
51 void FillOutlineModel(OutlineModel &m) const {
52 FillOutlineVBO(m.vertices, m.colors);
59 class BlockTypeRegistry {
65 int Add(const BlockType &);
67 BlockType *operator [](int id) { return &types[id]; }
68 const BlockType *Get(int id) const { return &types[id]; }
71 std::vector<BlockType> types;
79 const BlockType *type;
81 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
87 /// cube of size 16 (256 tiles, 4096 blocks)
94 Chunk &operator =(Chunk &&);
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(); }
102 static constexpr bool InBounds(const glm::vec3 &pos) {
104 pos.x >= 0 && pos.x < Width() &&
105 pos.y >= 0 && pos.y < Height() &&
106 pos.z >= 0 && pos.z < Depth();
108 static constexpr int ToIndex(const glm::vec3 &pos) {
109 return pos.x + pos.y * Width() + pos.z * Width() * Height();
111 static constexpr bool InBounds(int idx) {
112 return idx >= 0 && idx < Size();
114 static glm::vec3 ToCoords(int idx) {
117 (idx / Width()) % Height(),
118 idx / (Width() * Height())
122 void Invalidate() { dirty = true; }
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)); }
132 int *blkid = nullptr,
133 float *dist = nullptr,
134 glm::vec3 *normal = nullptr) const;
136 void Position(const glm::vec3 &);
137 const glm::vec3 &Position() const { return position; }
138 const glm::mat4 &Transform() const { return transform; }
143 int VertexCount() const;
147 std::vector<Block> blocks;
166 Chunk **chunk = nullptr,
167 int *blkid = nullptr,
168 float *dist = nullptr,
169 glm::vec3 *normal = nullptr);
171 BlockTypeRegistry &BlockTypes() { return blockType; }
172 std::list<Chunk> &LoadedChunks() { return chunks; }
174 Chunk &Next(const Chunk &, const glm::vec3 &dir);
177 Chunk &Generate(const glm::vec3 &);
180 BlockTypeRegistry blockType;
181 std::list<Chunk> chunks;