]> git.localhorst.tv Git - blank.git/blobdiff - src/block.cpp
use precalculated matrices for block orientation
[blank.git] / src / block.cpp
index e22d3e63d8ce309f71903a00a69cb985461483dc..c9fceec85aeb6da7ebe03381bd3af182cf1ac22f 100644 (file)
@@ -1,27 +1,77 @@
 #include "block.hpp"
 
+#include "geometry.hpp"
+
+#include <glm/gtx/euler_angles.hpp>
+#include <glm/gtx/transform.hpp>
+
 
 namespace blank {
 
+namespace {
+
+const glm::mat4 block_transforms[Block::FACE_COUNT * Block::TURN_COUNT] = {
+       {  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: up,    turn: none
+       {  0,  0, -1,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: left
+       { -1,  0,  0,  0,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: up,    turn: around
+       {  0,  0,  1,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: right
+       {  1,  0,  0,  0,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: down,  turn: none
+       {  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: left
+       { -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: down,  turn: around
+       {  0,  0,  1,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: right
+       {  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: right, turn: none
+       {  0, -1,  0,  0,  0,  0, -1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: left
+       {  0, -1,  0,  0, -1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: right, turn: around
+       {  0, -1,  0,  0,  0,  0,  1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: right
+       {  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: left,  turn: none
+       {  0,  1,  0,  0,  0,  0,  1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: left
+       {  0,  1,  0,  0,  1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: left,  turn: around
+       {  0,  1,  0,  0,  0,  0, -1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: right
+       {  1,  0,  0,  0,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: none
+       {  0,  0, -1,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: left
+       { -1,  0,  0,  0,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: around
+       {  0,  0,  1,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: right
+       {  1,  0,  0,  0,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: none
+       {  0,  0, -1,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: left
+       { -1,  0,  0,  0,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: around
+       {  0,  0,  1,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: right
+};
+
+}
+
+const glm::mat4 &Block::Transform() const {
+       return block_transforms[orient];
+}
+
+
 const NullShape BlockType::DEFAULT_SHAPE;
 
-void BlockType::FillVBO(
-       const glm::vec3 &pos,
-       std::vector<glm::vec3> &vertices,
-       std::vector<glm::vec3> &colors,
-       std::vector<glm::vec3> &normals
+BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s)
+: shape(s)
+, color(col)
+, outline_color(-1, -1, -1)
+, id(0)
+, visible(v)
+, fill({ false, false, false, false, false, false }) {
+
+}
+
+void BlockType::FillModel(
+       Model::Buffer &buf,
+       const glm::mat4 &transform,
+       Model::Index idx_offset
 ) const {
-       shape->Vertices(vertices, pos);
-       colors.insert(colors.end(), shape->VertexCount(), color);
-       shape->Normals(normals);
+       shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset);
+       buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
 }
 
-void BlockType::FillOutlineVBO(
-       std::vector<glm::vec3> &vertices,
-       std::vector<glm::vec3> &colors
+void BlockType::FillOutlineModel(
+       OutlineModel &model,
+       const glm::vec3 &pos_offset,
+       OutlineModel::Index idx_offset
 ) const {
-       shape->Outline(vertices);
-       colors.insert(colors.end(), shape->OutlineCount(), outline_color);
+       shape->Outline(model.vertices, model.indices, pos_offset, idx_offset);
+       model.colors.insert(model.colors.end(), shape->OutlineCount(), outline_color);
 }
 
 
@@ -29,7 +79,7 @@ BlockTypeRegistry::BlockTypeRegistry() {
        Add(BlockType());
 }
 
-int BlockTypeRegistry::Add(const BlockType &t) {
+Block::Type BlockTypeRegistry::Add(const BlockType &t) {
        int id = types.size();
        types.push_back(t);
        types.back().id = id;