main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. int main(int argc, char *argv[]){
  22. srand(0);
  23. std::string filename;
  24. Application app;
  25. Config config;
  26. ComputeShader cs_particles_update;
  27. ComputeShader cs_map_update;
  28. ComputeShader cs_map_reset;
  29. ComputeShader cs_draw;
  30. Buffer particles_buffer;
  31. Buffer map_buffer;
  32. Canvas canvas;
  33. config = {
  34. .map_width=1500,
  35. .map_height=1200,
  36. .population=5000000,
  37. .increase=10.0f,
  38. .decay=0.5f,
  39. .diffusion=5.0f,
  40. .speed=60.0f,
  41. .steering=180.0f * deg_to_rad,
  42. .sensing_distance=3.0f,
  43. .sensing_angle=45.0f * deg_to_rad,
  44. };
  45. float average_population = (float)config.population / (float)(config.map_width * config.map_height);
  46. float average_pheromone = (float)config.increase * average_population;
  47. printf("Avg pop %08f - Avg phe %08f\n", average_population, average_pheromone);
  48. app = Application(config.map_width, config.map_height);
  49. app.init();
  50. app.create_window();
  51. /* Load and compile particles update shader */
  52. cs_particles_update.load("./shaders/particles_update.glsl");
  53. cs_particles_update.compile();
  54. /* Load and compile map update shader */
  55. cs_map_update.load("./shaders/map_update.glsl");
  56. cs_map_update.compile();
  57. /* Load and compile map reset shader */
  58. cs_map_reset.load("./shaders/map_reset.glsl");
  59. cs_map_reset.compile();
  60. /* Load and compile draw shader */
  61. cs_draw.load("./shaders/draw.glsl");
  62. cs_draw.compile();
  63. /* Particles init */
  64. std::vector<Particle> particles(config.population);
  65. for(auto &particle : particles){
  66. particle.pos_x = random_float(0.0f, config.map_width);
  67. particle.pos_y = random_float(0.0f, config.map_height);
  68. float angle = random_float(0, 360) * deg_to_rad;
  69. particle.dir_x = cosf(angle);
  70. particle.dir_y = sinf(angle);
  71. }
  72. /* Create and fill particles buffer */
  73. particles_buffer.create();
  74. particles_buffer.set(particles.data(), particles.size(), sizeof(particles[0]));
  75. /* Map init */
  76. std::vector<Cell> map(config.map_width * config.map_height);
  77. for(auto &cell : map){
  78. cell = {
  79. .pheromone = 0,
  80. .result = 0,
  81. .population = 0,
  82. };
  83. }
  84. /* Create and fill map buffer */
  85. map_buffer.create();
  86. map_buffer.set(map.data(), map.size(), sizeof(map[0]));
  87. /* Create Canvas */
  88. canvas = Canvas(config.map_width, config.map_width);
  89. canvas.create();
  90. /* Bind buffers to compute shaders */
  91. particles_buffer.bind_base(cs_particles_update.get_program(), "particles_buffer", 1);
  92. map_buffer.bind_base(cs_particles_update.get_program(), "map_buffer", 2);
  93. map_buffer.bind_base(cs_map_update.get_program(), "map_buffer", 2);
  94. particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
  95. map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
  96. map_buffer.bind_base(cs_map_reset.get_program(), "map_buffer", 2);
  97. /* Main loop */
  98. float pervious_time;
  99. float current_time = app.time();
  100. int key_previous_status;
  101. int key_current_status = GLFW_RELEASE;
  102. while(app.running()){
  103. pervious_time = current_time;
  104. current_time = app.time();
  105. float dt = current_time - pervious_time;
  106. /* Update uniforms */
  107. particle_uniform(config, cs_particles_update);
  108. map_update_uniform(config, cs_map_update);
  109. map_reset_uniform(config, cs_map_reset);
  110. cs_draw.set_uniform("map_width", config.map_width);
  111. cs_draw.set_uniform("map_height", config.map_height);
  112. cs_draw.set_uniform("display_population_value", average_population);
  113. cs_draw.set_uniform("display_pheromone_value", average_pheromone);
  114. /* Update particles */
  115. cs_particles_update.set_uniform("dt", dt);
  116. cs_particles_update.execute(config.population, 256);
  117. ComputeShader::wait();
  118. /* Update map */
  119. cs_map_update.set_uniform("dt", dt);
  120. cs_map_update.execute(config.map_width, config.map_height, 16, 16);
  121. ComputeShader::wait();
  122. // Particle bob;
  123. // particles_buffer.get(&bob, 1, sizeof(bob));
  124. // size_t i = floor(bob.pos_y) * config.map_width + floor(bob.pos_x);
  125. // map_buffer.get(map.data(), i+1, sizeof(map[0]));
  126. // ComputeShader::wait();
  127. // printf("Bob[%6f,%6f], Map[%ld] = {.pop=%d,.phe=%f}\n",
  128. // bob.pos_x, bob.pos_y, i, map[i].population, map[i].pheromone);
  129. /* Draw map */
  130. cs_draw.activate();
  131. canvas.bind();
  132. cs_draw.execute(config.map_width, config.map_height, 16, 16);
  133. ComputeShader::wait();
  134. canvas.draw();
  135. /* Reset map */
  136. cs_map_reset.execute(config.map_width * config.map_height, 256);
  137. ComputeShader::wait();
  138. /* Display, move on */
  139. app.update();
  140. key_previous_status = key_current_status;
  141. key_current_status = app.key_status(GLFW_KEY_R);
  142. if(key_current_status != key_previous_status
  143. && key_current_status == GLFW_RELEASE){
  144. printf("Reloading draw Shader\n");
  145. cs_draw.load("./shaders/draw.glsl");
  146. cs_draw.compile();
  147. particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
  148. map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
  149. }
  150. }
  151. cs_particles_update.release();
  152. particles_buffer.release();
  153. return 0;
  154. }