]> git.localhorst.tv Git - blank.git/blob - src/world/block.cpp
tvec[234]<int> -> ivec[234]
[blank.git] / src / world / block.cpp
1 #include "Block.hpp"
2 #include "BlockType.hpp"
3 #include "BlockTypeRegistry.hpp"
4
5 #include "../model/geometry.hpp"
6
7 #include <ostream>
8 #include <glm/gtx/euler_angles.hpp>
9 #include <glm/gtx/transform.hpp>
10
11
12 namespace blank {
13
14 const NullShape BlockType::DEFAULT_SHAPE;
15
16
17 bool operator ==(const Block &a, const Block &b) {
18         return a.type == b.type && a.orient == b.orient;
19 }
20
21 std::ostream &operator <<(std::ostream &out, const Block &block) {
22         return out << "Block(" << block.type << ", " << block.GetFace() << ", " << block.GetTurn() << ')';
23 }
24
25 std::ostream &operator <<(std::ostream &out, const Block::Face &face) {
26         switch (face) {
27                 case Block::FACE_UP:
28                         out << "FACE_UP";
29                         break;
30                 case Block::FACE_DOWN:
31                         out << "FACE_DOWN";
32                         break;
33                 case Block::FACE_RIGHT:
34                         out << "FACE_RIGHT";
35                         break;
36                 case Block::FACE_LEFT:
37                         out << "FACE_LEFT";
38                         break;
39                 case Block::FACE_FRONT:
40                         out << "FACE_FRONT";
41                         break;
42                 case Block::FACE_BACK:
43                         out << "FACE_BACK";
44                         break;
45                 default:
46                 case Block::FACE_COUNT:
47                         out << "invalid Block::Face";
48                         break;
49         }
50         return out;
51 }
52
53 std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) {
54         switch (turn) {
55                 case Block::TURN_NONE:
56                         out << "TURN_NONE";
57                         break;
58                 case Block::TURN_LEFT:
59                         out << "TURN_LEFT";
60                         break;
61                 case Block::TURN_AROUND:
62                         out << "TURN_AROUND";
63                         break;
64                 case Block::TURN_RIGHT:
65                         out << "TURN_RIGHT";
66                         break;
67                 default:
68                 case Block::TURN_COUNT:
69                         out << "invalid Block::Turn";
70                         break;
71         }
72         return out;
73 }
74
75
76 BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s) noexcept
77 : shape(s)
78 , color(col)
79 , outline_color(-1, -1, -1)
80 , label("some block")
81 , id(0)
82 , luminosity(0)
83 , visible(v)
84 , block_light(false)
85 , collision(false)
86 , collide_block(false)
87 , fill({ false, false, false, false, false, false }) {
88
89 }
90
91 void BlockType::FillEntityModel(
92         EntityModel::Buffer &buf,
93         const glm::mat4 &transform,
94         EntityModel::Index idx_offset
95 ) const noexcept {
96         shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset);
97         buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
98 }
99
100 void BlockType::FillBlockModel(
101         BlockModel::Buffer &buf,
102         const glm::mat4 &transform,
103         BlockModel::Index idx_offset
104 ) const noexcept {
105         shape->Vertices(buf.vertices, buf.indices, transform, idx_offset);
106         buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
107 }
108
109 void BlockType::FillOutlineModel(
110         OutlineModel::Buffer &buf,
111         const glm::vec3 &pos_offset,
112         OutlineModel::Index idx_offset
113 ) const noexcept {
114         shape->Outline(buf.vertices, buf.indices, pos_offset, idx_offset);
115         buf.colors.insert(buf.colors.end(), shape->OutlineCount(), outline_color);
116 }
117
118
119 BlockTypeRegistry::BlockTypeRegistry() {
120         Add(BlockType());
121 }
122
123 Block::Type BlockTypeRegistry::Add(const BlockType &t) {
124         int id = types.size();
125         types.push_back(t);
126         types.back().id = id;
127         return id;
128 }
129
130
131 const glm::mat4 Block::orient2transform[ORIENT_COUNT] = {
132         {  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: up,    turn: none
133         {  0,  0, -1,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: left
134         { -1,  0,  0,  0,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: up,    turn: around
135         {  0,  0,  1,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: right
136         {  1,  0,  0,  0,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: down,  turn: none
137         {  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: left
138         { -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: down,  turn: around
139         {  0,  0,  1,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: right
140         {  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: right, turn: none
141         {  0, -1,  0,  0,  0,  0, -1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: left
142         {  0, -1,  0,  0, -1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: right, turn: around
143         {  0, -1,  0,  0,  0,  0,  1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: right
144         {  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: left,  turn: none
145         {  0,  1,  0,  0,  0,  0,  1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: left
146         {  0,  1,  0,  0,  1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: left,  turn: around
147         {  0,  1,  0,  0,  0,  0, -1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: right
148         {  1,  0,  0,  0,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: none
149         {  0,  0, -1,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: left
150         { -1,  0,  0,  0,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: around
151         {  0,  0,  1,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: right
152         {  1,  0,  0,  0,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: none
153         {  0,  0, -1,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: left
154         { -1,  0,  0,  0,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: around
155         {  0,  0,  1,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: right
156 };
157
158 const glm::ivec3 Block::face2normal[FACE_COUNT] = {
159         {  0,  1,  0 },
160         {  0, -1,  0 },
161         {  1,  0,  0 },
162         { -1,  0,  0 },
163         {  0,  0,  1 },
164         {  0,  0, -1 },
165 };
166
167 const Block::Face Block::orient2face[ORIENT_COUNT][FACE_COUNT] = {
168         { FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  }, // face: up,    turn: none
169         { FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, }, // face: up,    turn: left
170         { FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, }, // face: up,    turn: around
171         { FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  }, // face: up,    turn: right
172         { FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, }, // face: down,  turn: none
173         { FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, }, // face: down,  turn: left
174         { FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  }, // face: down,  turn: around
175         { FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  }, // face: down,  turn: right
176         { FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  }, // face: right, turn: none
177         { FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    }, // face: right, turn: left
178         { FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, }, // face: right, turn: around
179         { FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  }, // face: right, turn: right
180         { FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  }, // face: left,  turn: none
181         { FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  }, // face: left,  turn: left
182         { FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, }, // face: left,  turn: around
183         { FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    }, // face: left,  turn: right
184         { FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  }, // face: front, turn: none
185         { FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, }, // face: front, turn: left
186         { FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    }, // face: front, turn: around
187         { FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  }, // face: front, turn: right
188         { FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    }, // face: back,  turn: none
189         { FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, }, // face: back,  turn: left
190         { FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  }, // face: back,  turn: around
191         { FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  }, // face: back,  turn: right
192 };
193
194 }