1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
5 #include "geometry.hpp"
14 /// attributes of a type of block
22 constexpr explicit BlockType(
24 const glm::vec3 &color = { 1, 1, 1 })
25 : id(-1), visible(v), color(color) { }
27 static const BlockType DEFAULT;
32 std::vector<glm::vec3> &vertices,
33 std::vector<glm::vec3> &colors,
34 std::vector<glm::vec3> &normals
37 void FillModel(const glm::vec3 &pos, Model &m) const {
38 FillVBO(pos, m.vertices, m.colors, m.normals);
44 class BlockTypeRegistry {
50 int Add(const BlockType &);
52 BlockType *operator [](int id) { return &types[id]; }
53 const BlockType *Get(int id) const { return &types[id]; }
56 std::vector<BlockType> types;
64 const BlockType *type;
66 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
72 /// cube of size 16 (256 tiles, 4096 blocks)
78 static constexpr int Width() { return 16; }
79 static constexpr int Height() { return 16; }
80 static constexpr int Depth() { return 16; }
81 static constexpr int Size() { return Width() * Height() * Depth(); }
83 static constexpr int ToIndex(const glm::vec3 &pos) {
84 return pos.x + pos.y * Width() + pos.z * Width() * Height();
86 static glm::vec3 ToCoords(int idx) {
89 (idx / Width()) % Height(),
90 idx / (Width() * Height())
94 void Invalidate() { dirty = true; }
96 Block &BlockAt(int index) { return blocks[index]; }
97 const Block &BlockAt(int index) const { return blocks[index]; }
98 Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
99 const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
104 int *blkid = nullptr,
105 float *dist = nullptr,
106 glm::vec3 *normal = nullptr) const;
111 int VertexCount() const;
115 std::vector<Block> blocks;