|
|
@@ -22,34 +22,81 @@
|
|
|
|
|
|
constexpr float deg_to_rad = (float) (3.141592653589793 / 180.0);
|
|
|
|
|
|
+/* Keys callbacks */
|
|
|
+Config config;
|
|
|
+ComputeShader cs_draw;
|
|
|
+Buffer particles_buffer;
|
|
|
+Buffer map_buffer;
|
|
|
+
|
|
|
+void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
|
|
|
+ Config new_conf = config;
|
|
|
+ if(action != GLFW_PRESS)
|
|
|
+ return;
|
|
|
+ char filename[100];
|
|
|
+ if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE
|
|
|
+ && (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)){
|
|
|
+ char pattern[] = "./configs/config%d.cfg";
|
|
|
+ sprintf(filename, pattern, key - GLFW_KEY_0);
|
|
|
+ if(load_config(std::string(filename), &new_conf)){
|
|
|
+ config = new_conf;
|
|
|
+ std::cout << "Loaded '" << filename << "'" << std::endl;
|
|
|
+ }else{
|
|
|
+ std::cout << "Failed to load '" << filename << "'" << std::endl;
|
|
|
+ }
|
|
|
+ }else if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS
|
|
|
+ && (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)){
|
|
|
+ char pattern[] = "./shaders/draw%d.glsl";
|
|
|
+ sprintf(filename, pattern, key - GLFW_KEY_0);
|
|
|
+ cs_draw.load(filename);
|
|
|
+ cs_draw.compile();
|
|
|
+ particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
|
|
|
+ map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
int main(int argc, char *argv[]){
|
|
|
srand(0);
|
|
|
std::string filename;
|
|
|
Application app;
|
|
|
- Config config;
|
|
|
ComputeShader cs_particles_update;
|
|
|
ComputeShader cs_map_update;
|
|
|
ComputeShader cs_map_reset;
|
|
|
- ComputeShader cs_draw;
|
|
|
- Buffer particles_buffer;
|
|
|
- Buffer map_buffer;
|
|
|
Canvas canvas;
|
|
|
|
|
|
- config = {
|
|
|
- .map_width=1500,
|
|
|
- .map_height=1200,
|
|
|
+ int map_width = 1500;
|
|
|
+ int map_height = 1200;
|
|
|
+ size_t population = 5000000;
|
|
|
+ if(argc > 2){
|
|
|
+ map_width = strtol(argv[1], nullptr, 0);
|
|
|
+ map_height = strtol(argv[2], nullptr, 0);
|
|
|
+ }
|
|
|
+ if(argc > 3){
|
|
|
+ population = strtol(argv[3], nullptr, 0);
|
|
|
+ }
|
|
|
|
|
|
- .population=5000000,
|
|
|
+ // Default value
|
|
|
+ config = {
|
|
|
+ .map_width=map_width,
|
|
|
+ .map_height=map_height,
|
|
|
|
|
|
+ .population=population,
|
|
|
+
|
|
|
.increase=10.0f,
|
|
|
.decay=0.5f,
|
|
|
.diffusion=5.0f,
|
|
|
-
|
|
|
+
|
|
|
.speed=60.0f,
|
|
|
.steering=180.0f * deg_to_rad,
|
|
|
.sensing_distance=3.0f,
|
|
|
.sensing_angle=45.0f * deg_to_rad,
|
|
|
};
|
|
|
+
|
|
|
+ Config new_conf = config;
|
|
|
+ if(load_config("./configs/config0.cfg", &new_conf)){
|
|
|
+ config = new_conf;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
float average_population = (float)config.population / (float)(config.map_width * config.map_height);
|
|
|
float average_pheromone = (float)config.increase * average_population;
|
|
|
|
|
|
@@ -72,7 +119,7 @@ int main(int argc, char *argv[]){
|
|
|
cs_map_reset.compile();
|
|
|
|
|
|
/* Load and compile draw shader */
|
|
|
- cs_draw.load("./shaders/draw.glsl");
|
|
|
+ cs_draw.load("./shaders/draw0.glsl");
|
|
|
cs_draw.compile();
|
|
|
|
|
|
/* Particles init */
|
|
|
@@ -119,6 +166,9 @@ int main(int argc, char *argv[]){
|
|
|
|
|
|
map_buffer.bind_base(cs_map_reset.get_program(), "map_buffer", 2);
|
|
|
|
|
|
+ /* Register key callbacks */
|
|
|
+ app.register_callback(key_callback);
|
|
|
+
|
|
|
/* Main loop */
|
|
|
float pervious_time;
|
|
|
float current_time = app.time();
|
|
|
@@ -148,14 +198,6 @@ int main(int argc, char *argv[]){
|
|
|
cs_map_update.set_uniform("dt", dt);
|
|
|
cs_map_update.execute(config.map_width, config.map_height, 16, 16);
|
|
|
ComputeShader::wait();
|
|
|
- // Particle bob;
|
|
|
- // particles_buffer.get(&bob, 1, sizeof(bob));
|
|
|
- // size_t i = floor(bob.pos_y) * config.map_width + floor(bob.pos_x);
|
|
|
- // map_buffer.get(map.data(), i+1, sizeof(map[0]));
|
|
|
- // ComputeShader::wait();
|
|
|
- // printf("Bob[%6f,%6f], Map[%ld] = {.pop=%d,.phe=%f}\n",
|
|
|
- // bob.pos_x, bob.pos_y, i, map[i].population, map[i].pheromone);
|
|
|
-
|
|
|
|
|
|
/* Draw map */
|
|
|
cs_draw.activate();
|
|
|
@@ -175,17 +217,8 @@ int main(int argc, char *argv[]){
|
|
|
key_current_status = app.key_status(GLFW_KEY_R);
|
|
|
if(key_current_status != key_previous_status
|
|
|
&& key_current_status == GLFW_RELEASE){
|
|
|
- printf("Reloading draw Shader\n");
|
|
|
- cs_draw.load("./shaders/draw.glsl");
|
|
|
- cs_draw.compile();
|
|
|
- particles_buffer.bind_base(cs_draw.get_program(), "particles_buffer", 1);
|
|
|
- map_buffer.bind_base(cs_draw.get_program(), "map_buffer", 2);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- cs_particles_update.release();
|
|
|
- particles_buffer.release();
|
|
|
-
|
|
|
|
|
|
return 0;
|
|
|
}
|