ENH: Use entt

This commit is contained in:
David Allemang
2023-02-04 21:20:53 -05:00
parent 65e4960f09
commit e792e93eb9
5 changed files with 145 additions and 174 deletions

View File

@@ -10,20 +10,12 @@ namespace cgl {
GLuint id{};
public:
using Element = T;
Buffer() {
glCreateBuffers(1, &id);
}
Buffer(const T &data, GLenum usage = GL_STATIC_DRAW)
: Buffer() {
put(data, usage);
}
Buffer(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW)
: Buffer() {
put(data, usage);
}
Buffer(Buffer &) = delete;
Buffer(Buffer &&o) noexcept {
@@ -60,12 +52,5 @@ namespace cgl {
std::copy(begin, end, (T*) ptr);
glUnmapNamedBuffer(id);
}
void bound(GLenum target, const std::function<void()> &action) const {
glBindBuffer(target, id);
action();
glBindBuffer(target, 0);
}
};
}

View File

@@ -83,11 +83,5 @@ namespace cgl {
glUseProgramStages(id, GL_COMPUTE_SHADER_BIT, pgm);
return *this;
}
void bound(const std::function<void()> &action) const {
glBindProgramPipeline(id);
action();
glBindProgramPipeline(0);
}
};
}

View File

@@ -33,43 +33,34 @@ namespace cgl {
return id;
}
void bound(const std::function<void()> &action) const {
glBindVertexArray(id);
action();
glBindVertexArray(0);
}
template<class T>
void pointer(
void format(
GLuint index,
const Buffer<T> &buf,
unsigned size,
GLenum type,
bool normalized = false,
unsigned stride = 0
) const {
bound([&]() {
glEnableVertexAttribArray(index);
buf.bound(GL_ARRAY_BUFFER, [&]() {
glVertexAttribPointer(index, size, type, normalized, stride, nullptr);
});
});
) {
glEnableVertexArrayAttrib(id, index);
glVertexArrayAttribFormat(id, index, size, type, normalized, stride);
}
template<class T>
void ipointer(
void iformat(
GLuint index,
const Buffer<T> &buf,
unsigned size,
GLenum type,
unsigned stride = 0
) const {
bound([&]() {
glEnableVertexAttribArray(index);
buf.bound(GL_ARRAY_BUFFER, [&]() {
glVertexAttribIPointer(index, size, type, stride, nullptr);
});
});
) {
glEnableVertexArrayAttrib(id, index);
glVertexArrayAttribIFormat(id, index, size, type, stride);
}
template<class Buf>
void vertexBuffer(
GLuint index,
Buf &buf,
unsigned offset = 0
) {
glVertexArrayVertexBuffer(id, index, buf, offset, sizeof(typename Buf::Element));
}
};
}

103
vis/src/comps.hpp Normal file
View File

@@ -0,0 +1,103 @@
#pragma once
#include <cgl/buffer.hpp>
#include <cgl/shaderprogram.hpp>
#include <cgl/vertexarray.hpp>
#include <cgl/pipeline.hpp>
#include <tc/groups.hpp>
#include <Eigen/Eigen>
#include <entt/entt.hpp>
#include "mirror.hpp"
#include "geometry.hpp"
#include "solver.hpp"
#include <shaders.hpp>
namespace vis {
struct Group {
tc::Group<> group;
vec5 root;
vec3 color;
std::vector<std::vector<size_t>> exclude {{0, 1, 2}};
};
struct VBOs {
cgl::Buffer<vec4> verts;
cgl::Buffer<Primitive<4>> ibo;
};
void upload_groups(entt::registry &reg) {
auto view = reg.view<const Group, VBOs>();
for (auto [entity, group, vbos]: view.each()) {
auto cosets = group.group.solve();
auto mirrors = mirror<5>(group.group);
auto corners = plane_intersections(mirrors);
vec5 start = corners * group.root;
tc::Path<vec5> path(cosets, mirrors.colwise());
Eigen::Array<float, 5, Eigen::Dynamic> higher(5, path.order());
path.walk(start, Reflect(), higher.matrix().colwise().begin());
Eigen::Array<float, 4, Eigen::Dynamic> lower = Stereo()(higher);
vbos.verts.put(lower.colwise().begin(), lower.colwise().end());
// todo generate all, then mask using glMultiDraw.
const size_t N = 4;
auto gens = generators(group.group);
auto combos = combinations(gens, N - 1);
auto inds = merge<N>(hull<N>(group.group, combos, group.exclude));
vbos.ibo.put(inds.begin(), inds.end());
}
}
struct SliceRenderer {
cgl::pgm::vert defer = cgl::pgm::vert(shaders::deferred_vs_glsl);
cgl::pgm::geom slice = cgl::pgm::geom(shaders::slice_gm_glsl);
cgl::pgm::frag solid = cgl::pgm::frag(shaders::solid_fs_glsl);
cgl::pipeline pipe;
cgl::VertexArray vao;
SliceRenderer() {
pipe.stage(defer);
pipe.stage(slice);
pipe.stage(solid);
vao.iformat(0, 4, GL_UNSIGNED_INT);
}
void operator()(entt::registry &reg) {
auto view = reg.view<const Group, VBOs>();
for (auto [entity, group, vbos]: view.each()) {
const size_t N = 4;
glBindProgramPipeline(pipe);
glProgramUniform3fv(solid, 2, 1, group.color.data());
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbos.verts);
vao.vertexBuffer(0, vbos.ibo);
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, vbos.ibo.count());
glBindVertexArray(0);
glBindProgramPipeline(0);
}
}
};
}

View File

@@ -7,19 +7,15 @@
#include "util.hpp"
#include "mirror.hpp"
#include "solver.hpp"
#include <cgl/debug.hpp>
#include <cgl/vertexarray.hpp>
#include <cgl/shaderprogram.hpp>
#include <cgl/pipeline.hpp>
#include <random>
#include <chrono>
#include <yaml-cpp/yaml.h>
#include "comps.hpp"
#include <shaders.hpp>
#ifndef NDEBUG
#include <cgl/debug.hpp>
#endif
#ifdef _WIN32
extern "C" {
__attribute__((unused)) __declspec(dllexport) int NvOptimusEnablement = 0x00000001;
@@ -93,10 +89,7 @@ std::vector<vec4> points(const tc::Group<> &group, const C &coords) {
Eigen::Array<float, 5, Eigen::Dynamic> higher(5, path.order());
path.walk(start, Reflect(), higher.matrix().colwise().begin());
// std::vector<vec5> higher(path.order());
// path.walk(start, Reflect(), higher.begin());
// Eigen::Array4Xf lower = higher.topRows<4>().rowwise() / (1 - higher.bottomRows<1>());
Eigen::Array4Xf lower = Stereo()(higher);
std::vector<vec4> vec(lower.cols());
@@ -105,96 +98,6 @@ std::vector<vec4> points(const tc::Group<> &group, const C &coords) {
return vec;
}
template<unsigned N>
struct Prop {
cgl::VertexArray vao;
cgl::Buffer<vec4> vbo;
cgl::Buffer<Primitive<N>> ibo;
vec3 color;
Prop() : vao(), vbo(), ibo() {}
};
template<unsigned N>
struct Renderer {
std::vector<Prop<N>> props;
virtual void bound(const std::function<void()> &action) const = 0;
virtual void _draw(const Prop<N> &) const = 0;
void render() const {
bound([&]() {
for (const auto &prop: props) {
_draw(prop);
}
});
}
};
template<unsigned N>
struct SliceProp : public Prop<N> {
SliceProp(vec3 color) : Prop<N>() {
this->color = color;
}
SliceProp(SliceProp &) = delete;
SliceProp(SliceProp &&) noexcept = default;
template<class T, class C>
static SliceProp<N> build(
const tc::Group<> &g,
const C &coords,
vec3 color,
T all_sg_gens,
const std::vector<std::vector<size_t>> &exclude
) {
SliceProp<N> res(color);
auto pts = points(g, coords);
res.vbo.put(pts.begin(), pts.end());
auto inds = merge<N>(hull<N>(g, all_sg_gens, exclude));
res.ibo.put(inds.begin(), inds.end());
res.vao.ipointer(0, res.ibo, 4, GL_UNSIGNED_INT);
return res;
}
};
template<unsigned N>
struct SliceRenderer : public Renderer<N> {
cgl::pgm::vert defer = cgl::pgm::vert(shaders::deferred_vs_glsl);
cgl::pgm::geom slice = cgl::pgm::geom(shaders::slice_gm_glsl);
cgl::pgm::frag solid = cgl::pgm::frag(shaders::solid_fs_glsl);
cgl::pipeline pipe;
SliceRenderer() {
pipe.stage(defer);
pipe.stage(slice);
pipe.stage(solid);
}
SliceRenderer(SliceRenderer &) = delete;
SliceRenderer(SliceRenderer &&) noexcept = default;
void bound(const std::function<void()> &action) const override {
pipe.bound(action);
}
void _draw(const Prop<N> &prop) const override {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, prop.vbo);
glProgramUniform3fv(solid, 2, 1, prop.color.data());
// glProgramUniform3f(solid, 2, 1.f, 1.f, 1.f);
prop.vao.bound([&]() {
glDrawArrays(GL_POINTS, 0, prop.ibo.count() * N);
});
}
};
void run(const std::string &config_file, GLFWwindow* window) {
#ifndef NDEBUG
@@ -207,34 +110,29 @@ void run(const std::string &config_file, GLFWwindow* window) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
SliceRenderer<4> sRen{};
entt::registry registry;
vis::SliceRenderer renderer;
State state{};
glfwSetWindowUserPointer(window, &state);
std::cout << "building..." << std::endl;
{
auto group = tc::schlafli({5, 3, 3, 2});
auto gens = generators(group);
vec5 root;
root << 0.80, 0.09, 0.09, 0.09, 0.04;
vec3 color;
color << 0.90, 0.90, 0.90;
std::vector<std::vector<size_t>> exclude{
{0, 1, 2},
};
auto combos = combinations(gens, 3);
sRen.props.push_back(SliceProp<4>::build(
group, root, color, combos, exclude
));
}
std::cout << "built" << std::endl;
state.dimension = 4;
{
auto entity = registry.create();
registry.emplace<vis::Group>(
entity,
tc::schlafli({5, 3, 3, 2}),
vec5{0.80, 0.09, 0.09, 0.09, 0.04},
vec3{0.90, 0.90, 0.90},
std::vector<std::vector<size_t>>{{0, 1, 2}}
);
registry.emplace<vis::VBOs>(entity);
}
vis::upload_groups(registry);
auto ubo = cgl::Buffer<Matrices>();
glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo);
@@ -254,7 +152,7 @@ void run(const std::string &config_file, GLFWwindow* window) {
glLineWidth(1.5);
sRen.render();
renderer(registry);
glfwSwapInterval(2);
glfwSwapBuffers(window);