]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
simplified block face stuff
[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 / TURN_COUNT); }
46         void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47         Turn GetTurn() const { return Turn(orient % TURN_COUNT); }
48         void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
49
50         static Face Opposite(Face f) {
51                 return Face(f ^ 1);
52         }
53
54         static glm::tvec3<int> FaceNormal(Face face) {
55                 switch (face) {
56                         case FACE_UP:
57                                 return { 0, 1, 0 };
58                         case FACE_DOWN:
59                                 return { 0, -1, 0 };
60                         case FACE_RIGHT:
61                                 return { 1, 0, 0 };
62                         case FACE_LEFT:
63                                 return { -1, 0, 0 };
64                         case FACE_FRONT:
65                                 return { 0, 0, 1 };
66                         case FACE_BACK:
67                                 return { 0, 0, -1 };
68                         default:
69                                 return { 0, 0, 0 };
70                 }
71         }
72
73         static Face NormalFace(const glm::vec3 &norm) {
74                 const glm::vec3 anorm(abs(norm));
75                 if (anorm.x > anorm.y) {
76                         if (anorm.x > anorm.z) {
77                                 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
78                         } else {
79                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
80                         }
81                 } else {
82                         if (anorm.y > anorm.z) {
83                                 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
84                         } else {
85                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
86                         }
87                 }
88         }
89
90 };
91
92
93 /// attributes of a type of block
94 struct BlockType {
95
96         const Shape *shape;
97         glm::vec3 color;
98         glm::vec3 outline_color;
99
100         Block::Type id;
101
102         int luminosity;
103
104         bool visible;
105         bool block_light;
106
107         struct Faces {
108                 bool face[Block::FACE_COUNT];
109                 Faces &operator =(const Faces &other) {
110                         for (int i = 0; i < Block::FACE_COUNT; ++i) {
111                                 face[i] = other.face[i];
112                         }
113                         return *this;
114                 }
115                 bool operator [](Block::Face f) const {
116                         return face[f];
117                 }
118         } fill;
119
120         explicit BlockType(
121                 bool v = false,
122                 const glm::vec3 &color = { 1, 1, 1 },
123                 const Shape *shape = &DEFAULT_SHAPE
124         );
125
126         static const NullShape DEFAULT_SHAPE;
127
128         bool FaceFilled(const Block &, Block::Face) const;
129
130         void FillModel(
131                 Model::Buffer &m,
132                 const glm::mat4 &transform = glm::mat4(1.0f),
133                 Model::Index idx_offset = 0
134         ) const;
135         void FillBlockModel(
136                 BlockModel::Buffer &m,
137                 const glm::mat4 &transform = glm::mat4(1.0f),
138                 BlockModel::Index idx_offset = 0
139         ) const;
140         void FillOutlineModel(
141                 OutlineModel &m,
142                 const glm::vec3 &pos_offset = { 0, 0, 0 },
143                 OutlineModel::Index idx_offset = 0
144         ) const;
145
146 };
147
148
149 class BlockTypeRegistry {
150
151 public:
152         BlockTypeRegistry();
153
154 public:
155         Block::Type Add(const BlockType &);
156
157         size_t Size() const { return types.size(); }
158
159         BlockType *operator [](Block::Type id) { return &types[id]; }
160         const BlockType *Get(Block::Type id) const { return &types[id]; }
161
162 private:
163         std::vector<BlockType> types;
164
165 };
166
167 }
168
169 #endif