From 1ed481d4a8507e3b118e5d49c2a1307548d6c649 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Thu, 21 May 2020 20:49:26 -0400 Subject: [PATCH] add directrenderer for wireframes --- vis/presets/h_4_stereo_wire.yaml | 4 +- vis/src/main.cpp | 200 +++++++++++++++---------------- 2 files changed, 101 insertions(+), 103 deletions(-) diff --git a/vis/presets/h_4_stereo_wire.yaml b/vis/presets/h_4_stereo_wire.yaml index d06ffc2..9329dd9 100644 --- a/vis/presets/h_4_stereo_wire.yaml +++ b/vis/presets/h_4_stereo_wire.yaml @@ -2,10 +2,10 @@ dimension: 5 groups: - symbol: [3, 4, 3] slices: - - root: [0.80, 0.09, 0.09, 0.09, 0.0] + - root: [0.75, 0.09, 0.09, 0.09, 0.0] color: [0.9, 0.9, 0.9] exclude: - [0, 1, 2] wires: - - root: [0.80, 0.09, 0.09, 0.09, 0.0] + - root: [0.75, 0.09, 0.09, 0.09, 0.0] color: [0.3, 0.3, 0.3] diff --git a/vis/src/main.cpp b/vis/src/main.cpp index 8848be3..46d2e00 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -173,19 +173,23 @@ struct Renderer { template struct Slice : public Prop { - GLenum mode; vec3 color; - Slice(GLenum mode, vec3 color) : Prop(), mode(mode), color(color) {} + Slice(vec3 color) : Prop(), color(color) {} Slice(Slice &) = delete; Slice(Slice &&) noexcept = default; template - static Slice build(const tc::Group &g, const C &coords, vec3 color, T all_sg_gens, - const std::vector> &exclude) { - Slice res(GL_POINTS, color); + static Slice build( + const tc::Group &g, + const C &coords, + vec3 color, + T all_sg_gens, + const std::vector> &exclude + ) { + Slice res(color); res.vbo.put(points(g, coords)); res.ibo.put(merge(hull(g, all_sg_gens, exclude))); @@ -230,39 +234,67 @@ struct SliceRenderer : public Renderer { } }; -//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; -// } -//}; + +template +struct DirectRenderer : public Renderer { + cgl::pgm::vert direct_stereo = cgl::pgm::vert::file( + "shaders/direct-stereo.vs.glsl"); + cgl::pgm::frag solid = cgl::pgm::frag::file( + "shaders/solid.fs.glsl"); + + cgl::pipeline pipe; + + DirectRenderer() { + pipe.stage(direct_stereo); + pipe.stage(solid); + } + + DirectRenderer(DirectRenderer &) = delete; + + DirectRenderer(DirectRenderer &&) noexcept = default; + + void bound(const std::function &action) const override { + pipe.bound(action); + } + + void _draw(const Prop &prop) const override { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, prop.vbo); +// glProgramUniform3fv(sh.solid, 2, 1, &wire.color.front()); + glProgramUniform3f(solid, 2, .3f, .3f, .3f); + prop.vao.bound([&]() { + prop.ibo.bound(GL_ELEMENT_ARRAY_BUFFER, [&]() { + glDrawElements(GL_LINES, prop.ibo.count() * N, GL_UNSIGNED_INT, nullptr); + }); + }); + } +}; + +struct Wire : public Prop<2> { + vec3 color; + + Wire(vec3 color) : Prop<2>(), color(color) {} + + Wire(Wire &) = delete; + + Wire(Wire &&) noexcept = default; + + 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(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); @@ -270,18 +302,8 @@ void run(const std::string &config_file, GLFWwindow *window) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -// 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); + SliceRenderer<4> sRen{}; + DirectRenderer<2> wRen{}; auto scene = YAML::LoadFile(config_file); @@ -291,8 +313,7 @@ void run(const std::string &config_file, GLFWwindow *window) { state.dimension = scene["dimension"].as(); auto slices = Bundle<4>(); -// auto slices = std::vector>(); -// auto wires = std::vector(); + auto wires = Bundle<2>(); for (const auto &group_info : scene["groups"]) { auto symbol = group_info["symbol"].as>(); @@ -323,38 +344,36 @@ 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; @@ -371,28 +390,7 @@ 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); -// -// 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(); -// } -// }); - + wRen.draw(wires); sRen.draw(slices); glfwSwapInterval(2);