From 2606f74f700fa837c40352c8324c2c69bdb29112 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Wed, 18 Mar 2020 17:41:59 -0400 Subject: [PATCH] load wireframes from files --- vis/include/cgl/pipeline.hpp | 7 +++- vis/presets/default.yaml | 4 ++ vis/src/main.cpp | 74 +++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/vis/include/cgl/pipeline.hpp b/vis/include/cgl/pipeline.hpp index 0175cfd..209f980 100644 --- a/vis/include/cgl/pipeline.hpp +++ b/vis/include/cgl/pipeline.hpp @@ -11,7 +11,7 @@ #include -namespace cgl{ +namespace cgl { class pipeline { protected: GLuint id{}; @@ -49,6 +49,11 @@ namespace cgl{ return std::string(buffer); } + pipeline &unstage(GLenum stage_bits) { + glUseProgramStages(id, stage_bits, 0); + return *this; + } + pipeline &stage(const ShaderProgram &pgm) { glUseProgramStages(id, GL_VERTEX_SHADER_BIT, pgm); return *this; diff --git a/vis/presets/default.yaml b/vis/presets/default.yaml index 2cc082c..b82040c 100644 --- a/vis/presets/default.yaml +++ b/vis/presets/default.yaml @@ -7,3 +7,7 @@ groups: color: [0.3, 0.3, 0.3] exclude: - [0, 1, 2] + wires: + - root: [0.50, 0.05, 0.05, 0.05, 0.01] + color: [0.9, 0.9, 0.9] + curve: true diff --git a/vis/src/main.cpp b/vis/src/main.cpp index 9c94fde..cd18aab 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -147,6 +147,39 @@ struct Slice { } }; +struct Wire { + bool curve; + vec3 color; + cgl::VertexArray vao; + cgl::Buffer vbo; + cgl::Buffer> ibo; + + Wire(bool curve, vec3 color) : curve(curve), color(color), vao(), ibo(), vbo() {} + + Wire(Wire &) = delete; + + Wire(Wire &&) noexcept = default; + + void draw() const { + vao.bound([&]() { + ibo.bound(GL_ELEMENT_ARRAY_BUFFER, [&]() { + glDrawElements(GL_LINES, ibo.count() * 2, GL_UNSIGNED_INT, nullptr); + }); + }); + } + + template + static Wire build(const tc::Group &g, const C &coords, bool curve, vec3 color, T all_sg_gens, + const std::vector> &exclude) { + Wire res(curve, color); + + res.vbo.put(points(g, coords)); + res.ibo.put(merge<2>(hull<2>(g, all_sg_gens, exclude))); + + return res; + } +}; + void run(GLFWwindow *window) { glEnable(GL_DEPTH_TEST); @@ -155,8 +188,8 @@ void run(GLFWwindow *window) { Shaders sh; - auto proj_pipe = cgl::pipeline(); - proj_pipe + auto wire_pipe = cgl::pipeline(); + wire_pipe .stage(sh.direct_stereo) .stage(sh.solid); @@ -169,6 +202,7 @@ void run(GLFWwindow *window) { auto scene = YAML::LoadFile("presets/default.yaml"); auto slices = std::vector>(); + auto wires = std::vector(); for (const auto &group_info : scene["groups"]) { auto symbol = group_info["symbol"].as>(); @@ -198,6 +232,31 @@ void run(GLFWwindow *window) { } } } + + if (group_info["wires"].IsDefined()) { + for (const auto &wire_info : group_info["wires"]) { + auto root = wire_info["root"].as(); + auto color = wire_info["color"].as(); + auto exclude = std::vector>(); + auto curve = wire_info["curve"].IsDefined() && wire_info["curve"].as(); + + if (wire_info["exclude"].IsDefined()) { + exclude = wire_info["exclude"].as>>(); + } + + if (wire_info["subgroups"].IsDefined()) { + auto subgroups = wire_info["subgroups"].as>>(); + wires.push_back(Wire::build( + group, root, curve, color, subgroups, exclude + )); + } else { + auto combos = Combos(gens, 1); + wires.push_back(Wire::build( + group, root, curve, color, combos, exclude + )); + } + } + } } auto ubo = cgl::Buffer(); @@ -224,6 +283,17 @@ void run(GLFWwindow *window) { } }); + wire_pipe.bound([&]() { + for (const auto &wire : wires) { + if (wire.curve) wire_pipe.stage(sh.curve_stereo); + else wire_pipe.unstage(GL_GEOMETRY_SHADER_BIT); + + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, wire.vbo); + glProgramUniform3fv(sh.solid, 2, 1, &wire.color.front()); + wire.draw(); + } + }); + glfwSwapInterval(2); glfwSwapBuffers(window);