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 CuboidShape 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 BlockType *operator [](int id) { return &types[id]; }
73 const BlockType *Get(int id) const { return &types[id]; }
76 std::vector<BlockType> types;
84 const BlockType *type;
86 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
92 /// cube of size 16 (256 tiles, 4096 blocks)
99 Chunk &operator =(Chunk &&);
101 static constexpr int Width() { return 16; }
102 static constexpr int Height() { return 16; }
103 static constexpr int Depth() { return 16; }
104 static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
105 static constexpr int Size() { return Width() * Height() * Depth(); }
107 static constexpr bool InBounds(const glm::vec3 &pos) {
109 pos.x >= 0 && pos.x < Width() &&
110 pos.y >= 0 && pos.y < Height() &&
111 pos.z >= 0 && pos.z < Depth();
113 static constexpr int ToIndex(const glm::vec3 &pos) {
114 return pos.x + pos.y * Width() + pos.z * Width() * Height();
116 static constexpr bool InBounds(int idx) {
117 return idx >= 0 && idx < Size();
119 static glm::vec3 ToCoords(int idx) {
122 (idx / Width()) % Height(),
123 idx / (Width() * Height())
127 void Invalidate() { dirty = true; }
129 Block &BlockAt(int index) { return blocks[index]; }
130 const Block &BlockAt(int index) const { return blocks[index]; }
131 Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
132 const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
137 int *blkid = nullptr,
138 float *dist = nullptr,
139 glm::vec3 *normal = nullptr) const;
141 void Position(const glm::vec3 &);
142 const glm::vec3 &Position() const { return position; }
143 const glm::mat4 &Transform() const { return transform; }
148 int VertexCount() const;
152 std::vector<Block> blocks;
171 Chunk **chunk = nullptr,
172 int *blkid = nullptr,
173 float *dist = nullptr,
174 glm::vec3 *normal = nullptr);
176 BlockTypeRegistry &BlockTypes() { return blockType; }
177 std::list<Chunk> &LoadedChunks() { return chunks; }
179 Chunk &Next(const Chunk &, const glm::vec3 &dir);
182 Chunk &Generate(const glm::vec3 &);
185 BlockTypeRegistry blockType;
186 CuboidShape blockShape;
187 CuboidShape slabShape;
189 std::list<Chunk> chunks;