]> git.localhorst.tv Git - blank.git/blobdiff - src/model/shape.cpp
sky box model & shader
[blank.git] / src / model / shape.cpp
index 82659437f60fc87506f3847e181494514a99a7cd..7422f899e856297f7ccb2f49bbbf53c2c2fa991a 100644 (file)
 namespace blank {
 
 void Shape::Vertices(
-       Model::Positions &vertex,
-       Model::Normals &normal,
-       Model::Indices &index
+       EntityModel::Buffer &out,
+       float tex_offset
 ) const {
        for (const auto &pos : vtx_pos) {
-               vertex.emplace_back(pos);
+               out.vertices.emplace_back(pos);
+       }
+       for (const auto &coord : vtx_tex_coords) {
+               out.tex_coords.emplace_back(coord.x, coord.y, coord.z + tex_offset);
        }
        for (const auto &nrm : vtx_nrm) {
-               normal.emplace_back(nrm);
+               out.normals.emplace_back(nrm);
        }
        for (auto idx : vtx_idx) {
-               index.emplace_back(idx);
+               out.indices.emplace_back(idx);
        }
 }
 
 void Shape::Vertices(
-       Model::Positions &vertex,
-       Model::Normals &normal,
-       Model::Indices &index,
+       EntityModel::Buffer &out,
        const glm::mat4 &transform,
-       Model::Index idx_offset
+       float tex_offset,
+       EntityModel::Index idx_offset
 ) const {
        for (const auto &pos : vtx_pos) {
-               vertex.emplace_back(transform * glm::vec4(pos, 1.0f));
+               out.vertices.emplace_back(transform * glm::vec4(pos, 1.0f));
+       }
+       for (const auto &coord : vtx_tex_coords) {
+               out.tex_coords.emplace_back(coord.x, coord.y, coord.z + tex_offset);
        }
        for (const auto &nrm : vtx_nrm) {
-               normal.emplace_back(transform * glm::vec4(nrm, 0.0f));
+               out.normals.emplace_back(transform * glm::vec4(nrm, 0.0f));
        }
        for (auto idx : vtx_idx) {
-               index.emplace_back(idx_offset + idx);
+               out.indices.emplace_back(idx_offset + idx);
        }
 }
 
 void Shape::Vertices(
-       BlockModel::Positions &vertex,
-       BlockModel::Indices &index,
+       BlockModel::Buffer &out,
        const glm::mat4 &transform,
+       float tex_offset,
        BlockModel::Index idx_offset
 ) const {
        for (const auto &pos : vtx_pos) {
-               vertex.emplace_back(transform * glm::vec4(pos, 1.0f));
+               out.vertices.emplace_back(transform * glm::vec4(pos, 1.0f));
+       }
+       for (const auto &coord : vtx_tex_coords) {
+               out.tex_coords.emplace_back(coord.x, coord.y, coord.z + tex_offset);
        }
        for (auto idx : vtx_idx) {
-               index.emplace_back(idx_offset + idx);
+               out.indices.emplace_back(idx_offset + idx);
        }
 }
 
-void Shape::Outline(
-       OutlineModel::Positions &vertex,
-       OutlineModel::Indices &index,
-       const OutlineModel::Position &elem_offset,
-       OutlineModel::Index idx_offset
+void Shape::Vertices(
+       SkyBoxModel::Buffer &out
 ) const {
-       for (const auto &pos : out_pos) {
-               vertex.emplace_back(elem_offset + pos);
+       for (const auto &pos : vtx_pos) {
+               out.vertices.emplace_back(pos);
        }
-       for (auto idx : out_idx) {
-               index.emplace_back(idx_offset + idx);
+       for (auto idx : vtx_idx) {
+               out.indices.emplace_back(idx);
        }
 }
 
+void Shape::Outline(OutlineModel::Buffer &out) const {
+       out.vertices.insert(out.vertices.end(), out_pos.begin(), out_pos.end());
+       out.indices.insert(out.indices.end(), out_idx.begin(), out_idx.end());
+}
+
+void Shape::SetShape(
+       const EntityModel::Positions &pos,
+       const EntityModel::Normals &nrm,
+       const EntityModel::Indices &idx
+) {
+       vtx_pos = pos;
+       vtx_nrm = nrm;
+       vtx_idx = idx;
+}
+
+void Shape::SetTexture(
+       const BlockModel::TexCoords &tex_coords
+) {
+       vtx_tex_coords = tex_coords;
+}
+
+void Shape::SetOutline(
+       const OutlineModel::Positions &pos,
+       const OutlineModel::Indices &idx
+) {
+       out_pos = pos;
+       out_idx = idx;
+}
+
 
 NullShape::NullShape()
 : Shape() {
@@ -83,7 +116,9 @@ bool NullShape::Intersects(
 bool NullShape::Intersects(
        const glm::mat4 &,
        const AABB &,
-       const glm::mat4 &
+       const glm::mat4 &,
+       float &,
+       glm::vec3 &
 ) const noexcept {
        return false;
 }
@@ -151,6 +186,32 @@ CuboidShape::CuboidShape(const AABB &b)
                 16, 17, 18, 18, 17, 19, // left
                 20, 21, 22, 22, 21, 23, // right
        });
+       SetTexture({
+               { 0.0f, 1.0f, 0.0f }, // front
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f }, // back
+               { 1.0f, 0.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f }, // top
+               { 0.0f, 1.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f }, // bottom
+               { 0.0f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f }, // left
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f }, // right
+               { 1.0f, 0.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+       });
        SetOutline({
                { bb.min.x, bb.min.y, bb.min.z }, // back
                { bb.max.x, bb.min.y, bb.min.z },
@@ -178,9 +239,11 @@ bool CuboidShape::Intersects(
 bool CuboidShape::Intersects(
        const glm::mat4 &M,
        const AABB &box,
-       const glm::mat4 &box_M
+       const glm::mat4 &box_M,
+       float &depth,
+       glm::vec3 &normal
 ) const noexcept {
-       return Intersection(bb, M, box, box_M);
+       return Intersection(bb, M, box, box_M, depth, normal);
 }
 
 
@@ -282,6 +345,48 @@ StairShape::StairShape(const AABB &bb, const glm::vec2 &clip)
                32, 33, 34, 34, 33, 35, // right, upper
                36, 37, 38, 38, 37, 39, // right, lower
        });
+       SetTexture({
+               { 0.0f, 0.5f, 0.0f }, // front, upper
+               { 1.0f, 0.5f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f }, // front, lower
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 0.5f, 0.0f },
+               { 1.0f, 0.5f, 0.0f },
+               { 1.0f, 1.0f, 0.0f }, // back
+               { 1.0f, 0.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f }, // top, upper
+               { 0.0f, 0.5f, 0.0f },
+               { 1.0f, 0.0f, 0.0f },
+               { 1.0f, 0.5f, 0.0f },
+               { 0.0f, 0.5f, 0.0f }, // top, lower
+               { 0.0f, 1.0f, 0.0f },
+               { 1.0f, 0.5f, 0.0f },
+               { 1.0f, 1.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f }, // bottom
+               { 0.0f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 0.5f, 0.0f }, // left, upper
+               { 0.5f, 0.5f, 0.0f },
+               { 0.0f, 0.0f, 0.0f },
+               { 0.5f, 0.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f }, // left, lower
+               { 1.0f, 1.0f, 0.0f },
+               { 0.0f, 0.5f, 0.0f },
+               { 1.0f, 0.5f, 0.0f },
+               { 1.0f, 0.5f, 0.0f }, // right, upper
+               { 1.0f, 0.0f, 0.0f },
+               { 0.5f, 0.5f, 0.0f },
+               { 0.5f, 0.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f }, // right, lower
+               { 1.0f, 0.5f, 0.0f },
+               { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 0.5f, 0.0f },
+       });
        SetOutline({
                { bot.min.x, bot.min.y, bot.min.z }, // bottom
                { bot.max.x, bot.min.y, bot.min.z },
@@ -301,8 +406,6 @@ StairShape::StairShape(const AABB &bb, const glm::vec2 &clip)
                 8,  9,  9, 11, 11, 10, 10 , 8, // top
                 0,  8,  4, 10,  2,  6, // verticals, btf
                 1,  9,  5, 11,  3,  7,
-       //       5,  8,  7, 10,
-       //       1,  9,  3, 11,
        });
 }
 
@@ -345,9 +448,35 @@ bool StairShape::Intersects(
 bool StairShape::Intersects(
        const glm::mat4 &M,
        const AABB &box,
-       const glm::mat4 &box_M
+       const glm::mat4 &box_M,
+       float &dist,
+       glm::vec3 &normal
 ) const noexcept {
-       return Intersection(bot, M, box, box_M) || Intersection(top, M, box, box_M);
+       bool top_hit, bot_hit;
+       float top_dist, bot_dist;
+       glm::vec3 top_normal, bot_normal;
+
+       top_hit = Intersection(bot, M, box, box_M, top_dist, top_normal);
+       bot_hit = Intersection(top, M, box, box_M, bot_dist, bot_normal);
+
+       if (top_hit) {
+               if (bot_hit && bot_dist < top_dist) {
+                       dist = bot_dist;
+                       normal = bot_normal;
+                       return true;
+               } else {
+                       dist = top_dist;
+                       normal = top_normal;
+                       return true;
+               }
+               return true;
+       } else if (bot_hit) {
+               dist = bot_dist;
+               normal = bot_normal;
+               return true;
+       } else {
+               return false;
+       }
 }
 
 }