3 #include "geometry.hpp"
5 #include <glm/gtx/euler_angles.hpp>
6 #include <glm/gtx/transform.hpp>
13 const glm::mat4 block_transforms[Block::FACE_COUNT * Block::TURN_COUNT] = {
14 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, }, // face: up, turn: none
15 { 0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, }, // face: up, turn: left
16 { -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, }, // face: up, turn: around
17 { 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, }, // face: up, turn: right
18 { 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, }, // face: down, turn: none
19 { 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, }, // face: down, turn: left
20 { -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, }, // face: down, turn: around
21 { 0, 0, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, }, // face: down, turn: right
22 { 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, }, // face: right, turn: none
23 { 0, -1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 1, }, // face: right, turn: left
24 { 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, }, // face: right, turn: around
25 { 0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1, }, // face: right, turn: right
26 { 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, }, // face: left, turn: none
27 { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, }, // face: left, turn: left
28 { 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, }, // face: left, turn: around
29 { 0, 1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 1, }, // face: left, turn: right
30 { 1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, }, // face: front, turn: none
31 { 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, }, // face: front, turn: left
32 { -1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 1, }, // face: front, turn: around
33 { 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, }, // face: front, turn: right
34 { 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, }, // face: back, turn: none
35 { 0, 0, -1, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, }, // face: back, turn: left
36 { -1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, }, // face: back, turn: around
37 { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, }, // face: back, turn: right
42 const glm::mat4 &Block::Transform() const {
43 return block_transforms[orient];
47 const NullShape BlockType::DEFAULT_SHAPE;
49 BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s)
52 , outline_color(-1, -1, -1)
55 , fill({ false, false, false, false, false, false }) {
61 const Block::Face face_map[Block::FACE_COUNT * Block::TURN_COUNT][Block::FACE_COUNT] = {
62 { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_FRONT, Block::FACE_BACK, }, // face: up, turn: none x
63 { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_LEFT, Block::FACE_RIGHT, }, // face: up, turn: left
64 { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_BACK, Block::FACE_FRONT, }, // face: up, turn: around
65 { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_RIGHT, Block::FACE_LEFT, }, // face: up, turn: right
66 { Block::FACE_DOWN, Block::FACE_UP, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_BACK, Block::FACE_FRONT, }, // face: down, turn: none
67 { Block::FACE_DOWN, Block::FACE_UP, Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_LEFT, Block::FACE_RIGHT, }, // face: down, turn: left
68 { Block::FACE_DOWN, Block::FACE_UP, Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_FRONT, Block::FACE_BACK, }, // face: down, turn: around
69 { Block::FACE_DOWN, Block::FACE_UP, Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_RIGHT, Block::FACE_LEFT, }, // face: down, turn: right
70 { Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_UP, Block::FACE_DOWN, Block::FACE_FRONT, Block::FACE_BACK, }, // face: right, turn: none
71 { Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_DOWN, Block::FACE_UP, }, // face: right, turn: left
72 { Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_DOWN, Block::FACE_UP, Block::FACE_BACK, Block::FACE_FRONT, }, // face: right, turn: around
73 { Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_UP, Block::FACE_DOWN, }, // face: right, turn: right
74 { Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_DOWN, Block::FACE_UP, Block::FACE_FRONT, Block::FACE_BACK, }, // face: left, turn: none
75 { Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_UP, Block::FACE_DOWN, }, // face: left, turn: left
76 { Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_UP, Block::FACE_DOWN, Block::FACE_BACK, Block::FACE_FRONT, }, // face: left, turn: around
77 { Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_DOWN, Block::FACE_UP, }, // face: left, turn: right
78 { Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_UP, Block::FACE_DOWN, }, // face: front, turn: none
79 { Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_UP, Block::FACE_DOWN, Block::FACE_LEFT, Block::FACE_RIGHT, }, // face: front, turn: left
80 { Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_DOWN, Block::FACE_UP, }, // face: front, turn: around
81 { Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_DOWN, Block::FACE_UP, Block::FACE_RIGHT, Block::FACE_LEFT, }, // face: front, turn: right
82 { Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_DOWN, Block::FACE_UP, }, // face: back, turn: none
83 { Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_DOWN, Block::FACE_UP, Block::FACE_LEFT, Block::FACE_RIGHT, }, // face: back, turn: left
84 { Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_UP, Block::FACE_DOWN, }, // face: back, turn: around
85 { Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_UP, Block::FACE_DOWN, Block::FACE_RIGHT, Block::FACE_LEFT, }, // face: back, turn: right
90 bool BlockType::FaceFilled(const Block &block, Block::Face face) const {
91 return fill[face_map[block.orient][face]];
94 void BlockType::FillModel(
96 const glm::mat4 &transform,
97 Model::Index idx_offset
99 shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset);
100 buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
103 void BlockType::FillOutlineModel(
105 const glm::vec3 &pos_offset,
106 OutlineModel::Index idx_offset
108 shape->Outline(model.vertices, model.indices, pos_offset, idx_offset);
109 model.colors.insert(model.colors.end(), shape->OutlineCount(), outline_color);
113 BlockTypeRegistry::BlockTypeRegistry() {
117 Block::Type BlockTypeRegistry::Add(const BlockType &t) {
118 int id = types.size();
120 types.back().id = id;