main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* std */
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <cmath>
  8. /* OpenGL */
  9. #include "glad/glad.h"
  10. #include <GLFW/glfw3.h>
  11. /* Project classes */
  12. #include "Helper.hpp"
  13. #include "Config.hpp"
  14. #include "Cell.hpp"
  15. #include "Particle.hpp"
  16. #include "ComputeShader.hpp"
  17. #include "Application.hpp"
  18. #include "Buffer.hpp"
  19. #include "Canvas.hpp"
  20. constexpr float deg_to_rad = (float) (3.141592653589793 / 180.0);
  21. /* Keys callbacks */
  22. Config config;
  23. ComputeShader cs_draw;
  24. Buffer particles_buffer;
  25. Buffer map_buffer;
  26. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  27. Config new_conf = config;
  28. if(action != GLFW_PRESS)
  29. return;
  30. char filename[100];
  31. if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE
  32. && (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)){
  33. char pattern[] = "./configs/config%d.cfg";
  34. sprintf(filename, pattern, key - GLFW_KEY_0);
  35. if(load_config(std::string(filename), &new_conf)){
  36. config = new_conf;
  37. std::cout << "Loaded '" << filename << "'" << std::endl;
  38. }else{
  39. std::cout << "Failed to load '" << filename << "'" << std::endl;
  40. }
  41. }else if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS
  42. && (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)){
  43. char pattern[] = "./shaders/draw%d.glsl";
  44. sprintf(filename, pattern, key - GLFW_KEY_0);
  45. cs_draw.load(filename);
  46. cs_draw.compile();
  47. particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
  48. map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
  49. }else if (key == GLFW_KEY_C){
  50. float distance = std::min(config.map_width, config.map_height) / 5;
  51. std::vector<Particle> particles(config.population);
  52. for(auto &particle : particles){
  53. float angle = random_float(0, 360) * deg_to_rad;
  54. particle.dir_x = cosf(angle);
  55. particle.dir_y = sinf(angle);
  56. particle.pos_x = config.map_width / 2 + particle.dir_x * distance;
  57. particle.pos_y = config.map_height / 2 + particle.dir_y * distance;
  58. }
  59. particles_buffer.set(particles.data(), particles.size(), sizeof(particles[0]));
  60. std::vector<Cell> map(config.map_width * config.map_height);
  61. for(auto &cell : map){
  62. cell = {
  63. .pheromone = 0,
  64. .result = 0,
  65. .population = 0,
  66. };
  67. }
  68. map_buffer.set(map.data(), map.size(), sizeof(map[0]));
  69. }
  70. }
  71. int main(int argc, char *argv[]){
  72. srand(0);
  73. std::string filename;
  74. Application app;
  75. ComputeShader cs_particles_update;
  76. ComputeShader cs_map_update;
  77. ComputeShader cs_map_reset;
  78. Canvas canvas;
  79. int map_width = 1500;
  80. int map_height = 800;
  81. size_t population = 5000000;
  82. if(argc > 2){
  83. map_width = strtol(argv[1], nullptr, 0);
  84. map_height = strtol(argv[2], nullptr, 0);
  85. }
  86. if(argc > 3){
  87. population = strtol(argv[3], nullptr, 0);
  88. }
  89. // Default value
  90. config = {
  91. .map_width=map_width,
  92. .map_height=map_height,
  93. .population=population,
  94. .increase=10.0f,
  95. .decay=0.5f,
  96. .diffusion=5.0f,
  97. .speed=60.0f,
  98. .steering=180.0f * deg_to_rad,
  99. .sensing_distance=3.0f,
  100. .sensing_angle=45.0f * deg_to_rad,
  101. };
  102. Config new_conf = config;
  103. if(load_config("./configs/config0.cfg", &new_conf)){
  104. config = new_conf;
  105. }
  106. float average_population = (float)config.population / (float)(config.map_width * config.map_height);
  107. float average_pheromone = (float)config.increase * average_population;
  108. app = Application(config.map_width, config.map_height);
  109. app.init();
  110. app.create_window();
  111. /* Load and compile particles update shader */
  112. cs_particles_update.load("./shaders/particles_update.glsl");
  113. cs_particles_update.compile();
  114. /* Load and compile map update shader */
  115. cs_map_update.load("./shaders/map_update.glsl");
  116. cs_map_update.compile();
  117. /* Load and compile map reset shader */
  118. cs_map_reset.load("./shaders/map_reset.glsl");
  119. cs_map_reset.compile();
  120. /* Load and compile draw shader */
  121. cs_draw.load("./shaders/draw0.glsl");
  122. cs_draw.compile();
  123. /* Particles init */
  124. std::vector<Particle> particles(config.population);
  125. for(auto &particle : particles){
  126. particle.pos_x = random_float(0.0f, config.map_width);
  127. particle.pos_y = random_float(0.0f, config.map_height);
  128. float angle = random_float(0, 360 * deg_to_rad);
  129. particle.dir_x = cosf(angle);
  130. particle.dir_y = sinf(angle);
  131. }
  132. /* Create and fill particles buffer */
  133. particles_buffer.create();
  134. particles_buffer.set(particles.data(), particles.size(), sizeof(particles[0]));
  135. /* Map init */
  136. std::vector<Cell> map(config.map_width * config.map_height);
  137. for(auto &cell : map){
  138. cell = {
  139. .pheromone = 0,
  140. .result = 0,
  141. .population = 0,
  142. };
  143. }
  144. /* Create and fill map buffer */
  145. map_buffer.create();
  146. map_buffer.set(map.data(), map.size(), sizeof(map[0]));
  147. /* Create Canvas */
  148. canvas = Canvas(config.map_width, config.map_width);
  149. canvas.create();
  150. /* Bind buffers to compute shaders */
  151. particles_buffer.bind_base(cs_particles_update.get_program(), "particles_buffer", 1);
  152. map_buffer.bind_base(cs_particles_update.get_program(), "map_buffer", 2);
  153. map_buffer.bind_base(cs_map_update.get_program(), "map_buffer", 2);
  154. particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
  155. map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
  156. map_buffer.bind_base(cs_map_reset.get_program(), "map_buffer", 2);
  157. /* Register key callbacks */
  158. app.register_callback(key_callback);
  159. /* Main loop */
  160. float pervious_time = app.time();
  161. while(app.running()){
  162. float tick = app.time();
  163. float dt = tick - pervious_time;
  164. pervious_time = tick;
  165. /* Update uniforms */
  166. particle_uniform(config, cs_particles_update);
  167. map_update_uniform(config, cs_map_update);
  168. map_reset_uniform(config, cs_map_reset);
  169. cs_draw.set_uniform("map_width", config.map_width);
  170. cs_draw.set_uniform("map_height", config.map_height);
  171. cs_draw.set_uniform("display_population_value", average_population);
  172. cs_draw.set_uniform("display_pheromone_value", average_pheromone);
  173. /* Update particles */
  174. cs_particles_update.set_uniform("dt", dt);
  175. cs_particles_update.execute(config.population, 256);
  176. ComputeShader::wait();
  177. /* Update map */
  178. cs_map_update.set_uniform("dt", dt);
  179. cs_map_update.execute(config.map_width, config.map_height, 16, 16);
  180. ComputeShader::wait();
  181. /* Draw map */
  182. cs_draw.activate();
  183. canvas.bind();
  184. cs_draw.execute(config.map_width, config.map_height, 16, 16);
  185. ComputeShader::wait();
  186. canvas.draw();
  187. canvas.unbind();
  188. /* Reset map */
  189. cs_map_reset.execute(config.map_width, config.map_height, 16, 16);
  190. ComputeShader::wait();
  191. /* Display, move on */
  192. app.update();
  193. }
  194. particles_buffer.release();
  195. map_buffer.release();
  196. canvas.release();
  197. cs_draw.release();
  198. cs_map_reset.release();
  199. cs_map_update.release();
  200. cs_particles_update.release();
  201. app.terminate();
  202. return 0;
  203. }