WIP: organization

- move group, root into Structure<>
- semantic type aliases like VectorDf, MatrixGXui, etc.
- separate components and systems files
This commit is contained in:
David Allemang
2023-02-12 09:59:27 -05:00
parent 16e195a69b
commit 844bf23b3b
4 changed files with 130 additions and 66 deletions

View File

@@ -9,7 +9,7 @@ add_custom_command(
add_executable(vis
src/main.cpp
src/comps.hpp
src/components.hpp
)
target_include_directories(vis PRIVATE include)
target_link_libraries(vis PRIVATE

97
vis/src/components.hpp Normal file
View File

@@ -0,0 +1,97 @@
#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 {
template<int R_, int D_, int G_>
struct Structure {
static constexpr auto Rank = R_;
static constexpr auto Dim = D_;
static constexpr auto Grade = G_;
using Affine = Eigen::Transform<float, Dim, Eigen::Affine>;
using VectorRf = Eigen::Vector<float, Rank>;
using MatrixRXf = Eigen::Matrix<float, Rank, Eigen::Dynamic>;
using VectorDf = Eigen::Vector<float, Dim>;
using MatrixDXf = Eigen::Matrix<float, Dim, Eigen::Dynamic>;
using VectorGf = Eigen::Vector<float, Grade>;
using MatrixGXf = Eigen::Matrix<float, Grade, Eigen::Dynamic>;
using ArrayRui = Eigen::Array<GLuint, Rank, 1>;
using ArrayRXui = Eigen::Array<GLuint, Rank, Eigen::Dynamic>;
using ArrayDui = Eigen::Array<GLuint, Dim, 1>;
using ArrayDXui = Eigen::Array<GLuint, Dim, Eigen::Dynamic>;
using ArrayGui = Eigen::Array<GLuint, Grade, 1>;
using ArrayGXui = Eigen::Array<GLuint, Grade, Eigen::Dynamic>;
using Color = Eigen::Vector<float, 3>; // todo global typedef
// todo cache and recompute cells/points on frame (only if changed) in a system.
tc::Group group;
VectorRf root;
Points points;
Hull<Grade> hull;
std::vector<char> enabled;
std::vector<Eigen::Vector3f> colors;
Affine transform = Affine::Identity();
explicit Structure(
tc::Group group,
VectorRf root,
Color color_ = Color::Ones()
) :
group(group),
root(root),
points(group, root), // todo separate system
hull(group), // todo separate system
enabled(hull.tilings.size(), true),
colors(hull.tilings.size(), color_),
transform(Affine::Identity()) {
}
};
template<typename Str_>
struct VBOs {
using Str = Str_;
using VectorDf = Str::VectorDf;
using Color = Str::Color;
using ArrayGui = Str::ArrayGui;
struct Uniform {
Eigen::Matrix4f linear;
Eigen::Vector4f translation;
};
struct Command {
unsigned int count, instanceCount, first, baseInstance;
};
cgl::Buffer<VectorDf> vertices;
cgl::Buffer<Color> colors;
cgl::Buffer<ArrayGui> indices;
cgl::Buffer<Uniform> uniform;
cgl::Buffer<Command> commands;
};
}

View File

@@ -13,9 +13,10 @@
#include "util.hpp"
#include "mirror.hpp"
#include "comps.hpp"
#include "components.hpp"
#include "fmt/core.h"
#include "fmt/ranges.h"
#include "systems.hpp"
#include <shaders.hpp>
@@ -156,14 +157,13 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
auto entity = registry.create();
{
// todo move symbol and root to structure
// cache and recompute cells/points on frame (only if changed) in a system.
tc::Group group = tc::schlafli({5, 3, 3, 2});
Points points(group, vec5{0.80, 0.09, 0.09, 0.09, 0.09});
Hull<4> hull(group);
auto &structure = registry.emplace<Slice>(entity, std::move(points), std::move(hull));
auto &structure = registry.emplace<Slice>(
entity,
group,
vec5{0.80, 0.09, 0.09, 0.09, 0.09}
);
registry.emplace<vis::VBOs<Slice>>(entity);
structure.enabled[0] = false; // disable {0,1,2} cells

View File

@@ -14,60 +14,11 @@
#include "mirror.hpp"
#include "geometry.hpp"
#include "solver.hpp"
#include "components.hpp"
#include <shaders.hpp>
namespace vis {
template<int R_, int D_, int G_>
struct Structure {
static constexpr auto Rank = R_;
static constexpr auto Dim = D_;
static constexpr auto Grade = G_;
using Affine = Eigen::Transform<float, Dim, Eigen::Affine>;
using Vertex = Eigen::Vector<float, Dim>;
using Color = Eigen::Vector<float, 3>;
using Cell = Eigen::Array<unsigned int, Grade, 1>;
Points points;
Hull<Grade> hull;
std::vector<char> enabled;
std::vector<Eigen::Vector3f> colors;
Affine transform = Affine::Identity();
template<typename P, typename H>
explicit Structure(P &&points_, H &&hull_, Color color_ = Color::Ones()):
points(std::forward<P>(points_)),
hull(std::forward<H>(hull_)),
enabled(hull.tilings.size(), true),
colors(hull.tilings.size(), color_),
transform(Affine::Identity()) {
}
};
template<typename Str_>
struct VBOs {
using Str = Str_;
struct Uniform {
Eigen::Matrix4f linear;
Eigen::Vector4f translation;
};
struct Command {
unsigned int count, instanceCount, first, baseInstance;
};
cgl::Buffer<typename Str::Vertex> vertices;
cgl::Buffer<typename Str::Color> colors;
cgl::Buffer<typename Str::Cell> indices;
cgl::Buffer<Uniform> uniform;
cgl::Buffer<Command> commands;
};
template<typename Str>
void upload_structure(entt::registry &registry) {
auto view = registry.view<Str, VBOs<Str>>();
@@ -134,9 +85,9 @@ namespace vis {
pipe.stage(slice);
pipe.stage(solid);
vao.iformat(0, 4, GL_UNSIGNED_INT);
vao.format(1, 3, GL_FLOAT);
vao.iformat(0, Str::Grade, GL_UNSIGNED_INT); // inds
vao.format(1, 3, GL_FLOAT); // color
glVertexArrayBindingDivisor(vao, 1, 1);
}
@@ -146,16 +97,32 @@ namespace vis {
for (auto [entity, vbos]: view.each()) {
glBindProgramPipeline(pipe);
glBindBufferBase(GL_UNIFORM_BUFFER, 2, vbos.uniform);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbos.vertices);
glBindBufferBase(
GL_SHADER_STORAGE_BUFFER,
1,
vbos.vertices
);
glBindBufferBase(
GL_UNIFORM_BUFFER,
2,
vbos.uniform
);
glBindBuffer(
GL_DRAW_INDIRECT_BUFFER,
vbos.commands
);
vao.vertexBuffer(0, vbos.indices);
vao.vertexBuffer(1, vbos.colors);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vbos.commands);
glBindVertexArray(vao);
glMultiDrawArraysIndirect(GL_POINTS, nullptr, vbos.commands.count(), 0);
glMultiDrawArraysIndirect(
GL_POINTS,
nullptr,
vbos.commands.count(),
0
);
glBindVertexArray(0);
glBindProgramPipeline(0);