X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel%2Fshape.cpp;h=7422f899e856297f7ccb2f49bbbf53c2c2fa991a;hb=8ab4ea13545cccbacbd1ed610968d3f481c1b3c8;hp=e9364bd45498d0f06bea9efa09435998fe502e48;hpb=5998b18978bd8e7a0c9deb516474634e1d3521c9;p=blank.git diff --git a/src/model/shape.cpp b/src/model/shape.cpp index e9364bd..7422f89 100644 --- a/src/model/shape.cpp +++ b/src/model/shape.cpp @@ -5,67 +5,100 @@ namespace blank { void Shape::Vertices( - EntityModel::Positions &vertex, - EntityModel::Normals &normal, - EntityModel::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( - EntityModel::Positions &vertex, - EntityModel::Normals &normal, - EntityModel::Indices &index, + EntityModel::Buffer &out, const glm::mat4 &transform, + 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() { @@ -153,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 }, @@ -286,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 }, @@ -305,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, }); } @@ -350,11 +449,34 @@ bool StairShape::Intersects( const glm::mat4 &M, const AABB &box, const glm::mat4 &box_M, - float &depth, + float &dist, glm::vec3 &normal ) const noexcept { - // TODO: this is wrong, but simple. so for now will have to do - return Intersection(bot, M, box, box_M, depth, normal) || Intersection(top, M, box, box_M, depth, normal); + 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; + } } }