💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1319 lines
54 KiB

3 years ago
  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "animation/animation.hpp"
  20. #include "animation/animator.hpp"
  21. #include "animation/ease.hpp"
  22. #include "animation/screen-transition.hpp"
  23. #include "animation/timeline.hpp"
  24. #include "application.hpp"
  25. #include "debug/cli.hpp"
  26. #include "debug/console-commands.hpp"
  27. #include "debug/logger.hpp"
  28. #include "game/context.hpp"
  29. #include "gl/framebuffer.hpp"
  30. #include "gl/pixel-format.hpp"
  31. #include "gl/pixel-type.hpp"
  32. #include "gl/rasterizer.hpp"
  33. #include "gl/texture-2d.hpp"
  34. #include "gl/texture-filter.hpp"
  35. #include "gl/texture-wrapping.hpp"
  36. #include "gl/vertex-array.hpp"
  37. #include "gl/vertex-attribute-type.hpp"
  38. #include "gl/vertex-buffer.hpp"
  39. #include "renderer/material-flags.hpp"
  40. #include "renderer/material-property.hpp"
  41. #include "renderer/passes/bloom-pass.hpp"
  42. #include "renderer/passes/clear-pass.hpp"
  43. #include "renderer/passes/final-pass.hpp"
  44. #include "renderer/passes/material-pass.hpp"
  45. #include "renderer/passes/outline-pass.hpp"
  46. #include "renderer/passes/shadow-map-pass.hpp"
  47. #include "renderer/passes/sky-pass.hpp"
  48. #include "renderer/simple-render-pass.hpp"
  49. #include "renderer/vertex-attributes.hpp"
  50. #include "renderer/compositor.hpp"
  51. #include "renderer/renderer.hpp"
  52. #include "resources/config-file.hpp"
  53. #include "resources/resource-manager.hpp"
  54. #include "resources/resource-manager.hpp"
  55. #include "scene/scene.hpp"
  56. #include "game/states/loading.hpp"
  57. #include "entity/systems/behavior.hpp"
  58. #include "entity/systems/camera.hpp"
  59. #include "entity/systems/collision.hpp"
  60. #include "entity/systems/constraint.hpp"
  61. #include "entity/systems/control.hpp"
  62. #include "entity/systems/locomotion.hpp"
  63. #include "entity/systems/nest.hpp"
  64. #include "entity/systems/snapping.hpp"
  65. #include "entity/systems/render.hpp"
  66. #include "entity/systems/samara.hpp"
  67. #include "entity/systems/subterrain.hpp"
  68. #include "entity/systems/terrain.hpp"
  69. #include "entity/systems/tool.hpp"
  70. #include "entity/systems/ui.hpp"
  71. #include "entity/systems/vegetation.hpp"
  72. #include "entity/systems/spatial.hpp"
  73. #include "entity/systems/tracking.hpp"
  74. #include "entity/systems/painting.hpp"
  75. #include "entity/systems/astronomy.hpp"
  76. #include "entity/systems/blackbody.hpp"
  77. #include "entity/systems/atmosphere.hpp"
  78. #include "entity/systems/orbit.hpp"
  79. #include "entity/systems/proteome.hpp"
  80. #include "entity/components/marker.hpp"
  81. #include "entity/commands.hpp"
  82. #include "utility/paths.hpp"
  83. #include "event/event-dispatcher.hpp"
  84. #include "input/event-router.hpp"
  85. #include "input/mapper.hpp"
  86. #include "input/listener.hpp"
  87. #include "input/game-controller.hpp"
  88. #include "input/mouse.hpp"
  89. #include "input/keyboard.hpp"
  90. #include "pheromone-matrix.hpp"
  91. #include "configuration.hpp"
  92. #include "input/scancode.hpp"
  93. #include <cxxopts.hpp>
  94. #include <dirent.h>
  95. #include <entt/entt.hpp>
  96. #include <filesystem>
  97. #include <functional>
  98. #include <string>
  99. #include <vector>
  100. #include "utility/timestamp.hpp"
  101. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  102. static void parse_options(game::context* ctx, int argc, char** argv);
  103. static void setup_resources(game::context* ctx);
  104. static void load_config(game::context* ctx);
  105. static void load_strings(game::context* ctx);
  106. static void setup_window(game::context* ctx);
  107. static void setup_rendering(game::context* ctx);
  108. static void setup_scenes(game::context* ctx);
  109. static void setup_animation(game::context* ctx);
  110. static void setup_entities(game::context* ctx);
  111. static void setup_systems(game::context* ctx);
  112. static void setup_controls(game::context* ctx);
  113. static void setup_cli(game::context* ctx);
  114. static void setup_callbacks(game::context* ctx);
  115. int bootloader(application* app, int argc, char** argv)
  116. {
  117. // Get application logger
  118. debug::logger* logger = app->get_logger();
  119. logger->push_task("Running application bootloader");
  120. // Allocate game context
  121. game::context* ctx = new game::context();
  122. ctx->app = app;
  123. ctx->logger = logger;
  124. // Init game context
  125. try
  126. {
  127. parse_options(ctx, argc, argv);
  128. setup_resources(ctx);
  129. load_config(ctx);
  130. load_strings(ctx);
  131. setup_window(ctx);
  132. setup_rendering(ctx);
  133. setup_scenes(ctx);
  134. setup_animation(ctx);
  135. setup_entities(ctx);
  136. setup_systems(ctx);
  137. setup_controls(ctx);
  138. setup_cli(ctx);
  139. setup_callbacks(ctx);
  140. }
  141. catch (const std::exception& e)
  142. {
  143. logger->error("Caught exception: \"" + std::string(e.what()) + "\"");
  144. logger->pop_task(EXIT_FAILURE);
  145. return EXIT_FAILURE;
  146. }
  147. logger->pop_task(EXIT_SUCCESS);
  148. // Setup initial application state
  149. application::state initial_state;
  150. initial_state.name = "loading";
  151. initial_state.enter = std::bind(game::state::loading::enter, ctx);
  152. initial_state.exit = std::bind(game::state::loading::exit, ctx);
  153. // Enter initial application state
  154. app->change_state(initial_state);
  155. return EXIT_SUCCESS;
  156. }
  157. void parse_options(game::context* ctx, int argc, char** argv)
  158. {
  159. debug::logger* logger = ctx->logger;
  160. logger->push_task("Parsing command line options");
  161. try
  162. {
  163. cxxopts::Options options("Antkeeper", "Ant colony simulation game");
  164. options.add_options()
  165. ("b,biome", "Selects the biome to load", cxxopts::value<std::string>())
  166. ("c,continue", "Continues from the last save")
  167. ("d,data", "Sets the data package path", cxxopts::value<std::string>())
  168. ("f,fullscreen", "Starts in fullscreen mode")
  169. ("n,new-game", "Starts a new game")
  170. ("q,quick-start", "Skips to the main menu")
  171. ("r,reset", "Restores all settings to default")
  172. ("v,vsync", "Enables or disables v-sync", cxxopts::value<int>())
  173. ("w,windowed", "Starts in windowed mode");
  174. auto result = options.parse(argc, argv);
  175. // --biome
  176. if (result.count("biome"))
  177. ctx->option_biome = result["biome"].as<std::string>();
  178. // --continue
  179. if (result.count("continue"))
  180. ctx->option_continue = true;
  181. // --data
  182. if (result.count("data"))
  183. ctx->option_data = result["data"].as<std::string>();
  184. // --fullscreen
  185. if (result.count("fullscreen"))
  186. ctx->option_fullscreen = true;
  187. // --new-game
  188. if (result.count("new-game"))
  189. ctx->option_new_game = true;
  190. // --quick-start
  191. if (result.count("quick-start"))
  192. ctx->option_quick_start = true;
  193. // --reset
  194. if (result.count("reset"))
  195. ctx->option_reset = true;
  196. // --vsync
  197. if (result.count("vsync"))
  198. ctx->option_vsync = (result["vsync"].as<int>()) ? true : false;
  199. // --windowed
  200. if (result.count("windowed"))
  201. ctx->option_windowed = true;
  202. }
  203. catch (const std::exception& e)
  204. {
  205. logger->error("Exception caught: \"" + std::string(e.what()) + "\"");
  206. logger->pop_task(EXIT_FAILURE);
  207. return;
  208. }
  209. logger->pop_task(EXIT_SUCCESS);
  210. }
  211. void setup_resources(game::context* ctx)
  212. {
  213. debug::logger* logger = ctx->logger;
  214. // Setup resource manager
  215. ctx->resource_manager = new resource_manager(logger);
  216. // Determine application name
  217. std::string application_name;
  218. #if defined(_WIN32) || defined(__APPLE__)
  219. application_name = "Antkeeper";
  220. #else
  221. application_name = "antkeeper";
  222. #endif
  223. // Detect paths
  224. ctx->data_path = get_data_path(application_name);
  225. ctx->config_path = get_config_path(application_name);
  226. ctx->mods_path = ctx->config_path + "mods/";
  227. ctx->saves_path = ctx->config_path + "saves/";
  228. ctx->screenshots_path = ctx->config_path + "screenshots/";
  229. // Log resource paths
  230. logger->log("Detected data path as \"" + ctx->data_path + "\"");
  231. logger->log("Detected config path as \"" + ctx->config_path + "\"");
  232. // Create nonexistent config directories
  233. std::vector<std::string> config_paths;
  234. config_paths.push_back(ctx->config_path);
  235. config_paths.push_back(ctx->mods_path);
  236. config_paths.push_back(ctx->saves_path);
  237. config_paths.push_back(ctx->screenshots_path);
  238. for (const std::string& path: config_paths)
  239. {
  240. if (!path_exists(path))
  241. {
  242. logger->push_task("Creating directory \"" + path + "\"");
  243. if (create_directory(path))
  244. {
  245. logger->pop_task(EXIT_SUCCESS);
  246. }
  247. else
  248. {
  249. logger->pop_task(EXIT_FAILURE);
  250. }
  251. }
  252. }
  253. // Redirect logger output to log file on non-debug builds
  254. #if defined(NDEBUG)
  255. std::string log_filename = config_path + "log.txt";
  256. ctx->log_filestream.open(log_filename.c_str());
  257. ctx->log_filestream << logger->get_history();
  258. logger->redirect(&log_filestream);
  259. #endif
  260. // Scan for mods
  261. std::vector<std::string> mods;
  262. struct dirent** files = nullptr;
  263. if (int n = scandir(ctx->mods_path.c_str(), &files, NULL, alphasort); n >= 0)
  264. {
  265. for (int i = 0; i < n; ++i)
  266. {
  267. struct dirent* file = files[i];
  268. switch (file->d_type)
  269. {
  270. case DT_REG:
  271. case DT_DIR:
  272. {
  273. std::string mod_name = file->d_name;
  274. // Skip hidden files and directories
  275. if (mod_name.front() == '.')
  276. break;
  277. mods.push_back(mod_name);
  278. }
  279. default:
  280. break;
  281. }
  282. }
  283. }
  284. // Determine data package path
  285. if (ctx->option_data.has_value())
  286. {
  287. ctx->data_package_path = ctx->option_data.value();
  288. if (std::filesystem::path(ctx->data_package_path).is_relative())
  289. ctx->data_package_path = ctx->data_path + ctx->data_package_path;
  290. }
  291. else
  292. {
  293. ctx->data_package_path = ctx->data_path + "data.zip";
  294. }
  295. // Mount mods
  296. for (const std::string& mod_name: mods)
  297. ctx->resource_manager->mount(ctx->mods_path + mod_name);
  298. // Mount config path
  299. ctx->resource_manager->mount(ctx->config_path);
  300. // Mount data package
  301. ctx->resource_manager->mount(ctx->data_package_path);
  302. // Include resource search paths in order of priority
  303. ctx->resource_manager->include("/shaders/");
  304. ctx->resource_manager->include("/models/");
  305. ctx->resource_manager->include("/images/");
  306. ctx->resource_manager->include("/textures/");
  307. ctx->resource_manager->include("/materials/");
  308. ctx->resource_manager->include("/entities/");
  309. ctx->resource_manager->include("/behaviors/");
  310. ctx->resource_manager->include("/controls/");
  311. ctx->resource_manager->include("/localization/");
  312. ctx->resource_manager->include("/biomes/");
  313. ctx->resource_manager->include("/traits/");
  314. ctx->resource_manager->include("/");
  315. }
  316. void load_config(game::context* ctx)
  317. {
  318. debug::logger* logger = ctx->logger;
  319. logger->push_task("Loading config");
  320. // Load config file
  321. ctx->config = ctx->resource_manager->load<config_file>("config.txt");
  322. if (!ctx->config)
  323. {
  324. logger->pop_task(EXIT_FAILURE);
  325. return;
  326. }
  327. logger->pop_task(EXIT_SUCCESS);
  328. }
  329. void load_strings(game::context* ctx)
  330. {
  331. debug::logger* logger = ctx->logger;
  332. logger->push_task("Loading strings");
  333. ctx->string_table = ctx->resource_manager->load<string_table>("strings.csv");
  334. build_string_table_map(&ctx->string_table_map, *ctx->string_table);
  335. ctx->language_code = ctx->config->get<std::string>("language");
  336. ctx->language_index = -1;
  337. for (int i = 2; i < (*ctx->string_table)[0].size(); ++i)
  338. {
  339. if ((*ctx->string_table)[0][i] == ctx->language_code)
  340. ctx->language_index = i;
  341. }
  342. logger->log("lang index: " + std::to_string(ctx->language_index));
  343. ctx->strings = &ctx->string_table_map[ctx->language_code];
  344. logger->pop_task(EXIT_SUCCESS);
  345. }
  346. void setup_window(game::context* ctx)
  347. {
  348. debug::logger* logger = ctx->logger;
  349. logger->push_task("Setting up window");
  350. application* app = ctx->app;
  351. config_file* config = ctx->config;
  352. // Set fullscreen or windowed mode
  353. bool fullscreen = true;
  354. if (ctx->option_fullscreen.has_value())
  355. fullscreen = true;
  356. else if (ctx->option_windowed.has_value())
  357. fullscreen = false;
  358. else if (config->has("fullscreen"))
  359. fullscreen = (config->get<int>("fullscreen") != 0);
  360. app->set_fullscreen(fullscreen);
  361. // Set resolution
  362. const auto& display_dimensions = ctx->app->get_display_dimensions();
  363. int2 resolution = {display_dimensions[0], display_dimensions[1]};
  364. if (fullscreen)
  365. {
  366. if (config->has("fullscreen_resolution"))
  367. resolution = config->get<int2>("fullscreen_resolution");
  368. }
  369. else
  370. {
  371. if (config->has("windowed_resolution"))
  372. resolution = config->get<int2>("windowed_resolution");
  373. }
  374. app->resize_window(resolution.x, resolution.y);
  375. // Set v-sync
  376. bool vsync = true;
  377. if (ctx->option_vsync.has_value())
  378. vsync = (ctx->option_vsync.value() != 0);
  379. else if (config->has("vsync"))
  380. vsync = (config->get<int>("vsync") != 0);
  381. app->set_vsync(vsync);
  382. // Set title
  383. app->set_title((*ctx->strings)["title"]);
  384. logger->pop_task(EXIT_SUCCESS);
  385. }
  386. void setup_rendering(game::context* ctx)
  387. {
  388. debug::logger* logger = ctx->logger;
  389. logger->push_task("Setting up rendering");
  390. // Get rasterizer from application
  391. ctx->rasterizer = ctx->app->get_rasterizer();
  392. // Get default framebuffer
  393. const gl::framebuffer& default_framebuffer = ctx->rasterizer->get_default_framebuffer();
  394. const auto& viewport_dimensions = default_framebuffer.get_dimensions();
  395. // Create HDR framebuffer (32F color, 32F depth)
  396. ctx->framebuffer_hdr_color = new gl::texture_2d(viewport_dimensions[0], viewport_dimensions[1], gl::pixel_type::float_32, gl::pixel_format::rgb);
  397. ctx->framebuffer_hdr_color->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  398. ctx->framebuffer_hdr_color->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  399. ctx->framebuffer_hdr_color->set_max_anisotropy(0.0f);
  400. ctx->framebuffer_hdr_depth = new gl::texture_2d(viewport_dimensions[0], viewport_dimensions[1], gl::pixel_type::float_32, gl::pixel_format::ds);
  401. ctx->framebuffer_hdr_depth->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  402. ctx->framebuffer_hdr_depth->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  403. ctx->framebuffer_hdr_depth->set_max_anisotropy(0.0f);
  404. ctx->framebuffer_hdr = new gl::framebuffer(viewport_dimensions[0], viewport_dimensions[1]);
  405. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::color, ctx->framebuffer_hdr_color);
  406. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::depth, ctx->framebuffer_hdr_depth);
  407. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::stencil, ctx->framebuffer_hdr_depth);
  408. // Create shadow map framebuffer
  409. int shadow_map_resolution = 4096;
  410. if (ctx->config->has("shadow_map_resolution"))
  411. {
  412. shadow_map_resolution = ctx->config->get<int>("shadow_map_resolution");
  413. }
  414. ctx->shadow_map_depth_texture = new gl::texture_2d(shadow_map_resolution, shadow_map_resolution, gl::pixel_type::float_32, gl::pixel_format::d);
  415. ctx->shadow_map_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  416. ctx->shadow_map_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  417. ctx->shadow_map_depth_texture->set_max_anisotropy(0.0f);
  418. ctx->shadow_map_framebuffer = new gl::framebuffer(shadow_map_resolution, shadow_map_resolution);
  419. ctx->shadow_map_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx->shadow_map_depth_texture);
  420. // Create bloom pingpong framebuffers (16F color, no depth)
  421. int bloom_width = viewport_dimensions[0] >> 1;
  422. int bloom_height = viewport_dimensions[1] >> 1;
  423. ctx->bloom_texture = new gl::texture_2d(bloom_width, bloom_height, gl::pixel_type::float_16, gl::pixel_format::rgb);
  424. ctx->bloom_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  425. ctx->bloom_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  426. ctx->bloom_texture->set_max_anisotropy(0.0f);
  427. ctx->framebuffer_bloom = new gl::framebuffer(bloom_width, bloom_height);
  428. ctx->framebuffer_bloom->attach(gl::framebuffer_attachment_type::color, ctx->bloom_texture);
  429. // Load blue noise texture
  430. gl::texture_2d* blue_noise_map = ctx->resource_manager->load<gl::texture_2d>("blue-noise.tex");
  431. // Load fallback material
  432. ctx->fallback_material = ctx->resource_manager->load<material>("fallback.mtl");
  433. // Setup overworld compositor
  434. ctx->overworld_shadow_map_clear_pass = new clear_pass(ctx->rasterizer, ctx->shadow_map_framebuffer);
  435. ctx->overworld_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  436. ctx->overworld_shadow_map_clear_pass->set_clear_depth(1.0f);
  437. ctx->overworld_shadow_map_pass = new shadow_map_pass(ctx->rasterizer, ctx->shadow_map_framebuffer, ctx->resource_manager);
  438. ctx->overworld_shadow_map_pass->set_split_scheme_weight(0.75f);
  439. ctx->overworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  440. ctx->overworld_clear_pass->set_cleared_buffers(true, true, true);
  441. ctx->overworld_clear_pass->set_clear_depth(0.0f);
  442. ctx->overworld_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  443. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->overworld_sky_pass);
  444. ctx->overworld_sky_pass->set_enabled(true);
  445. ctx->overworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  446. ctx->overworld_material_pass->set_fallback_material(ctx->fallback_material);
  447. ctx->overworld_material_pass->shadow_map_pass = ctx->overworld_shadow_map_pass;
  448. ctx->overworld_material_pass->shadow_map = ctx->shadow_map_depth_texture;
  449. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->overworld_material_pass);
  450. ctx->overworld_outline_pass = new outline_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  451. ctx->overworld_outline_pass->set_outline_width(0.25f);
  452. ctx->overworld_outline_pass->set_outline_color(float4{1.0f, 1.0f, 1.0f, 1.0f});
  453. ctx->overworld_outline_pass->set_enabled(false);
  454. ctx->overworld_bloom_pass = new bloom_pass(ctx->rasterizer, ctx->framebuffer_bloom, ctx->resource_manager);
  455. ctx->overworld_bloom_pass->set_source_texture(ctx->framebuffer_hdr_color);
  456. ctx->overworld_bloom_pass->set_brightness_threshold(1.0f);
  457. ctx->overworld_bloom_pass->set_blur_iterations(5);
  458. ctx->overworld_bloom_pass->set_enabled(true);
  459. ctx->overworld_final_pass = new ::final_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  460. ctx->overworld_final_pass->set_color_texture(ctx->framebuffer_hdr_color);
  461. ctx->overworld_final_pass->set_bloom_texture(ctx->bloom_texture);
  462. ctx->overworld_final_pass->set_blue_noise_texture(blue_noise_map);
  463. ctx->overworld_compositor = new compositor();
  464. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_clear_pass);
  465. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_pass);
  466. ctx->overworld_compositor->add_pass(ctx->overworld_clear_pass);
  467. ctx->overworld_compositor->add_pass(ctx->overworld_sky_pass);
  468. ctx->overworld_compositor->add_pass(ctx->overworld_material_pass);
  469. //ctx->overworld_compositor->add_pass(ctx->overworld_outline_pass);
  470. ctx->overworld_compositor->add_pass(ctx->overworld_bloom_pass);
  471. ctx->overworld_compositor->add_pass(ctx->overworld_final_pass);
  472. // Setup underworld compositor
  473. ctx->underworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  474. ctx->underworld_clear_pass->set_cleared_buffers(true, true, false);
  475. ctx->underworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  476. ctx->underworld_material_pass->set_fallback_material(ctx->fallback_material);
  477. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->underworld_material_pass);
  478. gl::shader_program* underworld_final_shader = ctx->resource_manager->load<gl::shader_program>("underground-final.glsl");
  479. ctx->underworld_final_pass = new simple_render_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), underworld_final_shader);
  480. ctx->underground_color_texture_property = ctx->underworld_final_pass->get_material()->add_property<const gl::texture_2d*>("color_texture");
  481. ctx->underground_color_texture_property->set_value(ctx->framebuffer_hdr_color);
  482. ctx->underworld_final_pass->get_material()->update_tweens();
  483. ctx->underworld_compositor = new compositor();
  484. ctx->underworld_compositor->add_pass(ctx->underworld_clear_pass);
  485. ctx->underworld_compositor->add_pass(ctx->underworld_material_pass);
  486. ctx->underworld_compositor->add_pass(ctx->underworld_final_pass);
  487. // Setup UI camera compositor
  488. ctx->ui_clear_pass = new clear_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer());
  489. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  490. ctx->ui_clear_pass->set_clear_depth(0.0f);
  491. ctx->ui_material_pass = new material_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  492. ctx->ui_material_pass->set_fallback_material(ctx->fallback_material);
  493. ctx->ui_compositor = new compositor();
  494. ctx->ui_compositor->add_pass(ctx->ui_clear_pass);
  495. ctx->ui_compositor->add_pass(ctx->ui_material_pass);
  496. // Create billboard VAO
  497. {
  498. const float billboard_vertex_data[] =
  499. {
  500. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  501. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  502. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  503. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  504. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  505. 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  506. };
  507. std::size_t billboard_vertex_size = 8;
  508. std::size_t billboard_vertex_stride = sizeof(float) * billboard_vertex_size;
  509. std::size_t billboard_vertex_count = 6;
  510. ctx->billboard_vbo = new gl::vertex_buffer(sizeof(float) * billboard_vertex_size * billboard_vertex_count, billboard_vertex_data);
  511. ctx->billboard_vao = new gl::vertex_array();
  512. ctx->billboard_vao->bind_attribute(VERTEX_POSITION_LOCATION, *ctx->billboard_vbo, 3, gl::vertex_attribute_type::float_32, billboard_vertex_stride, 0);
  513. ctx->billboard_vao->bind_attribute(VERTEX_TEXCOORD_LOCATION, *ctx->billboard_vbo, 2, gl::vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 3);
  514. ctx->billboard_vao->bind_attribute(VERTEX_BARYCENTRIC_LOCATION, *ctx->billboard_vbo, 3, gl::vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 5);
  515. }
  516. // Load marker albedo textures
  517. ctx->marker_albedo_textures = new gl::texture_2d*[8];
  518. ctx->marker_albedo_textures[0] = ctx->resource_manager->load<gl::texture_2d>("marker-clear-albedo.tex");
  519. ctx->marker_albedo_textures[1] = ctx->resource_manager->load<gl::texture_2d>("marker-yellow-albedo.tex");
  520. ctx->marker_albedo_textures[2] = ctx->resource_manager->load<gl::texture_2d>("marker-green-albedo.tex");
  521. ctx->marker_albedo_textures[3] = ctx->resource_manager->load<gl::texture_2d>("marker-blue-albedo.tex");
  522. ctx->marker_albedo_textures[4] = ctx->resource_manager->load<gl::texture_2d>("marker-purple-albedo.tex");
  523. ctx->marker_albedo_textures[5] = ctx->resource_manager->load<gl::texture_2d>("marker-pink-albedo.tex");
  524. ctx->marker_albedo_textures[6] = ctx->resource_manager->load<gl::texture_2d>("marker-red-albedo.tex");
  525. ctx->marker_albedo_textures[7] = ctx->resource_manager->load<gl::texture_2d>("marker-orange-albedo.tex");
  526. // Create renderer
  527. ctx->renderer = new renderer();
  528. ctx->renderer->set_billboard_vao(ctx->billboard_vao);
  529. logger->pop_task(EXIT_SUCCESS);
  530. }
  531. void setup_scenes(game::context* ctx)
  532. {
  533. debug::logger* logger = ctx->logger;
  534. logger->push_task("Setting up scenes");
  535. // Get default framebuffer
  536. const auto& viewport_dimensions = ctx->rasterizer->get_default_framebuffer().get_dimensions();
  537. float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  538. // Create infinite culling mask
  539. float inf = std::numeric_limits<float>::infinity();
  540. ctx->no_cull = {{-inf, -inf, -inf}, {inf, inf, inf}};
  541. // Setup overworld camera
  542. ctx->overworld_camera = new scene::camera();
  543. ctx->overworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  544. ctx->overworld_camera->set_compositor(ctx->overworld_compositor);
  545. ctx->overworld_camera->set_composite_index(0);
  546. ctx->overworld_camera->set_active(true);
  547. // Setup underworld camera
  548. ctx->underworld_camera = new scene::camera();
  549. ctx->underworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  550. ctx->underworld_camera->look_at({0, 50, 0}, {0, 0, 0}, {0, 0, -1});
  551. ctx->underworld_camera->set_compositor(ctx->underworld_compositor);
  552. ctx->underworld_camera->set_composite_index(0);
  553. ctx->underworld_camera->set_active(false);
  554. // Setup UI camera
  555. ctx->ui_camera = new scene::camera();
  556. ctx->ui_camera->set_compositor(ctx->ui_compositor);
  557. // Setup lights
  558. ctx->moon_light = new scene::directional_light();
  559. ctx->moon_light->set_intensity(0.0f);
  560. ctx->moon_light->update_tweens();
  561. ctx->subterrain_light = new scene::point_light();
  562. ctx->subterrain_light->set_color({1, 1, 1});
  563. ctx->subterrain_light->set_intensity(1.0f);
  564. ctx->subterrain_light->set_attenuation({1.0f, 0.09f, 0.032f});
  565. ctx->subterrain_light->update_tweens();
  566. ctx->underworld_ambient_light = new scene::ambient_light();
  567. ctx->underworld_ambient_light->set_color({1, 1, 1});
  568. ctx->underworld_ambient_light->set_intensity(0.1f);
  569. ctx->underworld_ambient_light->update_tweens();
  570. ctx->lens_spot_light = new scene::spot_light();
  571. ctx->lens_spot_light->set_color({1, 1, 1});
  572. ctx->lens_spot_light->set_intensity(20.0f);
  573. ctx->lens_spot_light->set_attenuation({1.0f, 0.0f, 0.0f});
  574. ctx->lens_spot_light->set_cutoff({math::radians(1.25f), math::radians(1.8f)});
  575. ctx->flashlight_spot_light = new scene::spot_light();
  576. ctx->flashlight_spot_light->set_color({1, 1, 1});
  577. ctx->flashlight_spot_light->set_intensity(1.0f);
  578. ctx->flashlight_spot_light->set_attenuation({1.0f, 0.0f, 0.0f});
  579. ctx->flashlight_spot_light->set_cutoff({math::radians(10.0f), math::radians(19.0f)});
  580. const gl::texture_2d* splash_texture = ctx->resource_manager->load<gl::texture_2d>("splash.tex");
  581. auto splash_dimensions = splash_texture->get_dimensions();
  582. ctx->splash_billboard_material = new material();
  583. ctx->splash_billboard_material->set_shader_program(ctx->resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  584. ctx->splash_billboard_material->add_property<const gl::texture_2d*>("background")->set_value(splash_texture);
  585. ctx->splash_billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  586. ctx->splash_billboard_material->update_tweens();
  587. ctx->splash_billboard = new scene::billboard();
  588. ctx->splash_billboard->set_material(ctx->splash_billboard_material);
  589. ctx->splash_billboard->set_scale({(float)std::get<0>(splash_dimensions) * 0.5f, (float)std::get<1>(splash_dimensions) * 0.5f, 1.0f});
  590. ctx->splash_billboard->set_translation({0.0f, 0.0f, 0.0f});
  591. ctx->splash_billboard->update_tweens();
  592. // Create depth debug billboard
  593. /*
  594. material* depth_debug_material = new material();
  595. depth_debug_material->set_shader_program(ctx->resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  596. depth_debug_material->add_property<const gl::texture_2d*>("background")->set_value(shadow_map_depth_texture);
  597. depth_debug_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  598. billboard* depth_debug_billboard = new billboard();
  599. depth_debug_billboard->set_material(depth_debug_material);
  600. depth_debug_billboard->set_scale({128, 128, 1});
  601. depth_debug_billboard->set_translation({-960 + 128, 1080 * 0.5f - 128, 0});
  602. depth_debug_billboard->update_tweens();
  603. ui_system->get_scene()->add_object(depth_debug_billboard);
  604. */
  605. // Setup overworld scene
  606. ctx->overworld_scene = new scene::collection();
  607. ctx->overworld_scene->add_object(ctx->overworld_camera);
  608. //ctx->overworld_scene->add_object(ctx->moon_light);
  609. //ctx->overworld_scene->add_object(ctx->spot_light);
  610. // Setup underworld scene
  611. ctx->underworld_scene = new scene::collection();
  612. ctx->underworld_scene->add_object(ctx->underworld_camera);
  613. ctx->underworld_scene->add_object(ctx->underworld_ambient_light);
  614. //ctx->underworld_scene->add_object(ctx->lantern);
  615. //ctx->underworld_scene->add_object(ctx->subterrain_light);
  616. //ctx->underworld_scene->add_object(ctx->portal_billboard);
  617. //model_instance* larva = new scene::model_instance(ctx->resource_manager->load<model>("larva.mdl"));
  618. //ctx->underworld_scene->add_object(larva);
  619. // Setup UI scene
  620. ctx->ui_scene = new scene::collection();
  621. ctx->ui_scene->add_object(ctx->ui_camera);
  622. //ctx->overworld_scene->add_object(ctx->lens_spot_light);
  623. ctx->underworld_scene->add_object(ctx->flashlight_spot_light);
  624. // Set overworld as active scene
  625. ctx->active_scene = ctx->overworld_scene;
  626. logger->pop_task(EXIT_SUCCESS);
  627. }
  628. void setup_animation(game::context* ctx)
  629. {
  630. // Setup timeline system
  631. ctx->timeline = new timeline();
  632. ctx->timeline->set_autoremove(true);
  633. // Setup animator
  634. ctx->animator = new animator();
  635. // Initialize time tween
  636. ctx->time_tween = new tween<double>(0.0);
  637. ctx->time_tween->set_interpolator(math::lerp<double, double>);
  638. // Create fade transition
  639. ctx->fade_transition = new screen_transition();
  640. ctx->fade_transition->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("fade-transition.glsl"));
  641. ctx->fade_transition_color = ctx->fade_transition->get_material()->add_property<float3>("color");
  642. ctx->fade_transition_color->set_value({0, 0, 0});
  643. ctx->ui_scene->add_object(ctx->fade_transition->get_billboard());
  644. ctx->animator->add_animation(ctx->fade_transition->get_animation());
  645. // Create inner radial transition
  646. ctx->radial_transition_inner = new screen_transition();
  647. ctx->radial_transition_inner->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("radial-transition-inner.glsl"));
  648. ctx->ui_scene->add_object(ctx->radial_transition_inner->get_billboard());
  649. ctx->animator->add_animation(ctx->radial_transition_inner->get_animation());
  650. // Create outer radial transition
  651. ctx->radial_transition_outer = new screen_transition();
  652. ctx->radial_transition_outer->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("radial-transition-outer.glsl"));
  653. ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard());
  654. ctx->animator->add_animation(ctx->radial_transition_outer->get_animation());
  655. // Setup tweens
  656. ctx->focal_point_tween = new tween<float3>();
  657. ctx->focal_point_tween->set_interpolator(math::lerp<float3, float>);
  658. // Set material pass tweens
  659. ctx->overworld_sky_pass->set_time_tween(ctx->time_tween);
  660. ctx->overworld_material_pass->set_time_tween(ctx->time_tween);
  661. ctx->overworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  662. ctx->overworld_final_pass->set_time_tween(ctx->time_tween);
  663. ctx->underworld_material_pass->set_time_tween(ctx->time_tween);
  664. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  665. ctx->underworld_final_pass->set_time_tween(ctx->time_tween);
  666. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  667. ctx->ui_material_pass->set_time_tween(ctx->time_tween);
  668. }
  669. void setup_entities(game::context* ctx)
  670. {
  671. // Create entity registry
  672. ctx->entity_registry = new entt::registry();
  673. // Reserve named entities
  674. ctx->brush_entity = ctx->entity_registry->create();
  675. ctx->flashlight_entity = ctx->entity_registry->create();
  676. ctx->forceps_entity = ctx->entity_registry->create();
  677. ctx->lens_entity = ctx->entity_registry->create();
  678. ctx->marker_entity = ctx->entity_registry->create();
  679. ctx->container_entity = ctx->entity_registry->create();
  680. ctx->twig_entity = ctx->entity_registry->create();
  681. ctx->focal_point_entity = ctx->entity_registry->create();
  682. }
  683. void setup_systems(game::context* ctx)
  684. {
  685. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  686. const auto& viewport_dimensions = ctx->app->get_viewport_dimensions();
  687. float4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
  688. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  689. const double3 rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  690. // Setup terrain system
  691. ctx->terrain_system = new entity::system::terrain(*ctx->entity_registry);
  692. ctx->terrain_system->set_patch_subdivisions(30);
  693. ctx->terrain_system->set_patch_scene_collection(ctx->overworld_scene);
  694. ctx->terrain_system->set_max_error(200.0);
  695. // Setup vegetation system
  696. //ctx->vegetation_system = new entity::system::vegetation(*ctx->entity_registry);
  697. //ctx->vegetation_system->set_terrain_patch_size(TERRAIN_PATCH_SIZE);
  698. //ctx->vegetation_system->set_vegetation_patch_resolution(VEGETATION_PATCH_RESOLUTION);
  699. //ctx->vegetation_system->set_vegetation_density(1.0f);
  700. //ctx->vegetation_system->set_vegetation_model(ctx->resource_manager->load<model>("grass-tuft.mdl"));
  701. //ctx->vegetation_system->set_scene(ctx->overworld_scene);
  702. // Setup camera system
  703. ctx->camera_system = new entity::system::camera(*ctx->entity_registry);
  704. ctx->camera_system->set_viewport(viewport);
  705. event_dispatcher->subscribe<mouse_moved_event>(ctx->camera_system);
  706. event_dispatcher->subscribe<window_resized_event>(ctx->camera_system);
  707. // Setup tool system
  708. ctx->tool_system = new entity::system::tool(*ctx->entity_registry, event_dispatcher);
  709. ctx->tool_system->set_camera(ctx->overworld_camera);
  710. ctx->tool_system->set_orbit_cam(ctx->camera_system->get_orbit_cam());
  711. ctx->tool_system->set_viewport(viewport);
  712. // Setup subterrain system
  713. ctx->subterrain_system = new entity::system::subterrain(*ctx->entity_registry, ctx->resource_manager);
  714. ctx->subterrain_system->set_scene(ctx->underworld_scene);
  715. // Setup nest system
  716. ctx->nest_system = new entity::system::nest(*ctx->entity_registry, ctx->resource_manager);
  717. // Setup collision system
  718. ctx->collision_system = new entity::system::collision(*ctx->entity_registry);
  719. // Setup samara system
  720. ctx->samara_system = new entity::system::samara(*ctx->entity_registry);
  721. // Setup snapping system
  722. ctx->snapping_system = new entity::system::snapping(*ctx->entity_registry);
  723. // Setup behavior system
  724. ctx->behavior_system = new entity::system::behavior(*ctx->entity_registry);
  725. // Setup locomotion system
  726. ctx->locomotion_system = new entity::system::locomotion(*ctx->entity_registry);
  727. // Setup pheromone system
  728. ctx->pheromones = new pheromone_matrix();
  729. ctx->pheromones->rows = 256;
  730. ctx->pheromones->columns = 256;
  731. ctx->pheromones->buffers = new float*[2];
  732. ctx->pheromones->buffers[0] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  733. ctx->pheromones->buffers[1] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  734. ctx->pheromones->current = 0;
  735. //diffuse(ctx->pheromones);
  736. // Setup spatial system
  737. ctx->spatial_system = new entity::system::spatial(*ctx->entity_registry);
  738. // Setup constraint system
  739. ctx->constraint_system = new entity::system::constraint(*ctx->entity_registry);
  740. // Setup tracking system
  741. ctx->tracking_system = new entity::system::tracking(*ctx->entity_registry, event_dispatcher, ctx->resource_manager);
  742. ctx->tracking_system->set_scene(ctx->overworld_scene);
  743. // Setup painting system
  744. ctx->painting_system = new entity::system::painting(*ctx->entity_registry, event_dispatcher, ctx->resource_manager);
  745. ctx->painting_system->set_scene(ctx->overworld_scene);
  746. // Setup solar system
  747. ctx->orbit_system = new entity::system::orbit(*ctx->entity_registry);
  748. // Setup blackbody system
  749. ctx->blackbody_system = new entity::system::blackbody(*ctx->entity_registry);
  750. ctx->blackbody_system->set_rgb_wavelengths(rgb_wavelengths_nm);
  751. // Setup atmosphere system
  752. ctx->atmosphere_system = new entity::system::atmosphere(*ctx->entity_registry);
  753. ctx->atmosphere_system->set_rgb_wavelengths(rgb_wavelengths_nm);
  754. // Setup astronomy system
  755. ctx->astronomy_system = new entity::system::astronomy(*ctx->entity_registry);
  756. ctx->astronomy_system->set_sky_pass(ctx->overworld_sky_pass);
  757. // Setup proteome system
  758. ctx->proteome_system = new entity::system::proteome(*ctx->entity_registry);
  759. // Set time scale
  760. float time_scale = 60.0f;
  761. if (ctx->config->has("time_scale"))
  762. {
  763. time_scale = ctx->config->get<float>("time_scale");
  764. }
  765. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  766. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  767. // Setup render system
  768. ctx->render_system = new entity::system::render(*ctx->entity_registry);
  769. ctx->render_system->add_layer(ctx->overworld_scene);
  770. ctx->render_system->add_layer(ctx->underworld_scene);
  771. ctx->render_system->add_layer(ctx->ui_scene);
  772. ctx->render_system->set_renderer(ctx->renderer);
  773. // Setup control system
  774. ctx->control_system = new entity::system::control(*ctx->entity_registry);
  775. ctx->control_system->set_viewport(viewport);
  776. ctx->control_system->set_underworld_camera(ctx->underworld_camera);
  777. ctx->control_system->set_tool(nullptr);
  778. //ctx->control_system->set_flashlight(flashlight, flashlight_light_cone);
  779. ctx->control_system->get_adjust_camera_control()->set_activated_callback([ctx](){ ctx->app->set_relative_mouse_mode(true); ctx->tool_system->set_pick(false); });
  780. ctx->control_system->get_adjust_camera_control()->set_deactivated_callback([ctx](){ ctx->app->set_relative_mouse_mode(false); ctx->tool_system->set_pick(true); });
  781. ctx->control_system->set_flashlight(ctx->flashlight_entity);
  782. ctx->control_system->set_camera_subject(ctx->focal_point_entity);
  783. ctx->control_system->set_camera_system(ctx->camera_system);
  784. event_dispatcher->subscribe<mouse_moved_event>(ctx->control_system);
  785. event_dispatcher->subscribe<window_resized_event>(ctx->control_system);
  786. // Setup UI system
  787. ctx->ui_system = new entity::system::ui(ctx->resource_manager);
  788. ctx->ui_system->set_camera(ctx->ui_camera);
  789. ctx->ui_system->set_scene(ctx->ui_scene);
  790. ctx->ui_system->set_viewport(viewport);
  791. ctx->ui_system->set_tool_menu_control(ctx->control_system->get_tool_menu_control());
  792. event_dispatcher->subscribe<mouse_moved_event>(ctx->ui_system);
  793. event_dispatcher->subscribe<window_resized_event>(ctx->ui_system);
  794. }
  795. void setup_controls(game::context* ctx)
  796. {
  797. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  798. // Setup input event routing
  799. ctx->input_event_router = new input::event_router();
  800. ctx->input_event_router->set_event_dispatcher(event_dispatcher);
  801. // Setup input mapper
  802. ctx->input_mapper = new input::mapper();
  803. ctx->input_mapper->set_event_dispatcher(event_dispatcher);
  804. // Setup input listener
  805. ctx->input_listener = new input::listener();
  806. ctx->input_listener->set_event_dispatcher(event_dispatcher);
  807. // Create toggle fullscreen control
  808. ctx->toggle_fullscreen_control = new input::control();
  809. ctx->toggle_fullscreen_control->set_activated_callback
  810. (
  811. [ctx]()
  812. {
  813. bool fullscreen = !ctx->app->is_fullscreen();
  814. ctx->app->set_fullscreen(fullscreen);
  815. if (!fullscreen)
  816. {
  817. int2 resolution = ctx->config->get<int2>("windowed_resolution");
  818. ctx->app->resize_window(resolution.x, resolution.y);
  819. }
  820. ctx->config->set<int>("fullscreen", (fullscreen) ? 1 : 0);
  821. }
  822. );
  823. // Create screenshot control
  824. ctx->screenshot_control = new input::control();
  825. ctx->screenshot_control->set_activated_callback
  826. (
  827. [ctx]()
  828. {
  829. std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png";
  830. ctx->app->save_frame(path);
  831. }
  832. );
  833. // Create menu back control
  834. ctx->menu_back_control = new input::control();
  835. ctx->menu_back_control->set_activated_callback
  836. (
  837. std::bind(&application::close, ctx->app, 0)
  838. );
  839. // Create menu select control
  840. ctx->menu_select_control = new input::control();
  841. // Create application control set
  842. ctx->application_controls = new input::control_set();
  843. ctx->application_controls->add_control(ctx->toggle_fullscreen_control);
  844. ctx->application_controls->add_control(ctx->screenshot_control);
  845. // Create menu control set
  846. ctx->menu_controls = new input::control_set();
  847. ctx->menu_controls->add_control(ctx->menu_back_control);
  848. ctx->menu_controls->add_control(ctx->menu_select_control);
  849. ctx->camera_controls = ctx->control_system->get_control_set();
  850. // Application control mappings
  851. ctx->input_event_router->add_mapping(input::key_mapping(ctx->toggle_fullscreen_control, nullptr, input::scancode::f11));
  852. ctx->input_event_router->add_mapping(input::key_mapping(ctx->screenshot_control, nullptr, input::scancode::f12));
  853. // Add menu control mappings
  854. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_back_control, nullptr, input::scancode::escape));
  855. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_back_control, nullptr, input::scancode::backspace));
  856. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->menu_back_control, nullptr, input::game_controller_button::b));
  857. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_tool_menu_control(), nullptr, input::scancode::left_shift));
  858. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_tool_menu_control(), nullptr, input::game_controller_button::x));
  859. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_select_control, nullptr, input::scancode::enter));
  860. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_select_control, nullptr, input::scancode::space));
  861. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_toggle_view_control(), nullptr, input::scancode::tab));
  862. ctx->control_system->get_toggle_view_control()->set_activated_callback(
  863. [ctx]()
  864. {
  865. if (ctx->active_scene == ctx->overworld_scene)
  866. {
  867. ctx->active_scene = ctx->underworld_scene;
  868. ctx->radial_transition_inner->transition(0.5f, false, ease<float, double>::in_quad);
  869. auto switch_cameras = [ctx]()
  870. {
  871. ctx->overworld_camera->set_active(false);
  872. ctx->underworld_camera->set_active(true);
  873. ctx->fade_transition->transition(0.25f, true, ease<float, double>::out_quad);
  874. };
  875. float t = ctx->timeline->get_position();
  876. ctx->timeline->add_cue({t + 0.5f, switch_cameras});
  877. }
  878. else
  879. {
  880. ctx->active_scene = ctx->overworld_scene;
  881. ctx->fade_transition->transition(0.25f, false, ease<float, double>::out_quad);
  882. auto switch_cameras = [ctx]()
  883. {
  884. ctx->overworld_camera->set_active(true);
  885. ctx->underworld_camera->set_active(false);
  886. ctx->radial_transition_inner->transition(0.5f, true, ease<float, double>::out_quad);
  887. };
  888. float t = ctx->timeline->get_position();
  889. ctx->timeline->add_cue({t + 0.25f, switch_cameras});
  890. }
  891. });
  892. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_forward_control(), nullptr, input::scancode::w));
  893. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_forward_control(), nullptr, input::game_controller_axis::left_y, true));
  894. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_back_control(), nullptr, input::scancode::s));
  895. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_back_control(), nullptr, input::game_controller_axis::left_y, false));
  896. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_left_control(), nullptr, input::scancode::a));
  897. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_left_control(), nullptr, input::game_controller_axis::left_x, true));
  898. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_right_control(), nullptr, input::scancode::d));
  899. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_right_control(), nullptr, input::game_controller_axis::left_x, false));
  900. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, input::game_controller_axis::right_x, false));
  901. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, input::game_controller_axis::right_x, true));
  902. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_tilt_up_control(), nullptr, input::game_controller_axis::right_y, false));
  903. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_tilt_down_control(), nullptr, input::game_controller_axis::right_y, true));
  904. ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->control_system->get_zoom_in_control(), nullptr, input::mouse_wheel_axis::positive_y));
  905. ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->control_system->get_zoom_out_control(), nullptr, input::mouse_wheel_axis::negative_y));
  906. ctx->input_event_router->add_mapping(input::mouse_button_mapping(ctx->control_system->get_adjust_camera_control(), nullptr, 3));
  907. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_ascend_control(), nullptr, input::game_controller_button::y));
  908. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_descend_control(), nullptr, input::game_controller_button::a));
  909. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_zoom_out_control(), nullptr, input::game_controller_axis::trigger_left, false));
  910. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_zoom_in_control(), nullptr, input::game_controller_axis::trigger_right, false));
  911. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, input::scancode::q));
  912. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, input::scancode::e));
  913. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_fast_forward_control(), nullptr, input::scancode::dot));
  914. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rewind_control(), nullptr, input::scancode::comma));
  915. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_exposure_increase_control(), nullptr, input::scancode::right_brace));
  916. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_exposure_decrease_control(), nullptr, input::scancode::left_brace));
  917. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_brush_control(), nullptr, input::scancode::one));
  918. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_twig_control(), nullptr, input::scancode::two));
  919. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_forceps_control(), nullptr, input::scancode::three));
  920. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_container_control(), nullptr, input::scancode::four));
  921. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_lens_control(), nullptr, input::scancode::five));
  922. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_marker_control(), nullptr, input::scancode::six));
  923. //ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_next_marker_control(), nullptr, input::scancode::right_brace));
  924. //ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_previous_marker_control(), nullptr, input::scancode::left_brace));
  925. ctx->input_event_router->add_mapping(input::mouse_button_mapping(ctx->control_system->get_use_tool_control(), nullptr, 1));
  926. ctx->control_system->get_use_tool_control()->set_activated_callback
  927. (
  928. [ctx]()
  929. {
  930. ctx->tool_system->set_tool_active(true);
  931. }
  932. );
  933. ctx->control_system->get_use_tool_control()->set_deactivated_callback
  934. (
  935. [ctx]()
  936. {
  937. ctx->tool_system->set_tool_active(false);
  938. }
  939. );
  940. ctx->control_system->get_equip_forceps_control()->set_activated_callback
  941. (
  942. [ctx]()
  943. {
  944. ctx->tool_system->set_active_tool(ctx->forceps_entity);
  945. }
  946. );
  947. ctx->control_system->get_equip_brush_control()->set_activated_callback
  948. (
  949. [ctx]()
  950. {
  951. ctx->tool_system->set_active_tool(ctx->brush_entity);
  952. }
  953. );
  954. ctx->control_system->get_equip_lens_control()->set_activated_callback
  955. (
  956. [ctx]()
  957. {
  958. ctx->tool_system->set_active_tool(ctx->lens_entity);
  959. }
  960. );
  961. ctx->control_system->get_equip_marker_control()->set_activated_callback
  962. (
  963. [ctx]()
  964. {
  965. ctx->tool_system->set_active_tool(ctx->marker_entity);
  966. }
  967. );
  968. ctx->control_system->get_equip_container_control()->set_activated_callback
  969. (
  970. [ctx]()
  971. {
  972. ctx->tool_system->set_active_tool(ctx->container_entity);
  973. }
  974. );
  975. ctx->control_system->get_equip_twig_control()->set_activated_callback
  976. (
  977. [ctx]()
  978. {
  979. ctx->tool_system->set_active_tool(ctx->twig_entity);
  980. }
  981. );
  982. ctx->control_system->get_next_marker_control()->set_activated_callback
  983. (
  984. [ctx]()
  985. {
  986. auto& marker = ctx->entity_registry->get<entity::component::marker>(ctx->marker_entity);
  987. marker.color = (marker.color + 1) % 8;
  988. const gl::texture_2d* marker_albedo_texture = ctx->marker_albedo_textures[marker.color];
  989. model* marker_model = ctx->render_system->get_model_instance(ctx->marker_entity)->get_model();
  990. for (::model_group* group: *marker_model->get_groups())
  991. {
  992. material_property_base* albedo_property = group->get_material()->get_property("albedo_texture");
  993. if (albedo_property)
  994. {
  995. static_cast<material_property<const gl::texture_2d*>*>(albedo_property)->set_value(marker_albedo_texture);
  996. }
  997. }
  998. }
  999. );
  1000. ctx->control_system->get_previous_marker_control()->set_activated_callback
  1001. (
  1002. [ctx]()
  1003. {
  1004. auto& marker = ctx->entity_registry->get<entity::component::marker>(ctx->marker_entity);
  1005. marker.color = (marker.color + 7) % 8;
  1006. const gl::texture_2d* marker_albedo_texture = ctx->marker_albedo_textures[marker.color];
  1007. model* marker_model = ctx->render_system->get_model_instance(ctx->marker_entity)->get_model();
  1008. for (::model_group* group: *marker_model->get_groups())
  1009. {
  1010. material_property_base* albedo_property = group->get_material()->get_property("albedo_texture");
  1011. if (albedo_property)
  1012. {
  1013. static_cast<material_property<const gl::texture_2d*>*>(albedo_property)->set_value(marker_albedo_texture);
  1014. }
  1015. }
  1016. }
  1017. );
  1018. float time_scale = ctx->config->get<float>("time_scale");
  1019. ctx->control_system->get_fast_forward_control()->set_activated_callback
  1020. (
  1021. [ctx, time_scale]()
  1022. {
  1023. ctx->orbit_system->set_time_scale(time_scale * 100.0f / seconds_per_day);
  1024. ctx->astronomy_system->set_time_scale(time_scale * 100.0f / seconds_per_day);
  1025. }
  1026. );
  1027. ctx->control_system->get_fast_forward_control()->set_deactivated_callback
  1028. (
  1029. [ctx, time_scale]()
  1030. {
  1031. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  1032. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  1033. }
  1034. );
  1035. ctx->control_system->get_rewind_control()->set_activated_callback
  1036. (
  1037. [ctx, time_scale]()
  1038. {
  1039. ctx->orbit_system->set_time_scale(time_scale * -100.0f / seconds_per_day);
  1040. ctx->astronomy_system->set_time_scale(time_scale * -100.0f / seconds_per_day);
  1041. }
  1042. );
  1043. ctx->control_system->get_rewind_control()->set_deactivated_callback
  1044. (
  1045. [ctx, time_scale]()
  1046. {
  1047. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  1048. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  1049. }
  1050. );
  1051. // Make lens tool's model instance unculled, so its shadow is always visible.
  1052. scene::model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  1053. if (lens_model_instance)
  1054. {
  1055. lens_model_instance->set_culling_mask(&ctx->no_cull);
  1056. }
  1057. }
  1058. void setup_cli(game::context* ctx)
  1059. {
  1060. ctx->cli = new debug::cli();
  1061. ctx->cli->register_command("echo", debug::cc::echo);
  1062. ctx->cli->register_command("exit", std::function<std::string()>(std::bind(&debug::cc::exit, ctx)));
  1063. ctx->cli->register_command("scrot", std::function<std::string()>(std::bind(&debug::cc::scrot, ctx)));
  1064. ctx->cli->register_command("cue", std::function<std::string(float, std::string)>(std::bind(&debug::cc::cue, ctx, std::placeholders::_1, std::placeholders::_2)));
  1065. //std::string cmd = "cue 20 exit";
  1066. //logger->log(cmd);
  1067. //logger->log(cli.interpret(cmd));
  1068. }
  1069. void setup_callbacks(game::context* ctx)
  1070. {
  1071. // Set update callback
  1072. ctx->app->set_update_callback
  1073. (
  1074. [ctx](double t, double dt)
  1075. {
  1076. // Update tweens
  1077. ctx->time_tween->update();
  1078. ctx->overworld_sky_pass->update_tweens();
  1079. ctx->overworld_scene->update_tweens();
  1080. ctx->underworld_scene->update_tweens();
  1081. ctx->ui_scene->update_tweens();
  1082. ctx->focal_point_tween->update();
  1083. ctx->underworld_final_pass->get_material()->update_tweens();
  1084. // Set time tween time
  1085. (*ctx->time_tween)[1] = t;
  1086. ctx->timeline->advance(dt);
  1087. ctx->control_system->update(t, dt);
  1088. ctx->terrain_system->update(t, dt);
  1089. //ctx->vegetation_system->update(t, dt);
  1090. ctx->snapping_system->update(t, dt);
  1091. ctx->nest_system->update(t, dt);
  1092. ctx->subterrain_system->update(t, dt);
  1093. ctx->collision_system->update(t, dt);
  1094. ctx->samara_system->update(t, dt);
  1095. ctx->behavior_system->update(t, dt);
  1096. ctx->locomotion_system->update(t, dt);
  1097. ctx->camera_system->update(t, dt);
  1098. ctx->tool_system->update(t, dt);
  1099. ctx->orbit_system->update(t, dt);
  1100. ctx->blackbody_system->update(t, dt);
  1101. ctx->atmosphere_system->update(t, dt);
  1102. ctx->astronomy_system->update(t, dt);
  1103. ctx->spatial_system->update(t, dt);
  1104. ctx->constraint_system->update(t, dt);
  1105. ctx->tracking_system->update(t, dt);
  1106. ctx->painting_system->update(t, dt);
  1107. ctx->proteome_system->update(t, dt);
  1108. //(*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point();
  1109. auto xf = entity::command::get_world_transform(*ctx->entity_registry, ctx->lens_entity);
  1110. //ctx->lens_spot_light->look_at(xf.translation, xf.translation + ctx->sun_direct->get_direction(), {0, 1, 0});
  1111. xf = entity::command::get_world_transform(*ctx->entity_registry, ctx->flashlight_entity);
  1112. //ctx->flashlight_spot_light->set_transform(xf);
  1113. ctx->flashlight_spot_light->look_at(xf.translation, xf.translation + xf.rotation * float3{0, 0, 1}, {0, 0, -1});
  1114. ctx->ui_system->update(dt);
  1115. ctx->render_system->update(t, dt);
  1116. ctx->animator->animate(dt);
  1117. ctx->application_controls->update();
  1118. ctx->menu_controls->update();
  1119. ctx->camera_controls->update();
  1120. }
  1121. );
  1122. // Set render callback
  1123. ctx->app->set_render_callback
  1124. (
  1125. [ctx](double alpha)
  1126. {
  1127. ctx->render_system->draw(alpha);
  1128. }
  1129. );
  1130. }