X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fmesh.cpp;h=906b20f3e0732d802e61c740380259f0d63fdc34;hb=04b035d837668eb6444ec34e59c018d0c25983fd;hp=4b0f5829d73ed552ff9fc8c4998a77b785425379;hpb=3542823a1af7f5063d7cc8da84efa248eb889b8a;p=blank.git diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp index 4b0f582..906b20f 100644 --- a/src/graphics/mesh.cpp +++ b/src/graphics/mesh.cpp @@ -1,9 +1,11 @@ #include "BlockMesh.hpp" #include "EntityMesh.hpp" -#include "OutlineMesh.hpp" +#include "PrimitiveMesh.hpp" #include "SkyBoxMesh.hpp" #include "SpriteMesh.hpp" +#include "../geometry/primitive.hpp" + #include #include @@ -16,10 +18,10 @@ void EntityMesh::Update(const Buffer &buf) noexcept { std::cerr << "EntityMesh: not enough tex coords!" << std::endl; } if (buf.hsl_mods.size() < buf.vertices.size()) { - std::cerr << "BlockMesh: not enough HSL modifiers!" << std::endl; + std::cerr << "EntityMesh: not enough HSL modifiers!" << std::endl; } if (buf.rgb_mods.size() < buf.vertices.size()) { - std::cerr << "BlockMesh: not enough RGB modifiers!" << std::endl; + std::cerr << "EntityMesh: not enough RGB modifiers!" << std::endl; } if (buf.normals.size() < buf.vertices.size()) { std::cerr << "EntityMesh: not enough normals!" << std::endl; @@ -29,18 +31,13 @@ void EntityMesh::Update(const Buffer &buf) noexcept { vao.Bind(); vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords); - vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods); - vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods); + vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods, true); + vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods, true); vao.PushAttribute(ATTRIB_NORMAL, buf.normals); vao.PushIndices(ATTRIB_INDEX, buf.indices); } -void EntityMesh::Draw() const noexcept { - vao.DrawTriangleElements(); -} - - void BlockMesh::Update(const Buffer &buf) noexcept { #ifndef NDEBUG if (buf.tex_coords.size() < buf.vertices.size()) { @@ -60,33 +57,69 @@ void BlockMesh::Update(const Buffer &buf) noexcept { vao.Bind(); vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords); - vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods); - vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods); + vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods, true); + vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods, true); vao.PushAttribute(ATTRIB_LIGHT, buf.lights); vao.PushIndices(ATTRIB_INDEX, buf.indices); } -void BlockMesh::Draw() const noexcept { - vao.DrawTriangleElements(); +void PrimitiveMesh::Buffer::FillRect( + float w, float h, + const Color &color, + const glm::vec2 &pivot +) { + Clear(); + Reserve(4, 6); + + vertices.emplace_back( -pivot.x, -pivot.y, 0.0f); + vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f); + vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f); + vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f); + + colors.resize(4, color); + + indices.assign({ 0, 2, 1, 1, 2, 3 }); } +void PrimitiveMesh::Buffer::OutlineBox(const AABB &box, const Color &color) { + Clear(); + Reserve(8, 24); + + vertices.emplace_back(box.min.x, box.min.y, box.min.z); + vertices.emplace_back(box.min.x, box.min.y, box.max.z); + vertices.emplace_back(box.min.x, box.max.y, box.min.z); + vertices.emplace_back(box.min.x, box.max.y, box.max.z); + vertices.emplace_back(box.max.x, box.min.y, box.min.z); + vertices.emplace_back(box.max.x, box.min.y, box.max.z); + vertices.emplace_back(box.max.x, box.max.y, box.min.z); + vertices.emplace_back(box.max.x, box.max.y, box.max.z); + + colors.resize(8, color); + + indices.assign({ + 0, 1, 1, 3, 3, 2, 2, 0, // left + 4, 5, 5, 7, 7, 6, 6, 4, // right + 0, 4, 1, 5, 3, 7, 2, 6, // others + }); +} -void OutlineMesh::Update(const Buffer &buf) noexcept { + +void PrimitiveMesh::Update(const Buffer &buf) noexcept { #ifndef NDEBUG if (buf.colors.size() < buf.vertices.size()) { - std::cerr << "OutlineMesh: not enough colors!" << std::endl; + std::cerr << "PrimitiveMesh: not enough colors!" << std::endl; } #endif vao.Bind(); vao.PushAttribute(ATTRIB_VERTEX, buf.vertices); - vao.PushAttribute(ATTRIB_COLOR, buf.colors); + vao.PushAttribute(ATTRIB_COLOR, buf.colors, true); vao.PushIndices(ATTRIB_INDEX, buf.indices); } -void OutlineMesh::Draw() noexcept { +void PrimitiveMesh::DrawLines() const noexcept { glEnable(GL_LINE_SMOOTH); glLineWidth(2.0f); vao.DrawLineElements(); @@ -122,10 +155,6 @@ void SkyBoxMesh::Update(const Buffer &buf) noexcept { vao.PushIndices(ATTRIB_INDEX, buf.indices); } -void SkyBoxMesh::Draw() const noexcept { - vao.DrawTriangleElements(); -} - void SpriteMesh::Buffer::LoadRect( float w, float h, @@ -163,9 +192,4 @@ void SpriteMesh::Update(const Buffer &buf) noexcept { vao.PushIndices(ATTRIB_INDEX, buf.indices); } - -void SpriteMesh::Draw() noexcept { - vao.DrawTriangleElements(); -} - }