1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
4 #include "controller.hpp"
5 #include "geometry.hpp"
14 #include <glm/glm.hpp>
19 /// attributes of a type of block
28 glm::vec3 outline_color;
32 const glm::vec3 &color = { 1, 1, 1 },
33 const Shape *shape = &DEFAULT_SHAPE,
34 const glm::vec3 &outline_color = { -1, -1, -1 })
35 : id(-1), visible(v), shape(shape), color(color), outline_color(outline_color) { }
37 static const BlockType DEFAULT;
38 static const NullShape DEFAULT_SHAPE;
43 std::vector<glm::vec3> &vertices,
44 std::vector<glm::vec3> &colors,
45 std::vector<glm::vec3> &normals
48 void FillModel(const glm::vec3 &pos, Model &m) const {
49 FillVBO(pos, m.vertices, m.colors, m.normals);
55 std::vector<glm::vec3> &vertices,
56 std::vector<glm::vec3> &colors
59 void FillOutlineModel(OutlineModel &m) const {
60 FillOutlineVBO(m.vertices, m.colors);
67 class BlockTypeRegistry {
73 int Add(const BlockType &);
75 size_t Size() const { return types.size(); }
77 BlockType *operator [](int id) { return &types[id]; }
78 const BlockType *Get(int id) const { return &types[id]; }
81 std::vector<BlockType> types;
89 const BlockType *type;
91 constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
97 /// cube of size 16 (256 tiles, 4096 blocks)
104 Chunk &operator =(Chunk &&);
106 static constexpr int Width() { return 16; }
107 static constexpr int Height() { return 16; }
108 static constexpr int Depth() { return 16; }
109 static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
110 static constexpr int Size() { return Width() * Height() * Depth(); }
112 static constexpr bool InBounds(const glm::vec3 &pos) {
114 pos.x >= 0 && pos.x < Width() &&
115 pos.y >= 0 && pos.y < Height() &&
116 pos.z >= 0 && pos.z < Depth();
118 static constexpr int ToIndex(const glm::vec3 &pos) {
119 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
121 static constexpr bool InBounds(int idx) {
122 return idx >= 0 && idx < Size();
124 static glm::vec3 ToCoords(int idx) {
126 0.5f + idx % Width(),
127 0.5f + (idx / Width()) % Height(),
128 0.5f + idx / (Width() * Height())
132 void Invalidate() { dirty = true; }
134 Block &BlockAt(int index) { return blocks[index]; }
135 const Block &BlockAt(int index) const { return blocks[index]; }
136 Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
137 const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
142 int *blkid = nullptr,
143 float *dist = nullptr,
144 glm::vec3 *normal = nullptr) const;
146 void Position(const glm::vec3 &);
147 const glm::vec3 &Position() const { return position; }
148 const glm::mat4 &Transform() const { return transform; }
153 int VertexCount() const;
157 std::vector<Block> blocks;
176 Chunk **chunk = nullptr,
177 int *blkid = nullptr,
178 float *dist = nullptr,
179 glm::vec3 *normal = nullptr);
181 BlockTypeRegistry &BlockTypes() { return blockType; }
182 std::list<Chunk> &LoadedChunks() { return chunks; }
184 FPSController &Controller() { return player; }
186 Chunk &Next(const Chunk &, const glm::vec3 &dir);
190 void Render(DirectionalLighting &);
193 Chunk &Generate(const glm::vec3 &);
196 BlockTypeRegistry blockType;
197 CuboidShape blockShape;
198 StairShape stairShape;
199 CuboidShape slabShape;
201 SimplexNoise blockNoise;
202 SimplexNoise colorNoise;
204 FPSController player;
206 std::list<Chunk> chunks;