]> git.localhorst.tv Git - blank.git/blob - src/block.cpp
fa39790ee35e0e396224d12581ed102baba1083e
[blank.git] / src / block.cpp
1 #include "block.hpp"
2
3 #include "geometry.hpp"
4
5 #include <glm/gtx/euler_angles.hpp>
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace blank {
10
11 namespace {
12
13 const glm::mat4 block_transforms[Block::DIR_COUNT * Block::ROT_COUNT] = {
14         glm::mat4(1.0f),
15         glm::eulerAngleY(PI_0p5),
16         glm::eulerAngleY(PI),
17         glm::eulerAngleY(PI_1p5),
18         glm::eulerAngleX(PI),
19         glm::eulerAngleYX(PI_0p5, PI),
20         glm::eulerAngleYX(PI, PI),
21         glm::eulerAngleYX(PI_1p5, PI),
22         glm::eulerAngleZ(PI_0p5),
23         glm::eulerAngleYZ(PI_0p5, PI_0p5),
24         glm::eulerAngleYZ(PI, PI_0p5),
25         glm::eulerAngleYZ(PI_1p5, PI_0p5),
26         glm::eulerAngleZ(PI_1p5),
27         glm::eulerAngleYZ(PI_0p5, PI_1p5),
28         glm::eulerAngleYZ(PI, PI_1p5),
29         glm::eulerAngleYZ(PI_1p5, PI_1p5),
30         glm::eulerAngleX(PI_0p5),
31         glm::eulerAngleYX(PI_0p5, PI_0p5),
32         glm::eulerAngleYX(PI, PI_0p5),
33         glm::eulerAngleYX(PI_1p5, PI_0p5),
34         glm::eulerAngleX(PI_1p5),
35         glm::eulerAngleYX(PI_0p5, PI_1p5),
36         glm::eulerAngleYX(PI, PI_1p5),
37         glm::eulerAngleYX(PI_1p5, PI_1p5),
38 };
39
40 }
41
42 const glm::mat4 &Block::Transform() const {
43         return block_transforms[orient];
44 }
45
46
47 const NullShape BlockType::DEFAULT_SHAPE;
48
49 BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s)
50 : shape(s)
51 , color(col)
52 , outline_color(-1, -1, -1)
53 , id(0)
54 , visible(v)
55 , fill({ false, false, false, false, false, false }) {
56
57 }
58
59 void BlockType::FillModel(
60         Model::Buffer &buf,
61         const glm::mat4 &transform,
62         Model::Index idx_offset
63 ) const {
64         shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset);
65         buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
66 }
67
68 void BlockType::FillOutlineModel(
69         OutlineModel &model,
70         const glm::vec3 &pos_offset,
71         OutlineModel::Index idx_offset
72 ) const {
73         shape->Outline(model.vertices, model.indices, pos_offset, idx_offset);
74         model.colors.insert(model.colors.end(), shape->OutlineCount(), outline_color);
75 }
76
77
78 BlockTypeRegistry::BlockTypeRegistry() {
79         Add(BlockType());
80 }
81
82 Block::Type BlockTypeRegistry::Add(const BlockType &t) {
83         int id = types.size();
84         types.push_back(t);
85         types.back().id = id;
86         return id;
87 }
88
89 }