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