1 #ifndef BLANK_BLOCK_HPP_
2 #define BLANK_BLOCK_HPP_
4 #include "geometry.hpp"
17 using Type = unsigned short;
18 using Pos = glm::vec3;
40 constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE)
41 : type(type), orient(face * TURN_COUNT + turn) { }
43 const glm::mat4 &Transform() const;
45 Face GetFace() const { return Face(orient / TURN_COUNT); }
46 void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47 Turn GetTurn() const { return Turn(orient % TURN_COUNT); }
48 void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
50 static Face Opposite(Face f) {
54 static glm::tvec3<int> FaceNormal(Face face) {
73 static Face NormalFace(const glm::vec3 &norm) {
74 const glm::vec3 anorm(abs(norm));
75 if (anorm.x > anorm.y) {
76 if (anorm.x > anorm.z) {
77 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
79 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
82 if (anorm.y > anorm.z) {
83 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
85 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
92 explicit FaceSet(unsigned char v = 0)
95 bool IsSet(Face f) const {
96 return value & Mask(f);
109 value = Mask(FACE_COUNT) - 1;
116 return value == Mask(FACE_COUNT) - 1;
119 unsigned char Mask(Face f) const {
130 /// attributes of a type of block
135 glm::vec3 outline_color;
145 bool face[Block::FACE_COUNT];
146 Faces &operator =(const Faces &other) {
147 for (int i = 0; i < Block::FACE_COUNT; ++i) {
148 face[i] = other.face[i];
152 bool operator [](Block::Face f) const {
159 const glm::vec3 &color = { 1, 1, 1 },
160 const Shape *shape = &DEFAULT_SHAPE
163 static const NullShape DEFAULT_SHAPE;
165 bool FaceFilled(const Block &, Block::Face) const;
169 const glm::mat4 &transform = glm::mat4(1.0f),
170 Model::Index idx_offset = 0
173 BlockModel::Buffer &m,
174 const glm::mat4 &transform = glm::mat4(1.0f),
175 BlockModel::Index idx_offset = 0
177 void FillOutlineModel(
179 const glm::vec3 &pos_offset = { 0, 0, 0 },
180 OutlineModel::Index idx_offset = 0
186 class BlockTypeRegistry {
192 Block::Type Add(const BlockType &);
194 size_t Size() const { return types.size(); }
196 BlockType *operator [](Block::Type id) { return &types[id]; }
197 const BlockType *Get(Block::Type id) const { return &types[id]; }
200 std::vector<BlockType> types;