From 7409178f0afcc8f376078cc1304438375da09971 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Thu, 21 May 2020 20:21:43 -0400 Subject: [PATCH] transistioning to props and renderers --- vis/CMakeLists.txt | 5 - vis/src/combotest.cpp | 14 --- vis/src/main.cpp | 228 +++++++++++++++++++++++++----------------- 3 files changed, 136 insertions(+), 111 deletions(-) delete mode 100644 vis/src/combotest.cpp diff --git a/vis/CMakeLists.txt b/vis/CMakeLists.txt index e8e488d..fce5b98 100644 --- a/vis/CMakeLists.txt +++ b/vis/CMakeLists.txt @@ -19,8 +19,3 @@ add_executable(vis src/main.cpp) target_include_directories(vis PRIVATE include) target_link_libraries(vis PRIVATE tc glad glm glfw yaml-cpp) add_dependencies(vis shaders presets) - -add_executable(ctest src/combotest.cpp) -target_link_libraries(ctest PRIVATE yaml-cpp) -target_include_directories(ctest PRIVATE include) -add_dependencies(ctest presets) diff --git a/vis/src/combotest.cpp b/vis/src/combotest.cpp deleted file mode 100644 index dc2e294..0000000 --- a/vis/src/combotest.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include - -int main() { - auto cfg = YAML::LoadFile("presets/default.yaml"); - - for (const auto &group : cfg["groups"]) { -// std::cout << group["symbol"] << std::endl; - auto s = group["symbol"].as>(); - for (const auto e : s) std::cout << e << " "; - std::cout << std::endl; - } -} diff --git a/vis/src/main.cpp b/vis/src/main.cpp index 0c032e7..5691118 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -138,25 +138,30 @@ public: }; template -struct Slice { - GLenum mode; - vec3 color; +struct Prop { cgl::VertexArray vao; cgl::Buffer vbo; cgl::Buffer> ibo; - Slice(GLenum mode, vec3 color) : mode(mode), color(color), vao(), ibo(), vbo() {} + Prop() : vao(), vbo(), ibo() {} +}; + +template +struct Renderer { + virtual void draw(const Prop &) const = 0; +}; + +template +struct Slice : public Prop { + GLenum mode; + vec3 color; + + Slice(GLenum mode, vec3 color) : Prop(), mode(mode), color(color) {} Slice(Slice &) = delete; Slice(Slice &&) noexcept = default; - void draw() const { - vao.bound([&]() { - glDrawArrays(GL_POINTS, 0, ibo.count() * N); - }); - } - template static Slice build(const tc::Group &g, const C &coords, vec3 color, T all_sg_gens, const std::vector> &exclude) { @@ -170,58 +175,91 @@ struct Slice { } }; -struct Wire { - bool curve; - bool ortho; - vec3 color; - cgl::VertexArray vao; - cgl::Buffer vbo; - cgl::Buffer> ibo; +template +struct SliceRenderer : public Renderer { + cgl::pgm::vert defer = cgl::pgm::vert::file( + "shaders/slice/deferred.vs.glsl"); + cgl::pgm::geom slice = cgl::pgm::geom::file( + "shaders/slice/slice.gm.glsl"); + cgl::pgm::frag solid = cgl::pgm::frag::file( + "shaders/solid.fs.glsl"); - Wire(bool curve, bool ortho, vec3 color) : curve(curve), ortho(ortho), color(color), vao(), ibo(), vbo() {} + cgl::pipeline pipe; - Wire(Wire &) = delete; + SliceRenderer() { + pipe.stage(defer); + pipe.stage(slice); + pipe.stage(solid); + } - Wire(Wire &&) noexcept = default; + SliceRenderer(SliceRenderer &) = delete; - void draw() const { - vao.bound([&]() { - ibo.bound(GL_ELEMENT_ARRAY_BUFFER, [&]() { - glDrawElements(GL_LINES, ibo.count() * 2, GL_UNSIGNED_INT, nullptr); + SliceRenderer(SliceRenderer &&) noexcept = default; + + void draw(const Prop &prop) const override { + pipe.bound([&]() { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, prop.vbo); +// glProgramUniform3fv(solid, 2, 1, &prop.color.front()); + glProgramUniform3f(solid, 2, 1.f, 1.f, 1.f); + prop.vao.bound([&]() { + glDrawArrays(GL_POINTS, 0, prop.ibo.count() * N); }); }); } - - template - static Wire build(const tc::Group &g, const C &coords, bool curve, bool ortho, vec3 color, T all_sg_gens, - const std::vector> &exclude) { - Wire res(curve, ortho, color); - - res.vbo.put(points(g, coords)); - res.ibo.put(merge<2>(hull<2>(g, all_sg_gens, exclude))); - - return res; - } }; +//struct Wire { +// bool curve; +// bool ortho; +// vec3 color; +// cgl::VertexArray vao; +// cgl::Buffer vbo; +// cgl::Buffer> ibo; +// +// Wire(bool curve, bool ortho, vec3 color) : curve(curve), ortho(ortho), 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, bool ortho, vec3 color, T all_sg_gens, +// const std::vector> &exclude) { +// Wire res(curve, ortho, color); +// +// res.vbo.put(points(g, coords)); +// res.ibo.put(merge<2>(hull<2>(g, all_sg_gens, exclude))); +// +// return res; +// } +//}; + void run(const std::string &config_file, GLFWwindow *window) { glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - Shaders sh; +// Shaders sh; - auto wire_pipe = cgl::pipeline(); - wire_pipe - .stage(sh.direct_stereo) - .stage(sh.solid); - - auto slice_pipe = cgl::pipeline(); - slice_pipe - .stage(sh.defer) - .stage(sh.slice) - .stage(sh.solid); +// auto wire_pipe = cgl::pipeline(); +// wire_pipe +// .stage(sh.direct_stereo) +// .stage(sh.solid); +// +// auto slice_pipe = cgl::pipeline(); +// slice_pipe +// .stage(sh.defer) +// .stage(sh.slice) +// .stage(sh.solid); auto scene = YAML::LoadFile(config_file); @@ -231,7 +269,7 @@ void run(const std::string &config_file, GLFWwindow *window) { state.dimension = scene["dimension"].as(); auto slices = std::vector>(); - auto wires = std::vector(); +// auto wires = std::vector(); for (const auto &group_info : scene["groups"]) { auto symbol = group_info["symbol"].as>(); @@ -262,36 +300,38 @@ void run(const std::string &config_file, 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(); - auto ortho = wire_info["ortho"].IsDefined() && wire_info["ortho"].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, ortho, color, subgroups, exclude - )); - } else { - auto combos = Combos(gens, 1); - wires.push_back(Wire::build( - group, root, curve, ortho, color, combos, exclude - )); - } - } - } +// 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(); +// auto ortho = wire_info["ortho"].IsDefined() && wire_info["ortho"].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, ortho, color, subgroups, exclude +// )); +// } else { +// auto combos = Combos(gens, 1); +// wires.push_back(Wire::build( +// group, root, curve, ortho, color, combos, exclude +// )); +// } +// } +// } } auto ubo = cgl::Buffer(); glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo); + SliceRenderer<4> sRen{}; + while (!glfwWindowShouldClose(window)) { auto time = (float) glfwGetTime(); state.time_delta = state.time - time; @@ -308,27 +348,31 @@ void run(const std::string &config_file, GLFWwindow *window) { glLineWidth(1.5); - 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); +// 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); +// +// if (wire.ortho) wire_pipe.stage(sh.direct_ortho); +// else wire_pipe.stage(sh.direct_stereo); +// +// glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, wire.vbo); +// glProgramUniform3fv(sh.solid, 2, 1, &wire.color.front()); +// wire.draw(); +// } +// }); +// +// slice_pipe.bound([&]() { +// for (const auto &slice : slices) { +// glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, slice.vbo); +// glProgramUniform3fv(sh.solid, 2, 1, &slice.color.front()); +// slice.draw(); +// } +// }); - if (wire.ortho) wire_pipe.stage(sh.direct_ortho); - else wire_pipe.stage(sh.direct_stereo); - - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, wire.vbo); - glProgramUniform3fv(sh.solid, 2, 1, &wire.color.front()); - wire.draw(); - } - }); - - slice_pipe.bound([&]() { - for (const auto &slice : slices) { - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, slice.vbo); - glProgramUniform3fv(sh.solid, 2, 1, &slice.color.front()); - slice.draw(); - } - }); + for (const auto &slice : slices) { + sRen.draw(slice); + } glfwSwapInterval(2); glfwSwapBuffers(window);