PenroseTiling.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * © 2020 Michael Percival <[email protected]>
  3. * See LICENSE file for copyright and license details.
  4. */
  5. // Adapted from https://github.com/mpizzzle/penrose
  6. //https://www.boost.org/doc/libs/1_72_0/libs/geometry/doc/html/geometry/reference/adapted/boost_polygon/point_data.html
  7. //#include <GL/glew.h>
  8. //#include <GLFW/glfw3.h>
  9. //#include <glm/glm.hpp>
  10. //#include <glm/gtx/rotate_vector.hpp>
  11. #include "StdInc.h"
  12. #include "PenroseTiling.h"
  13. //#include "shader.hpp"
  14. //#include "png_writer.hpp"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. //static const std::string file_name = "penrose.png";
  17. Point2D Point2D::operator * (float scale) const
  18. {
  19. return Point2D(x() * scale, y() * scale);
  20. }
  21. Point2D Point2D::operator + (const Point2D& other) const
  22. {
  23. return Point2D(x() + other.x(), y() + other.y());
  24. }
  25. Triangle::Triangle(bool t_123, const TIndices & inds):
  26. tiling(t_123),
  27. indices(inds)
  28. {}
  29. Point2D Point2D::rotated(float radians) const
  30. {
  31. float cosAngle = cos(radians);
  32. float sinAngle = sin(radians);
  33. // Apply rotation matrix transformation
  34. float newX = x() * cosAngle - y() * sinAngle;
  35. float newY = x() * sinAngle + y() * cosAngle;
  36. return Point2D(newX, newY);
  37. }
  38. /*
  39. Point2D rotatePoint(const Point2D& point, double radians, const Point2D& origin = Point2D(0, 0))
  40. {
  41. // Define a rotate_transformer: the first template argument `2` stands for 2D,
  42. // `float` is the coordinate type, and 2 is input and output dimension
  43. strategy::transform::rotate_transformer<boost::geometry::radian, float, 2, 2> rot(radians);
  44. Point2D rotatedPoint;
  45. rot.apply(point, rotatedPoint);
  46. //transform(point, rotatedPoint, rot);
  47. return rotatedPoint;
  48. }
  49. */
  50. void PenroseTiling::split(Triangle& p, std::vector<Point2D>& points,
  51. std::array<std::vector<uint32_t>, 5>& indices, uint32_t depth)
  52. {
  53. uint32_t s = points.size();
  54. TIndices& i = p.indices;
  55. const auto p2 = P2;
  56. if (depth > 0)
  57. {
  58. if (p.tiling ^ !p2)
  59. {
  60. points.push_back(Point2D((points[i[0]] * (1.0f - PHI) ) + (points[i[2]]) * PHI));
  61. points.push_back(Point2D((points[i[p2]] * (1.0f - PHI)) + (points[i[!p2]] * PHI)));
  62. Triangle t1(p2, TIndices({ i[(!p2) + 1], p2 ? i[2] : s, p2 ? s : i[1] }));
  63. Triangle t2(true, TIndices({ p2 ? i[1] : s, s + 1, p2 ? s : i[1] }));
  64. Triangle t3(false, TIndices({ s, s + 1, i[0] }));
  65. // FIXME: Make sure these are not destroyed when we leave the scope
  66. p.subTriangles = { &t1, &t2, &t3 };
  67. }
  68. else
  69. {
  70. points.push_back(Point2D((points[i[p2 * 2]] * (1.0f - PHI)) + (points[i[!p2]]) * PHI));
  71. Triangle t1(true, TIndices({ i[2], s, i[1] }));
  72. Triangle t2(false, TIndices({ i[(!p2) + 1], s, i[0] }));
  73. p.subTriangles = { &t1, &t2 };
  74. }
  75. for (auto& t : p.subTriangles)
  76. {
  77. if (depth == 1)
  78. {
  79. for (uint32_t k = 0; k < 3; ++k)
  80. {
  81. if (k != (t->tiling ^ !p2 ? 2 : 1))
  82. {
  83. indices[indices.size() - 1].push_back(t->indices[k]);
  84. indices[indices.size() - 1].push_back(t->indices[((k + 1) % 3)]);
  85. }
  86. }
  87. indices[t->tiling + (p.tiling ? 0 : 2)].insert(indices[t->tiling + (p.tiling ? 0 : 2)].end(), t->indices.begin(), t->indices.end());
  88. }
  89. // Split recursively
  90. split(*t, points, indices, depth - 1);
  91. }
  92. }
  93. return;
  94. }
  95. // TODO: Return something
  96. void PenroseTiling::generatePenroseTiling(size_t numZones, CRandomGenerator * rand)
  97. {
  98. float scale = 100.f / (numZones + 20); //TODO: Use it to initialize the large tile
  99. //static std::default_random_engine e(std::random_device{}());
  100. static std::uniform_real_distribution<> d(0, 1);
  101. /*
  102. std::vector<glm::vec3> colours = { glm::vec3(d(e), d(e), d(e)), glm::vec3(d(e), d(e), d(e)),
  103. glm::vec3(d(e), d(e), d(e)), glm::vec3(d(e), d(e), d(e)),
  104. glm::vec3(d(e), d(e), d(e)), glm::vec3(d(e), d(e), d(e)) };
  105. */
  106. float polyAngle = 360.0f / POLY;
  107. std::vector<Point2D> points = { Point2D(0.0f, 0.0f), Point2D(0.0f, 1.0f) };
  108. std::array<std::vector<uint32_t>, 5> indices;
  109. for (uint32_t i = 1; i < POLY; ++i)
  110. {
  111. Point2D next = points[i].rotated(polyAngle);
  112. points.push_back(next);
  113. }
  114. // TODO: Scale to unit square
  115. for (auto& p : points)
  116. {
  117. p.x(p.x() * scale * BASE_SIZE);
  118. }
  119. for (uint32_t i = 0; i < POLY; i++)
  120. {
  121. std::array<uint32_t, 2> p = { (i % (POLY + 1)) + 1, ((i + 1) % POLY) + 1 };
  122. Triangle t(true, TIndices({ 0, p[i & 1], p[!(i & 1)] }));
  123. split(t, points, indices, DEPTH);
  124. }
  125. // TODO: Return collection of triangles
  126. // TODO: Get center point of the triangle
  127. // Do not draw anything
  128. /*
  129. if(!glfwInit())
  130. {
  131. return -1;
  132. }
  133. glfwWindowHint(GLFW_SAMPLES, 4);
  134. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  135. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  136. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  137. GLFWwindow* window = glfwCreateWindow(window_w, window_h, "penrose", NULL, NULL);
  138. if(window == NULL) {
  139. glfwTerminate();
  140. return -1;
  141. }
  142. glfwMakeContextCurrent(window);
  143. glewExperimental=true;
  144. if (glewInit() != GLEW_OK) {
  145. return -1;
  146. }
  147. glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  148. uint32_t VAOs[5], VBO, EBOs[5];
  149. glGenVertexArrays(5, VAOs);
  150. glGenBuffers(1, &VBO);
  151. glGenBuffers(5, EBOs);
  152. glLineWidth(line_w);
  153. for (uint32_t i = 0; i < indices.size(); ++i) {
  154. glBindVertexArray(VAOs[i]);
  155. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  156. glBufferData(GL_ARRAY_BUFFER, points.size() * 4 * 2, &points[0], GL_STATIC_DRAW);
  157. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[i]);
  158. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices[i].size() * 4, &indices[i][0], GL_STATIC_DRAW);
  159. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
  160. glEnableVertexAttribArray(0);
  161. }
  162. uint32_t programID = Shader::loadShaders("vertex.vert", "fragment.frag");
  163. GLint paint = glGetUniformLocation(programID, "paint");
  164. while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 && paint != -1) {
  165. glViewport(-1.0 * (window_w / scale) * ((0.5 * scale) - 0.5), -1.0 * (window_h / scale) * ((0.5 * scale) - 0.5), window_w, window_h);
  166. glClearColor(colours.back().x, colours.back().y, colours.back().z, 1.0f);
  167. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  168. glUseProgram(programID);
  169. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  170. for (uint32_t i = 0; i < indices.size(); ++i) {
  171. glPolygonMode(GL_FRONT_AND_BACK, i < indices.size() - 1 ? GL_FILL : GL_LINE);
  172. glUniform3fv(paint, 1, &colours[i][0]);
  173. glBindVertexArray(VAOs[i]);
  174. glDrawElements(i < indices.size() - 1 ? GL_TRIANGLES : GL_LINES, indices[i].size(), GL_UNSIGNED_INT, 0);
  175. }
  176. glfwSwapBuffers(window);
  177. glfwPollEvents();
  178. }
  179. int frame_w, frame_h;
  180. glfwGetFramebufferSize(window, &frame_w, &frame_h);
  181. png_bytep* row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * frame_h);
  182. for (int y = 0; y < frame_h; ++y) {
  183. row_pointers[y] = (png_byte*) malloc((4 * sizeof(png_byte)) * frame_w);
  184. glReadPixels(0, y, frame_w, 1, GL_RGBA, GL_UNSIGNED_BYTE, row_pointers[y]);
  185. }
  186. PngWriter::write_png_file(file_name, frame_w, frame_h, row_pointers);
  187. return 0;
  188. */
  189. }
  190. VCMI_LIB_NAMESPACE_END