penrose

program for generating penrose tilings.
Log | Files | Refs | README | LICENSE

commit fa5c5ff1d17a8ba69838d6e657925b8839514b53
parent 5da842d23d4e7c01882fcd08af05c500fab96df3
Author: mpizzzle <m@michaelpercival.xyz>
Date:   Fri, 23 Oct 2020 18:05:17 +0100

minor cleanup (trying to reduce line-count)

Diffstat:
Mpenrose.cpp | 60++++++++++++++++++++++++++----------------------------------
1 file changed, 26 insertions(+), 34 deletions(-)

diff --git a/penrose.cpp b/penrose.cpp @@ -12,82 +12,75 @@ static const uint32_t window_w = 1920; static const uint32_t window_h = 1080; static const uint32_t depth = 6; +static const uint32_t tiling_type = 2; static const glm::vec4 primary(0.7f, 0.7f, 0.0f, 1.0f); static const glm::vec4 secondary(0.2f, 0.2f, 0.4f, 1.0f); static const glm::vec4 line(0.2f, 0.4f, 2.0f, 1.0f); static const glm::vec4 background(0.15f, 0.15f, 0.35f, 1.0f); -static const GLfloat phi = 1.0 / ((1.0 + sqrt(5.0)) / 2); +static const float phi = 1.0 / ((1.0 + sqrt(5.0)) / 2); struct triangle { bool t_123; std::array<uint32_t, 3> indices; - std::array<glm::vec2, 3> points; std::vector<triangle*> subtriangles; }; -void split(triangle& parent, std::vector<glm::vec2>& points, std::array<std::vector<uint32_t>, 3>& indices, uint32_t depth) { +void split(triangle& p, std::vector<glm::vec2>& points, std::array<std::vector<uint32_t>, 3>& indices, uint32_t depth) { uint32_t s = points.size(); - std::array<glm::vec2, 3>& p = parent.points; + std::array<uint32_t, 3>& i = p.indices; if (depth > 0) { - if (parent.t_123) { + if (p.t_123) { //distance d = √((x1 − x0)2 + (y1 − y0)2) //ratio t = dt / d, in this case dt = phi * d so t = phi (no need to calc d) //therefore, (xt, yt) = (((1 − phi) x0 + phi * x1),((1 − phi) y0 + phi * y1)) - points.push_back(glm::vec2(((1.0f - phi) * p[0].x) + (phi * p[2].x), ((1.0f - phi) * p[0].y) + (phi * p[2].y))); - points.push_back(glm::vec2(((1.0f - phi) * p[1].x) + (phi * p[0].x), ((1.0f - phi) * p[1].y) + (phi * p[0].y))); + points.push_back(glm::vec2(((1.0f - phi) * points[i[0]].x) + (phi * points[i[2]].x), ((1.0f - phi) * points[i[0]].y) + (phi * points[i[2]].y))); + points.push_back(glm::vec2(((1.0f - phi) * points[i[1]].x) + (phi * points[i[0]].x), ((1.0f - phi) * points[i[1]].y) + (phi * points[i[0]].y))); triangle t123_1; t123_1.t_123 = true; - t123_1.points = { parent.points[1], parent.points[2], points[s] }; - t123_1.indices = { parent.indices[1], parent.indices[2], s }; + t123_1.indices = { i[1], i[2], s }; triangle t123_2; t123_2.t_123 = true; - t123_2.points = { parent.points[1], points[s + 1], points[s] }; - t123_2.indices = { parent.indices[1], s + 1, s }; + t123_2.indices = { i[1], s + 1, s }; triangle t124; t124.t_123 = false; - t124.points = { points[s], points[s + 1], parent.points[0] }; - t124.indices = { s, s + 1, parent.indices[0] }; + t124.indices = { s, s + 1, i[0] }; - parent.subtriangles = { &t123_1, &t123_2, &t124 }; + p.subtriangles = { &t123_1, &t123_2, &t124 }; } else { - points.push_back(glm::vec2(((1.0f - phi) * p[2].x) + (phi * p[0].x), ((1.0f - phi) * p[2].y) + (phi * p[0].y))); + points.push_back(glm::vec2(((1.0f - phi) * points[i[2]].x) + (phi * points[i[0]].x), ((1.0f - phi) * points[i[2]].y) + (phi * points[i[0]].y))); triangle t123; t123.t_123 = true; - t123.points = { parent.points[2], points[s], parent.points[1] }; - t123.indices = { parent.indices[2], s, parent.indices[1] }; + t123.indices = { i[2], s, i[1] }; triangle t124; t124.t_123 = false; - t124.points = { parent.points[1], points[s], parent.points[0] }; - t124.indices = { parent.indices[1], s, parent.indices[0] }; + t124.indices = { i[1], s, i[0] }; - parent.subtriangles = { &t123, &t124 }; + p.subtriangles = { &t123, &t124 }; } - if (depth == 1) { - for (auto& tri : parent.subtriangles) { + for (auto& t : p.subtriangles) { + if (depth == 1) { for (uint32_t k = 0; k < 3; ++k) { - if (k != (tri->t_123 ? 2 : 1)) { - indices[2].push_back(tri->indices[k]); - indices[2].push_back(tri->indices[((k + 1) % 3)]); + if (k != (t->t_123 ? 2 : 1)) { + indices[2].push_back(t->indices[k]); + indices[2].push_back(t->indices[((k + 1) % 3)]); } } - indices[tri->t_123].insert(indices[tri->t_123].end(), tri->indices.begin(), tri->indices.end()); + indices[t->t_123].insert(indices[t->t_123].end(), t->indices.begin(), t->indices.end()); } - } - for (auto& tri : parent.subtriangles) { - split(*tri, points, indices, depth - 1); + split(*t, points, indices, depth - 1); } } @@ -96,7 +89,7 @@ void split(triangle& parent, std::vector<glm::vec2>& points, std::array<std::vec int main() { uint32_t poly = 10; - GLfloat poly_angle = glm::radians(360.0f / poly); + float poly_angle = glm::radians(360.0f / poly); std::vector<glm::vec2> points = { glm::vec2(0.0f, 0.0f), glm::vec2(0.0f, 1.0f) }; std::array<std::vector<uint32_t>, 3> indices; @@ -117,7 +110,6 @@ int main() { triangle t; t.t_123 = true; - t.points = { points[0], points[p[0]], points[p[1]] }; t.indices = { 0, p[0], p[1] }; split(t, points, indices, depth); @@ -149,7 +141,7 @@ int main() { glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); - GLuint VAOs[3], VBO, EBOs[3]; + uint32_t VAOs[3], VBO, EBOs[3]; glGenVertexArrays(3, VAOs); glGenBuffers(1, &VBO); @@ -164,11 +156,11 @@ int main() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[i]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices[i].size() * 4, &indices[i][0], GL_STATIC_DRAW); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void*)0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); } - GLuint programID = Shader::loadShaders("vertex.vert", "fragment.frag"); + uint32_t programID = Shader::loadShaders("vertex.vert", "fragment.frag"); GLint paint = glGetUniformLocation(programID, "paint"); while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 && paint != -1) {