1 #ifndef BLANK_WORLD_BLOCK_HPP_
2 #define BLANK_WORLD_BLOCK_HPP_
13 using Type = unsigned short;
14 using Pos = glm::vec3;
33 static constexpr int ORIENT_COUNT = FACE_COUNT * TURN_COUNT;
38 constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE) noexcept
39 : type(type), orient(face * TURN_COUNT + turn) { }
41 const glm::mat4 &Transform() const noexcept { return orient2transform[orient]; }
43 Face GetFace() const noexcept { return Face(orient / TURN_COUNT); }
44 void SetFace(Face face) noexcept { orient = face * TURN_COUNT + GetTurn(); }
45 Turn GetTurn() const noexcept { return Turn(orient % TURN_COUNT); }
46 void SetTurn(Turn turn) noexcept { orient = GetFace() * TURN_COUNT + turn; }
48 Face OrientedFace(Face f) const noexcept { return orient2face[orient][f]; }
50 static Face Opposite(Face f) noexcept {
54 static int Axis(Face f) noexcept {
69 static glm::ivec3 FaceNormal(Face face) noexcept {
70 return face2normal[face];
73 static Face NormalFace(const glm::vec3 &norm) noexcept {
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 {
128 static const glm::ivec3 face2normal[6];
129 static const glm::mat4 orient2transform[ORIENT_COUNT];
130 static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
134 bool operator ==(const Block &, const Block &);
136 std::ostream &operator <<(std::ostream &, const Block &);
137 std::ostream &operator <<(std::ostream &, const Block::Face &);
138 std::ostream &operator <<(std::ostream &, const Block::Turn &);