1 #ifndef BLANK_WORLD_BLOCK_HPP_
2 #define BLANK_WORLD_BLOCK_HPP_
13 using Type = unsigned short;
32 static constexpr int ORIENT_COUNT = FACE_COUNT * TURN_COUNT;
37 constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE) noexcept
38 : type(type), orient(face * TURN_COUNT + turn) { }
40 const glm::mat4 &Transform() const noexcept { return orient2transform[orient]; }
42 Face GetFace() const noexcept { return Face(orient / TURN_COUNT); }
43 void SetFace(Face face) noexcept { orient = face * TURN_COUNT + GetTurn(); }
44 Turn GetTurn() const noexcept { return Turn(orient % TURN_COUNT); }
45 void SetTurn(Turn turn) noexcept { orient = GetFace() * TURN_COUNT + turn; }
47 Face OrientedFace(Face f) const noexcept { return orient2face[orient][f]; }
49 static Face Opposite(Face f) noexcept {
53 static int Axis(Face f) noexcept {
68 /// returns 1 for pro-axis, -1 for retro-axis, 0 for invalid faces
69 static int Direction(Face f) noexcept {
84 static glm::ivec3 FaceNormal(Face face) noexcept {
85 return face2normal[face];
88 static Face NormalFace(const glm::vec3 &norm) noexcept {
89 const glm::vec3 anorm(abs(norm));
90 if (anorm.x > anorm.y) {
91 if (anorm.x > anorm.z) {
92 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
94 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
97 if (anorm.y > anorm.z) {
98 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
100 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
107 explicit FaceSet(unsigned char v = 0)
110 bool IsSet(Face f) const {
111 return value & Mask(f);
124 value = Mask(FACE_COUNT) - 1;
131 return value == Mask(FACE_COUNT) - 1;
134 unsigned char Mask(Face f) const {
143 static const glm::ivec3 face2normal[6];
144 static const glm::mat4 orient2transform[ORIENT_COUNT];
145 static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
149 inline bool operator ==(const Block &a, const Block &b) {
150 return a.type == b.type && a.orient == b.orient;
153 inline bool operator !=(const Block &a, const Block &b) {
157 std::ostream &operator <<(std::ostream &, const Block &);
158 std::ostream &operator <<(std::ostream &, const Block::Face &);
159 std::ostream &operator <<(std::ostream &, const Block::Turn &);