From c345e54d02647f2697551f595743b0f1ebf6ed84 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Tue, 10 Mar 2020 00:38:38 -0400 Subject: [PATCH] slices working, broken colors --- vis/include/cgl/render.hpp | 10 ++++ vis/src/main.cpp | 94 +++++++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/vis/include/cgl/render.hpp b/vis/include/cgl/render.hpp index b115647..12daa26 100644 --- a/vis/include/cgl/render.hpp +++ b/vis/include/cgl/render.hpp @@ -39,6 +39,16 @@ namespace cgl { return id; } + [[nodiscard]] size_t size() const { + GLint res; + glGetNamedBufferParameteriv(id, GL_BUFFER_SIZE, &res); + return (size_t) res; + } + + [[nodiscard]] size_t count() const { + return size() / sizeof(T); + } + void put(const T &data, GLenum usage = GL_STATIC_DRAW) { glNamedBufferData(id, sizeof(T), &data, usage); } diff --git a/vis/src/main.cpp b/vis/src/main.cpp index f5f6732..74ccee9 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -29,18 +29,45 @@ struct Matrices { }; template -struct MeshRef { +struct DirectMesh { + cgl::buffer> ibo; + GLenum mode; + + explicit DirectMesh(GLenum mode, const Mesh &mesh) + : ibo(), mode(mode) { + + ibo.put(mesh.prims); + } + + void draw() const { + ibo.bound(GL_ELEMENT_ARRAY_BUFFER, [&]() { + glDrawElements(mode, ibo.count() * N, GL_UNSIGNED_INT, nullptr); + }); + } +}; + +template +struct DeferredMesh { cgl::vertexarray vao; cgl::buffer> ibo; - unsigned primitive_count; - unsigned index_count; + explicit DeferredMesh(const Mesh &mesh) + : ibo(), vao() { - explicit MeshRef(const Mesh &mesh) : vao(), ibo() { - primitive_count = mesh.size(); - index_count = primitive_count * N; + ibo.put(mesh.prims); - ibo.put(mesh.prims);; + vao.bound([&]() { + ibo.bound(GL_ARRAY_BUFFER, [&]() { + glEnableVertexAttribArray(0); + glVertexAttribIPointer(0, N, GL_INT, 0, nullptr); + }); + }); + } + + void draw() const { + vao.bound([&]() { + glDrawArrays(GL_POINTS, 0, ibo.count() * N); + }); } }; @@ -70,6 +97,18 @@ Matrices build(GLFWwindow *window, float st) { return Matrices(proj, view); } +template +std::vector> poly_parts(const tc::Group &group) { + std::vector> parts; + auto g_gens = gens(group); + for (const auto &sg_gens : Combos(g_gens, N - 1)) { + parts.push_back( + triangulate(group, sg_gens).tile(group, g_gens, sg_gens) + ); + } + return parts; +} + void run(GLFWwindow *window) { glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_POINT_SMOOTH); @@ -90,11 +129,16 @@ void run(GLFWwindow *window) { auto proj_pipe = cgl::pipeline(); proj_pipe .stage(direct_stereo) - .stage(curve_stereo) + .stage(solid); + + auto slice_pipe = cgl::pipeline(); + slice_pipe + .stage(defer) + .stage(slice) .stage(solid); //region points - auto group = tc::group::H(4); + auto group = tc::group::F4(); auto res = group.solve(); auto mirrors = mirror(group); @@ -106,15 +150,11 @@ void run(GLFWwindow *window) { auto g_gens = gens(group); - const unsigned WIRES_N = 2; - const GLenum WIRE_MODE = GL_LINES; - std::vector> wires; + auto wire_data = merge(poly_parts<2>(group)); + DirectMesh<2> wires(GL_LINES, wire_data); - for (const auto &sg_gens : Combos(g_gens, WIRES_N - 1)) { - const auto s = triangulate(group, sg_gens).tile(group, g_gens, sg_gens); - - wires.emplace_back(s); - } + auto slice_data = merge(poly_parts<4>(group)); + DeferredMesh<4> slices(slice_data); //endregion @@ -125,8 +165,6 @@ void run(GLFWwindow *window) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo); glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo); - glBindVertexArray(0); - while (!glfwWindowShouldClose(window)) { int width, height; glfwGetFramebufferSize(window, &width, &height); @@ -138,20 +176,22 @@ void run(GLFWwindow *window) { Matrices mats = build(window, st); ubo.put(mats); - glLineWidth(1.5); const auto wires_dark = glm::vec3(.3, .3, .3); const auto wires_light = wires_dark; - proj_pipe.bound([&]() { - for (const auto &ref : wires) { - glProgramUniform3f(solid, 2, 1, 1, 1); + glLineWidth(1.5); - ref.ibo.bound(GL_ELEMENT_ARRAY_BUFFER, [&]() { - glDrawElements(WIRE_MODE, ref.index_count, GL_UNSIGNED_INT, nullptr); - }); - } + glProgramUniform3f(solid, 2, 0.9, 0.9, 0.9); + proj_pipe.bound([&]() { + wires.draw(); }); + glProgramUniform3f(solid, 2, 0.9, 0.1, 0.1); + slice_pipe.bound([&]() { + slices.draw(); + }); + + glfwSwapInterval(2); glfwSwapBuffers(window); glfwPollEvents();