1 #ifndef BLANK_BLOCK_HPP_
2 #define BLANK_BLOCK_HPP_
4 #include "geometry.hpp"
17 using Type = unsigned short;
18 using Pos = glm::vec3;
37 static constexpr int ORIENT_COUNT = FACE_COUNT * TURN_COUNT;
42 constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE) noexcept
43 : type(type), orient(face * TURN_COUNT + turn) { }
45 const glm::mat4 &Transform() const noexcept { return orient2transform[orient]; }
47 Face GetFace() const noexcept { return Face(orient / TURN_COUNT); }
48 void SetFace(Face face) noexcept { orient = face * TURN_COUNT + GetTurn(); }
49 Turn GetTurn() const noexcept { return Turn(orient % TURN_COUNT); }
50 void SetTurn(Turn turn) noexcept { orient = GetFace() * TURN_COUNT + turn; }
52 Face OrientedFace(Face f) const noexcept { return orient2face[orient][f]; }
54 static Face Opposite(Face f) noexcept {
58 static glm::tvec3<int> FaceNormal(Face face) noexcept {
59 return face2normal[face];
62 static Face NormalFace(const glm::vec3 &norm) noexcept {
63 const glm::vec3 anorm(abs(norm));
64 if (anorm.x > anorm.y) {
65 if (anorm.x > anorm.z) {
66 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
68 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
71 if (anorm.y > anorm.z) {
72 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
74 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
81 explicit FaceSet(unsigned char v = 0)
84 bool IsSet(Face f) const {
85 return value & Mask(f);
98 value = Mask(FACE_COUNT) - 1;
105 return value == Mask(FACE_COUNT) - 1;
108 unsigned char Mask(Face f) const {
117 static const glm::tvec3<int> face2normal[6];
118 static const glm::mat4 orient2transform[ORIENT_COUNT];
119 static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
124 /// attributes of a type of block
129 glm::vec3 outline_color;
139 bool face[Block::FACE_COUNT];
140 Faces &operator =(const Faces &other) noexcept {
141 for (int i = 0; i < Block::FACE_COUNT; ++i) {
142 face[i] = other.face[i];
146 bool operator [](Block::Face f) const noexcept {
153 const glm::vec3 &color = { 1, 1, 1 },
154 const Shape *shape = &DEFAULT_SHAPE
157 static const NullShape DEFAULT_SHAPE;
159 bool FaceFilled(const Block &block, Block::Face face) const noexcept {
160 return fill[block.OrientedFace(face)];
165 const glm::mat4 &transform = glm::mat4(1.0f),
166 Model::Index idx_offset = 0
169 BlockModel::Buffer &m,
170 const glm::mat4 &transform = glm::mat4(1.0f),
171 BlockModel::Index idx_offset = 0
173 void FillOutlineModel(
175 const glm::vec3 &pos_offset = { 0, 0, 0 },
176 OutlineModel::Index idx_offset = 0
182 class BlockTypeRegistry {
188 Block::Type Add(const BlockType &);
190 size_t Size() const noexcept { return types.size(); }
192 BlockType &operator [](Block::Type id) { return types[id]; }
193 const BlockType &Get(Block::Type id) const { return types[id]; }
196 std::vector<BlockType> types;