]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
update light levels from border on neighbor change
[blank.git] / src / block.hpp
1 #ifndef BLANK_BLOCK_HPP_
2 #define BLANK_BLOCK_HPP_
3
4 #include "geometry.hpp"
5 #include "model.hpp"
6 #include "shape.hpp"
7
8 #include <vector>
9 #include <glm/glm.hpp>
10
11
12 namespace blank {
13
14 /// single 1x1x1 cube
15 struct Block {
16
17         using Type = unsigned short;
18         using Pos = glm::vec3;
19
20         enum Face {
21                 FACE_UP,
22                 FACE_DOWN,
23                 FACE_RIGHT,
24                 FACE_LEFT,
25                 FACE_FRONT,
26                 FACE_BACK,
27                 FACE_COUNT,
28         };
29         enum Turn {
30                 TURN_NONE,
31                 TURN_LEFT,
32                 TURN_AROUND,
33                 TURN_RIGHT,
34                 TURN_COUNT,
35         };
36
37         Type type;
38         unsigned char orient;
39
40         constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE)
41         : type(type), orient(face * TURN_COUNT + turn) { }
42
43         const glm::mat4 &Transform() const;
44
45         Face GetFace() const { return Face(orient / 4); }
46         void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47         Turn GetTurn() const { return Turn(orient % 4); }
48         void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
49
50         static glm::tvec3<int> FaceNormal(Face face) {
51                 switch (face) {
52                         case FACE_UP:
53                                 return { 0, 1, 0 };
54                         case FACE_DOWN:
55                                 return { 0, -1, 0 };
56                         case FACE_RIGHT:
57                                 return { 1, 0, 0 };
58                         case FACE_LEFT:
59                                 return { -1, 0, 0 };
60                         case FACE_FRONT:
61                                 return { 0, 0, 1 };
62                         case FACE_BACK:
63                                 return { 0, 0, -1 };
64                         default:
65                                 return { 0, 0, 0 };
66                 }
67         }
68
69         static Face NormalFace(const glm::vec3 &norm) {
70                 const glm::vec3 anorm(abs(norm));
71                 if (anorm.x > anorm.y) {
72                         if (anorm.x > anorm.z) {
73                                 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
74                         } else {
75                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
76                         }
77                 } else {
78                         if (anorm.y > anorm.z) {
79                                 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
80                         } else {
81                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
82                         }
83                 }
84         }
85
86 };
87
88
89 /// attributes of a type of block
90 struct BlockType {
91
92         const Shape *shape;
93         glm::vec3 color;
94         glm::vec3 outline_color;
95
96         Block::Type id;
97
98         int luminosity;
99
100         bool visible;
101         bool block_light;
102
103         struct Faces {
104                 bool face[Block::FACE_COUNT];
105                 Faces &operator =(const Faces &other) {
106                         for (int i = 0; i < Block::FACE_COUNT; ++i) {
107                                 face[i] = other.face[i];
108                         }
109                         return *this;
110                 }
111                 bool operator [](Block::Face f) const {
112                         return face[f];
113                 }
114         } fill;
115
116         explicit BlockType(
117                 bool v = false,
118                 const glm::vec3 &color = { 1, 1, 1 },
119                 const Shape *shape = &DEFAULT_SHAPE
120         );
121
122         static const NullShape DEFAULT_SHAPE;
123
124         bool FaceFilled(const Block &, Block::Face) const;
125
126         void FillModel(
127                 Model::Buffer &m,
128                 const glm::mat4 &transform = glm::mat4(1.0f),
129                 Model::Index idx_offset = 0
130         ) const;
131         void FillBlockModel(
132                 BlockModel::Buffer &m,
133                 const glm::mat4 &transform = glm::mat4(1.0f),
134                 BlockModel::Index idx_offset = 0
135         ) const;
136         void FillOutlineModel(
137                 OutlineModel &m,
138                 const glm::vec3 &pos_offset = { 0, 0, 0 },
139                 OutlineModel::Index idx_offset = 0
140         ) const;
141
142 };
143
144
145 class BlockTypeRegistry {
146
147 public:
148         BlockTypeRegistry();
149
150 public:
151         Block::Type Add(const BlockType &);
152
153         size_t Size() const { return types.size(); }
154
155         BlockType *operator [](Block::Type id) { return &types[id]; }
156         const BlockType *Get(Block::Type id) const { return &types[id]; }
157
158 private:
159         std::vector<BlockType> types;
160
161 };
162
163 }
164
165 #endif