Merge pull request #10 from allemangD/mesh

Mesh
This commit is contained in:
2019-09-15 20:03:57 -04:00
committed by GitHub
7 changed files with 137 additions and 33 deletions

View File

@@ -12,6 +12,7 @@ add_executable(${PROJECT_NAME}
add_dependencies(cosets shaders)
target_link_libraries(${PROJECT_NAME}
PRIVATE glad glm glfw)
target_compile_options(${PROJECT_NAME} PRIVATE -O3)
add_executable(coxeter src/coxeter.cpp)

View File

@@ -13,14 +13,19 @@ using namespace std;
class CosetsWindow : public Window {
GLint program;
GLuint points_vao, edges_vao;
GLuint vert_vao, edge_vao, face_vao;
GLuint verts_buf;
std::vector<glm::vec4> verts_data;
std::vector<glm::vec4> vert_data;
GLuint edges_buf;
std::vector<int> edge_data;
GLuint faces_buf;
std::vector<int> face_data;
GLint u_proj, u_view, u_color;
public:
void init() override {
auto vs = build_shader_file(
@@ -35,34 +40,58 @@ public:
program = build_program("main", vs, fs);
const Mults &mults = schlafli<4>({3, 4, 3});
verts_data = vertices<4>(mults, {10, 1, 1, 1});
edge_data = edges<4>(mults);
u_proj = glGetUniformLocation(program, "proj");
u_view = glGetUniformLocation(program, "view");
u_color = glGetUniformLocation(program, "color");
const int N = 3;
const Mults &mults = schlafli<N>({5, 3});
vert_data = vertices<N>(mults, {10, 1, 1});
edge_data = edges<N>(mults);
face_data = faces<N>(mults);
glGenBuffers(1, &verts_buf);
glBindBuffer(GL_ARRAY_BUFFER, verts_buf);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * verts_data.size(), &verts_data[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * vert_data.size(), &vert_data[0], GL_STATIC_DRAW);
glGenBuffers(1, &edges_buf);
glBindBuffer(GL_ARRAY_BUFFER, edges_buf);
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * edge_data.size(), &edge_data[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &points_vao);
glBindVertexArray(points_vao);
glGenBuffers(1, &faces_buf);
glBindBuffer(GL_ARRAY_BUFFER, faces_buf);
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * face_data.size(), &face_data[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &vert_vao);
glBindVertexArray(vert_vao);
glBindBuffer(GL_ARRAY_BUFFER, verts_buf);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &edges_vao);
glBindVertexArray(edges_vao);
glGenVertexArrays(1, &edge_vao);
glBindVertexArray(edge_vao);
glBindBuffer(GL_ARRAY_BUFFER, verts_buf);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, edges_buf);
glGenVertexArrays(1, &face_vao);
glBindVertexArray(face_vao);
glBindBuffer(GL_ARRAY_BUFFER, verts_buf);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, faces_buf);
glBindVertexArray(0);
std::cout << "verts: " << vert_data.size() << std::endl;
std::cout << "vendor: " << glGetString(GL_VENDOR) << std::endl
<< "renderer: " << glGetString(GL_RENDERER) << std::endl
<< "version: " << glGetString(GL_VERSION) << std::endl
<< "shading version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
}
void render() override {
@@ -75,7 +104,7 @@ public:
const auto ax_1 = glm::vec3(0, 0, 1);
const auto ax_2 = glm::vec3(.5, 1, 0.2);
const auto t = glfwGetTime();
const auto angle = (float) t / 2;
const auto angle = (float) t / 5;
const float sc = 1.5f;
const float ar = (float) w / (float) h;
@@ -84,28 +113,38 @@ public:
const glm::mat4 view = glm::rotate(id, angle, ax_1) * glm::rotate(id, angle, ax_2);
glUseProgram(program);
glUniformMatrix4fv(1, 1, false, glm::value_ptr(proj));
glUniformMatrix4fv(2, 1, false, glm::value_ptr(view));
glUniformMatrix4fv(u_proj, 1, false, glm::value_ptr(proj));
glUniformMatrix4fv(u_view, 1, false, glm::value_ptr(view));
glEnable(GL_DEPTH_TEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPointSize(10.f);
glBindVertexArray(points_vao);
glUniform4f(0, 1, 1, 1, 1);
glDrawArrays(GL_POINTS, 0, verts_data.size());
glPointSize(10.0f);
glLineWidth(5.0f);
glBindVertexArray(edges_vao);
glUniform4f(0, 1, 1, 0, 1);
glBindVertexArray(vert_vao);
glUniform4f(u_color, 1, 0, 0, 1);
glDrawArrays(GL_POINTS, 0, vert_data.size());
glBindVertexArray(edge_vao);
glUniform4f(u_color, 1, 0, 0, 1);
glDrawElements(GL_LINES, edge_data.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(face_vao);
glCullFace(GL_BACK);
glUniform4f(u_color, 1, 1, 1, 1);
glDrawElements(GL_TRIANGLES, face_data.size(), GL_UNSIGNED_INT, 0);
swapbuffers();
}
void deinit() override {
glDeleteProgram(program);
glDeleteVertexArrays(1, &points_vao);
glDeleteVertexArrays(1, &vert_vao);
}
};

View File

@@ -1,4 +1,4 @@
#version 440 core
#version 400 core
uniform vec4 color;

View File

@@ -1,4 +1,4 @@
#version 440 core
#version 400 core
uniform mat4 proj;
uniform mat4 view;
@@ -13,7 +13,7 @@ void main(){
vec4 vert = view * pos;
/* stereographic projection */
vert = vec4(vert.xyw / (1 - vert.z), 1);
vert = vec4(vert.xyz / (1 - vert.w), 1);
screen = gl_Position = proj * vert;
}

View File

@@ -45,22 +45,70 @@ template<int N>
std::vector<int> edges(const Mults &mults) {
std::vector<int> res{};
Table *verts = solve<N>({}, mults);
Table *t_vert = solve<N>({}, mults);
int K = 1;
for (const auto &subgens : combinations(N, K)) {
Table *edge = solve(subgens, {}, mults);
for (const auto &subgens : combinations(N, 1)) {
Table *t_edge = solve(subgens, {}, mults);
std::vector<int> primitive = verts->apply_each(edge->words());
std::vector<int> edge = t_vert->apply_each(t_edge->words());
Table *cosets = solve<N>(subgens, mults);
Table *c_edge = solve<N>(subgens, mults);
for (const auto &coset : cosets->words()) {
for (const auto &e : primitive) {
res.push_back(verts->apply(e, coset));
for (const auto &coset : c_edge->words()) {
for (const auto &e : edge) {
res.push_back(t_vert->apply(e, coset));
}
}
}
return res;
}
template<int N>
std::vector<int> faces(const Mults &mults) {
std::vector<int> res{};
Table *t_vert = solve<N>({}, mults);
// for each *kind* of face
for (const auto &sg_face : combinations(N, 2)) {
Table *cs_face = solve<N>(sg_face, mults);
// for each *kind* of edge
for (const auto &sg_edge : combinations(sg_face, 1)) {
Table *cs_edge = solve(sg_face, sg_edge, mults);
// find the vertices of that edge
Table *t_edge = solve(sg_edge, {}, mults);
std::vector<int> edge = t_vert->apply_each(t_edge->words());
// for each face
for (const auto &c_face : cs_face->words()) {
// for each edge
for (const auto &c_edge : cs_edge->words()) {
if (c_edge.empty()) { continue; }
for (auto e : edge) {
e = t_vert->apply(e, c_edge);
e = t_vert->apply(e, c_face);
res.push_back(e);
}
res.push_back(t_vert->apply(0, c_face));
if (c_edge.size() & 1u)
std::swap(res[res.size() - 1], res[res.size() - 2]);
unsigned ro_si1 = (sg_face[0] + sg_face[1]);
unsigned flag = sg_edge[0] == sg_face[0];
unsigned n_mirrors = c_face.size();
if ((n_mirrors + flag + ro_si1) & 1u) {
std::swap(res[res.size() - 1], res[res.size() - 2]);
}
}
}
}
}
return res;
}

View File

@@ -77,3 +77,19 @@ std::vector<std::vector<int>> combinations(int N, int K) {
return combos;
}
std::vector<std::vector<int>> combinations(std::vector<int> N, int K) {
std::vector<std::vector<int>> inds = combinations(N.size(), K);
std::vector<std::vector<int>> res{};
for (const auto &combo: inds) {
std::vector<int> vals{};
vals.reserve(combo.size());
for (const auto &i: combo) {
vals.push_back(N[i]);
}
res.push_back(vals);
}
return res;
}

View File

@@ -34,7 +34,7 @@ public:
glfwWindowHint(GLFW_ALPHA_BITS, 8);
_window = glfwCreateWindow(640, 480, "GLFW App", nullptr, nullptr);
_window = glfwCreateWindow(1920, 1080, "GLFW App", nullptr, nullptr);
if (!_window) {
fprintf(stderr, "Failed to create window;");
glfwTerminate();