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 /// returns 1 for pro-axis, -1 for retro-axis, 0 for invalid faces
70 static int Direction(Face f) noexcept {
85 static glm::ivec3 FaceNormal(Face face) noexcept {
86 return face2normal[face];
89 static Face NormalFace(const glm::vec3 &norm) noexcept {
90 const glm::vec3 anorm(abs(norm));
91 if (anorm.x > anorm.y) {
92 if (anorm.x > anorm.z) {
93 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
95 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
98 if (anorm.y > anorm.z) {
99 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
101 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
108 explicit FaceSet(unsigned char v = 0)
111 bool IsSet(Face f) const {
112 return value & Mask(f);
125 value = Mask(FACE_COUNT) - 1;
132 return value == Mask(FACE_COUNT) - 1;
135 unsigned char Mask(Face f) const {
144 static const glm::ivec3 face2normal[6];
145 static const glm::mat4 orient2transform[ORIENT_COUNT];
146 static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
150 inline bool operator ==(const Block &a, const Block &b) {
151 return a.type == b.type && a.orient == b.orient;
154 inline bool operator !=(const Block &a, const Block &b) {
158 std::ostream &operator <<(std::ostream &, const Block &);
159 std::ostream &operator <<(std::ostream &, const Block::Face &);
160 std::ostream &operator <<(std::ostream &, const Block::Turn &);