Simplify mesh class

This commit is contained in:
David Allemang
2022-02-16 22:19:03 -05:00
parent 89b9780f6c
commit 22976a9778
6 changed files with 139 additions and 237 deletions

View File

@@ -41,6 +41,11 @@ public:
return count;
}
template<typename T>
GLuint upload(const T& data, GLenum mode = GL_STATIC_DRAW) {
return upload(data.begin(), data.end(), mode);
}
~Buffer() {
// delete silently ignores 0.
glDeleteBuffers(1, &id);

View File

@@ -4,7 +4,7 @@
#include <Eigen/Eigen>
template<class T_>
template<typename T_>
struct Buffer;
template<class ...Fmt_>
@@ -66,18 +66,23 @@ struct AutoFormat {
};
template<>
struct AutoFormat<double> {
using Fmt = LFormat<double, GL_DOUBLE, 1>;
struct AutoFormat<GLdouble> {
using Fmt = LFormat<GLdouble, GL_DOUBLE, 1>;
};
template<>
struct AutoFormat<float> {
using Fmt = Format<float, GL_FLOAT, 1>;
struct AutoFormat<GLfloat> {
using Fmt = Format<GLfloat, GL_FLOAT, 1>;
};
template<>
struct AutoFormat<int> {
using Fmt = IFormat<int, GL_INT, 1>;
struct AutoFormat<GLint> {
using Fmt = IFormat<GLint, GL_INT, 1>;
};
template<>
struct AutoFormat<GLuint> {
using Fmt = IFormat<GLuint, GL_UNSIGNED_INT, 1>;
};
template<typename Scalar, int Dim>

View File

@@ -128,45 +128,23 @@ int run(GLFWwindow *window, ImGuiContext *context) {
Buffer<GLuint> ind_buf;
Buffer<Eigen::Vector3f> vert_buf;
Buffer<Eigen::Vector3f> vert2_buf;
Buffer<GLuint> ind4d_buf;
Buffer<Eigen::Vector4f> vert4d_buf;
VertexArray<Eigen::Vector3f> vao(vert_buf);
glVertexArrayElementBuffer(vao, ind_buf);
auto mesh = ml::CubeMesh(0.25f);
auto ind_data = mesh.cells();
auto ind_flat = ind_data.reshaped();
auto elements = ind_buf.upload(ind_flat.begin(), ind_flat.end(), GL_STATIC_DRAW);
// todo add <Dim, Rank> to DynamicMesh
// need to do weird piping because dynamicmesh returns dynamic-sized matrix, VertexArray requires a static-sized matrix
auto vert_data_dyn = mesh.points();
Eigen::Ref<Eigen::Matrix3Xf> vert_data(vert_data_dyn);
auto vert_flat = vert_data.colwise();
vert_buf.upload(vert_flat.begin(), vert_flat.end(), GL_STATIC_DRAW);
auto mesh2 = ml::CubeMesh(0.5f);
auto vert2_data_dyn = mesh2.points();
Eigen::Ref<Eigen::Matrix3Xf> vert2_data(vert2_data_dyn);
auto vert2_flat = vert2_data.colwise();
vert2_buf.upload(vert2_flat.begin(), vert2_flat.end(), GL_STATIC_DRAW);
auto mesh4d = ml::WireCubeMesh(4, 0.33f);
Buffer<GLuint> ind4d_buf;
Buffer<Eigen::Vector4f> vert4d_buf;
VertexArray<Eigen::Vector4f> vao4d(vert4d_buf);
glVertexArrayElementBuffer(vao4d, ind4d_buf);
auto ind4d_data = mesh4d.cells();
auto ind4d_flat = ind4d_data.reshaped();
auto elements4d = ind4d_buf.upload(ind4d_flat.begin(), ind4d_flat.end(), GL_STATIC_DRAW);
auto mesh = ml::make_cube(0.22f);
auto elements = (GLint) ind_buf.upload(mesh.cells.reshaped());
vert_buf.upload(mesh.points.colwise());
auto vert4d_data_dyn = mesh4d.points();
Eigen::Ref<Eigen::Matrix4Xf> vert4d_data(vert4d_data_dyn);
auto vert4d_flat = vert4d_data.colwise();
vert4d_buf.upload(vert4d_flat.begin(), vert4d_flat.end(), GL_STATIC_DRAW);
auto mesh4d = ml::make_cube_wire<4>(0.33f);
auto elements4d = (GLint) ind4d_buf.upload(mesh4d.cells.reshaped());
vert4d_buf.upload(mesh4d.points.colwise());
VertexShader vs(std::ifstream("res/shaders/main.vert.glsl"));
VertexShader vs4d(std::ifstream("res/shaders/4d.vert.glsl"));

View File

@@ -6,48 +6,40 @@
#include <ml/meshlib.hpp>
#include <ml/meshlib_json.hpp>
class Circle : public ml::MeshBase {
public:
PointsType::Scalar radius;
Circle(PointsType::Scalar radius) : radius(radius) {}
PointsType points() const override {
PointsType t(1, 32);
for (int i = 0; i < t.size(); ++i) {
t(i) = 6.28318f * (float) i / 32;
}
PointsType out(3, 32);
out.array().row(0) = t.array().sin();
out.array().row(1) = t.array().cos();
out.array().row(2).setZero();
return out;
auto make_circle(float radius, size_t npoints = 32) {
Eigen::Array<float, 1, Eigen::Dynamic> theta(1, npoints);
for (int i = 0; i < theta.size(); ++i) {
theta(i) = (float) M_PI * 2.0f * (float) i / (float) npoints;
}
CellsType cells() const override {
CellsType t(1, 31);
for (int i = 0; i < t.size(); ++i) {
t(i) = i;
}
Eigen::Array<float, 3, Eigen::Dynamic> points(3, npoints);
points.row(0) = theta.sin();
points.row(1) = theta.cos();
points.row(2).setZero();
CellsType out(3, 31);
out.array().row(0) = 0;
out.array().row(1) = t.array();
out.array().row(2) = t.array() + 1;
return out;
Eigen::Array<unsigned int, 1, Eigen::Dynamic> idx(1, npoints - 1);
for (int i = 0; i < idx.size(); ++i) {
idx(i) = i;
}
};
Eigen::Array<unsigned int, 3, Eigen::Dynamic> cells(3, npoints - 1);
cells.row(0) = 0;
cells.row(1) = idx;
cells.row(2) = idx + 1;
return ml::Mesh(points, cells);
}
int main() {
auto omesh = Circle(1.0f);
std::string path = "circle.pak";
ml::write(omesh, "circle.pak");
auto omesh = make_circle(1.0f);
using MT = decltype(omesh);
auto imesh = ml::read("circle.pak");
ml::write(omesh, std::ofstream(path, std::ios::out | std::ios::binary));
std::cout << "= points ===============" << std::endl;
std::cout << imesh.points() << std::endl;
std::cout << "= cells ================" << std::endl;
std::cout << imesh.cells() << std::endl;
auto imesh = ml::read<MT>(std::ifstream(path, std::ios::in | std::ios::binary));
std::cout << imesh.points << std::endl;
std::cout << imesh.cells << std::endl;
}