1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
4 #include "geometry.hpp"
12 #include <glm/glm.hpp>
17 /// attributes of a type of block
26 glm::vec3 outline_color;
30 const glm::vec3 &color = { 1, 1, 1 },
31 const Shape *shape = &DEFAULT_SHAPE,
32 const glm::vec3 &outline_color = { -1, -1, -1 })
33 : id(-1), visible(v), shape(shape), color(color), outline_color(outline_color) { }
35 static const BlockType DEFAULT;
36 static const NullShape DEFAULT_SHAPE;
41 std::vector<glm::vec3> &vertices,
42 std::vector<glm::vec3> &colors,
43 std::vector<glm::vec3> &normals
46 void FillModel(const glm::vec3 &pos, Model &m) const {
47 FillVBO(pos, m.vertices, m.colors, m.normals);
53 std::vector<glm::vec3> &vertices,
54 std::vector<glm::vec3> &colors
57 void FillOutlineModel(OutlineModel &m) const {
58 FillOutlineVBO(m.vertices, m.colors);
65 class BlockTypeRegistry {
71 int Add(const BlockType &);
73 size_t Size() const { return types.size(); }
75 BlockType *operator [](int id) { return &types[id]; }
76 const BlockType *Get(int id) const { return &types[id]; }
79 std::vector<BlockType> types;
87 const BlockType *type;
89 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
95 /// cube of size 16 (256 tiles, 4096 blocks)
102 Chunk &operator =(Chunk &&);
104 static constexpr int Width() { return 16; }
105 static constexpr int Height() { return 16; }
106 static constexpr int Depth() { return 16; }
107 static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
108 static constexpr int Size() { return Width() * Height() * Depth(); }
110 static constexpr bool InBounds(const glm::vec3 &pos) {
112 pos.x >= 0 && pos.x < Width() &&
113 pos.y >= 0 && pos.y < Height() &&
114 pos.z >= 0 && pos.z < Depth();
116 static constexpr int ToIndex(const glm::vec3 &pos) {
117 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
119 static constexpr bool InBounds(int idx) {
120 return idx >= 0 && idx < Size();
122 static glm::vec3 ToCoords(int idx) {
124 0.5f + idx % Width(),
125 0.5f + (idx / Width()) % Height(),
126 0.5f + idx / (Width() * Height())
130 void Invalidate() { dirty = true; }
132 Block &BlockAt(int index) { return blocks[index]; }
133 const Block &BlockAt(int index) const { return blocks[index]; }
134 Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
135 const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
140 int *blkid = nullptr,
141 float *dist = nullptr,
142 glm::vec3 *normal = nullptr) const;
144 void Position(const glm::vec3 &);
145 const glm::vec3 &Position() const { return position; }
146 const glm::mat4 &Transform() const { return transform; }
151 int VertexCount() const;
155 std::vector<Block> blocks;
174 Chunk **chunk = nullptr,
175 int *blkid = nullptr,
176 float *dist = nullptr,
177 glm::vec3 *normal = nullptr);
179 BlockTypeRegistry &BlockTypes() { return blockType; }
180 std::list<Chunk> &LoadedChunks() { return chunks; }
182 Chunk &Next(const Chunk &, const glm::vec3 &dir);
185 Chunk &Generate(const glm::vec3 &);
188 BlockTypeRegistry blockType;
189 CuboidShape blockShape;
190 StairShape stairShape;
191 CuboidShape slabShape;
193 SimplexNoise blockNoise;
194 SimplexNoise colorNoise;
196 std::list<Chunk> chunks;