]> git.localhorst.tv Git - blank.git/blob - src/world.hpp
very basic chunk model
[blank.git] / src / world.hpp
1 #ifndef BLANK_WORLD_HPP_
2 #define BLANK_WORLD_HPP_
3
4 #include "model.hpp"
5
6 #include <vector>
7 #include <GL/glew.h>
8 #include <glm/glm.hpp>
9
10
11 namespace blank {
12
13 /// attributes of a type of block
14 struct BlockType {
15
16         bool visible;
17
18         constexpr explicit BlockType(bool v = false)
19         : visible(v) { }
20
21         static const BlockType DEFAULT;
22
23
24         void FillVBO(
25                 const glm::vec3 &pos,
26                 std::vector<glm::vec3> &vertices,
27                 std::vector<glm::vec3> &colors,
28                 std::vector<glm::vec3> &normals
29         ) const;
30
31         void FillModel(const glm::vec3 &pos, Model &m) const {
32                 FillVBO(pos, m.vertices, m.colors, m.normals);
33         }
34
35 };
36
37
38 /// single 1x1x1 cube
39 struct Block {
40
41         const BlockType *type;
42
43         constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
44         : type(t) { }
45
46 };
47
48
49 /// cube of size 16 (256 tiles, 4096 blocks)
50 class Chunk {
51
52 public:
53         Chunk();
54
55         static constexpr int Width() { return 16; }
56         static constexpr int Height() { return 16; }
57         static constexpr int Depth() { return 16; }
58         static constexpr int Size() { return Width() * Height() * Depth(); }
59
60         static constexpr int ToIndex(const glm::vec3 &pos) {
61                 return pos.x + pos.y * Width() + pos.z * Width() * Height();
62         }
63         static glm::vec3 ToCoords(int idx) {
64                 return glm::vec3(
65                         idx % Width(),
66                         (idx / Width()) % Height(),
67                         idx / (Width() * Height())
68                 );
69         }
70
71         void Invalidate() { dirty = true; }
72
73         Block &BlockAt(const glm::vec3 &pos) { return blocks[ToIndex(pos)]; }
74         const Block &BlockAt(const glm::vec3 &pos) const { return blocks[ToIndex(pos)]; }
75
76         void Draw();
77
78 private:
79         int VertexCount() const;
80         void Update();
81
82 private:
83         std::vector<Block> blocks;
84         Model model;
85         bool dirty;
86
87 };
88
89 }
90
91 #endif