💿🐜 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.

1252 lines
52 KiB

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