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 int Axis(Face f) noexcept {
73 static glm::tvec3<int> FaceNormal(Face face) noexcept {
74 return face2normal[face];
77 static Face NormalFace(const glm::vec3 &norm) noexcept {
78 const glm::vec3 anorm(abs(norm));
79 if (anorm.x > anorm.y) {
80 if (anorm.x > anorm.z) {
81 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
83 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
86 if (anorm.y > anorm.z) {
87 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
89 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
96 explicit FaceSet(unsigned char v = 0)
99 bool IsSet(Face f) const {
100 return value & Mask(f);
113 value = Mask(FACE_COUNT) - 1;
120 return value == Mask(FACE_COUNT) - 1;
123 unsigned char Mask(Face f) const {
132 static const glm::tvec3<int> face2normal[6];
133 static const glm::mat4 orient2transform[ORIENT_COUNT];
134 static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
139 /// attributes of a type of block
144 glm::vec3 outline_color;
154 bool face[Block::FACE_COUNT];
155 Faces &operator =(const Faces &other) noexcept {
156 for (int i = 0; i < Block::FACE_COUNT; ++i) {
157 face[i] = other.face[i];
161 bool operator [](Block::Face f) const noexcept {
168 const glm::vec3 &color = { 1, 1, 1 },
169 const Shape *shape = &DEFAULT_SHAPE
172 static const NullShape DEFAULT_SHAPE;
174 bool FaceFilled(const Block &block, Block::Face face) const noexcept {
175 return fill[block.OrientedFace(face)];
180 const glm::mat4 &transform = glm::mat4(1.0f),
181 Model::Index idx_offset = 0
184 BlockModel::Buffer &m,
185 const glm::mat4 &transform = glm::mat4(1.0f),
186 BlockModel::Index idx_offset = 0
188 void FillOutlineModel(
190 const glm::vec3 &pos_offset = { 0, 0, 0 },
191 OutlineModel::Index idx_offset = 0
197 class BlockTypeRegistry {
203 Block::Type Add(const BlockType &);
205 size_t Size() const noexcept { return types.size(); }
207 BlockType &operator [](Block::Type id) { return types[id]; }
208 const BlockType &Get(Block::Type id) const { return types[id]; }
211 std::vector<BlockType> types;