1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
4 #include "geometry.hpp"
11 #include <glm/glm.hpp>
16 /// attributes of a type of block
25 glm::vec3 outline_color;
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) { }
34 static const BlockType DEFAULT;
35 static const NullShape DEFAULT_SHAPE;
40 std::vector<glm::vec3> &vertices,
41 std::vector<glm::vec3> &colors,
42 std::vector<glm::vec3> &normals
45 void FillModel(const glm::vec3 &pos, Model &m) const {
46 FillVBO(pos, m.vertices, m.colors, m.normals);
52 std::vector<glm::vec3> &vertices,
53 std::vector<glm::vec3> &colors
56 void FillOutlineModel(OutlineModel &m) const {
57 FillOutlineVBO(m.vertices, m.colors);
64 class BlockTypeRegistry {
70 int Add(const BlockType &);
72 size_t Size() const { return types.size(); }
74 BlockType *operator [](int id) { return &types[id]; }
75 const BlockType *Get(int id) const { return &types[id]; }
78 std::vector<BlockType> types;
86 const BlockType *type;
88 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
94 /// cube of size 16 (256 tiles, 4096 blocks)
101 Chunk &operator =(Chunk &&);
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(); }
109 static constexpr bool InBounds(const glm::vec3 &pos) {
111 pos.x >= 0 && pos.x < Width() &&
112 pos.y >= 0 && pos.y < Height() &&
113 pos.z >= 0 && pos.z < Depth();
115 static constexpr int ToIndex(const glm::vec3 &pos) {
116 return pos.x + pos.y * Width() + pos.z * Width() * Height();
118 static constexpr bool InBounds(int idx) {
119 return idx >= 0 && idx < Size();
121 static glm::vec3 ToCoords(int idx) {
124 (idx / Width()) % Height(),
125 idx / (Width() * Height())
129 void Invalidate() { dirty = true; }
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)); }
139 int *blkid = nullptr,
140 float *dist = nullptr,
141 glm::vec3 *normal = nullptr) const;
143 void Position(const glm::vec3 &);
144 const glm::vec3 &Position() const { return position; }
145 const glm::mat4 &Transform() const { return transform; }
150 int VertexCount() const;
154 std::vector<Block> blocks;
173 Chunk **chunk = nullptr,
174 int *blkid = nullptr,
175 float *dist = nullptr,
176 glm::vec3 *normal = nullptr);
178 BlockTypeRegistry &BlockTypes() { return blockType; }
179 std::list<Chunk> &LoadedChunks() { return chunks; }
181 Chunk &Next(const Chunk &, const glm::vec3 &dir);
184 Chunk &Generate(const glm::vec3 &);
187 BlockTypeRegistry blockType;
188 CuboidShape blockShape;
189 StairShape stairShape;
190 CuboidShape slabShape;
192 std::list<Chunk> chunks;