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

1047 lines
39 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/snapping.hpp"
  63. #include "entity/systems/render.hpp"
  64. #include "entity/systems/samara.hpp"
  65. #include "entity/systems/subterrain.hpp"
  66. #include "entity/systems/terrain.hpp"
  67. #include "entity/systems/tool.hpp"
  68. #include "entity/systems/ui.hpp"
  69. #include "entity/systems/vegetation.hpp"
  70. #include "entity/systems/spatial.hpp"
  71. #include "entity/systems/tracking.hpp"
  72. #include "entity/systems/painting.hpp"
  73. #include "entity/systems/astronomy.hpp"
  74. #include "entity/systems/blackbody.hpp"
  75. #include "entity/systems/atmosphere.hpp"
  76. #include "entity/systems/orbit.hpp"
  77. #include "entity/systems/proteome.hpp"
  78. #include "entity/components/marker.hpp"
  79. #include "entity/commands.hpp"
  80. #include "utility/paths.hpp"
  81. #include "event/event-dispatcher.hpp"
  82. #include "input/event-router.hpp"
  83. #include "input/mapper.hpp"
  84. #include "input/listener.hpp"
  85. #include "input/game-controller.hpp"
  86. #include "input/mouse.hpp"
  87. #include "input/keyboard.hpp"
  88. #include "configuration.hpp"
  89. #include "input/scancode.hpp"
  90. #include <cxxopts.hpp>
  91. #include <dirent.h>
  92. #include <entt/entt.hpp>
  93. #include <filesystem>
  94. #include <functional>
  95. #include <string>
  96. #include <vector>
  97. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  98. static void parse_options(game::context* ctx, int argc, char** argv);
  99. static void setup_resources(game::context* ctx);
  100. static void load_config(game::context* ctx);
  101. static void load_strings(game::context* ctx);
  102. static void setup_window(game::context* ctx);
  103. static void setup_rendering(game::context* ctx);
  104. static void setup_scenes(game::context* ctx);
  105. static void setup_animation(game::context* ctx);
  106. static void setup_entities(game::context* ctx);
  107. static void setup_systems(game::context* ctx);
  108. static void setup_controls(game::context* ctx);
  109. static void setup_cli(game::context* ctx);
  110. static void setup_callbacks(game::context* ctx);
  111. int bootloader(application* app, int argc, char** argv)
  112. {
  113. // Get application logger
  114. debug::logger* logger = app->get_logger();
  115. logger->push_task("Running application bootloader");
  116. // Allocate game context
  117. game::context* ctx = new game::context();
  118. ctx->app = app;
  119. ctx->logger = logger;
  120. // Init game context
  121. try
  122. {
  123. parse_options(ctx, argc, argv);
  124. setup_resources(ctx);
  125. load_config(ctx);
  126. load_strings(ctx);
  127. setup_window(ctx);
  128. setup_rendering(ctx);
  129. setup_scenes(ctx);
  130. setup_animation(ctx);
  131. setup_entities(ctx);
  132. setup_systems(ctx);
  133. setup_controls(ctx);
  134. setup_cli(ctx);
  135. setup_callbacks(ctx);
  136. }
  137. catch (const std::exception& e)
  138. {
  139. logger->error("Caught exception: \"" + std::string(e.what()) + "\"");
  140. logger->pop_task(EXIT_FAILURE);
  141. return EXIT_FAILURE;
  142. }
  143. logger->pop_task(EXIT_SUCCESS);
  144. // Setup initial application state
  145. application::state initial_state;
  146. initial_state.name = "loading";
  147. initial_state.enter = std::bind(game::state::loading::enter, ctx);
  148. initial_state.exit = std::bind(game::state::loading::exit, ctx);
  149. // Enter initial application state
  150. app->change_state(initial_state);
  151. return EXIT_SUCCESS;
  152. }
  153. void parse_options(game::context* ctx, int argc, char** argv)
  154. {
  155. debug::logger* logger = ctx->logger;
  156. logger->push_task("Parsing command line options");
  157. try
  158. {
  159. cxxopts::Options options("Antkeeper", "Ant colony simulation game");
  160. options.add_options()
  161. ("b,biome", "Selects the biome to load", cxxopts::value<std::string>())
  162. ("c,continue", "Continues from the last save")
  163. ("d,data", "Sets the data package path", cxxopts::value<std::string>())
  164. ("f,fullscreen", "Starts in fullscreen mode")
  165. ("n,new-game", "Starts a new game")
  166. ("q,quick-start", "Skips to the main menu")
  167. ("r,reset", "Restores all settings to default")
  168. ("v,vsync", "Enables or disables v-sync", cxxopts::value<int>())
  169. ("w,windowed", "Starts in windowed mode");
  170. auto result = options.parse(argc, argv);
  171. // --biome
  172. if (result.count("biome"))
  173. ctx->option_biome = result["biome"].as<std::string>();
  174. // --continue
  175. if (result.count("continue"))
  176. ctx->option_continue = true;
  177. // --data
  178. if (result.count("data"))
  179. ctx->option_data = result["data"].as<std::string>();
  180. // --fullscreen
  181. if (result.count("fullscreen"))
  182. ctx->option_fullscreen = true;
  183. // --new-game
  184. if (result.count("new-game"))
  185. ctx->option_new_game = true;
  186. // --quick-start
  187. if (result.count("quick-start"))
  188. ctx->option_quick_start = true;
  189. // --reset
  190. if (result.count("reset"))
  191. ctx->option_reset = true;
  192. // --vsync
  193. if (result.count("vsync"))
  194. ctx->option_vsync = (result["vsync"].as<int>()) ? true : false;
  195. // --windowed
  196. if (result.count("windowed"))
  197. ctx->option_windowed = true;
  198. }
  199. catch (const std::exception& e)
  200. {
  201. logger->error("Exception caught: \"" + std::string(e.what()) + "\"");
  202. logger->pop_task(EXIT_FAILURE);
  203. return;
  204. }
  205. logger->pop_task(EXIT_SUCCESS);
  206. }
  207. void setup_resources(game::context* ctx)
  208. {
  209. debug::logger* logger = ctx->logger;
  210. // Setup resource manager
  211. ctx->resource_manager = new resource_manager(logger);
  212. // Determine application name
  213. std::string application_name;
  214. #if defined(_WIN32) || defined(__APPLE__)
  215. application_name = "Antkeeper";
  216. #else
  217. application_name = "antkeeper";
  218. #endif
  219. // Detect paths
  220. ctx->data_path = get_data_path(application_name);
  221. ctx->config_path = get_config_path(application_name);
  222. ctx->mods_path = ctx->config_path + "mods/";
  223. ctx->saves_path = ctx->config_path + "saves/";
  224. ctx->screenshots_path = ctx->config_path + "screenshots/";
  225. // Log resource paths
  226. logger->log("Detected data path as \"" + ctx->data_path + "\"");
  227. logger->log("Detected config path as \"" + ctx->config_path + "\"");
  228. // Create nonexistent config directories
  229. std::vector<std::string> config_paths;
  230. config_paths.push_back(ctx->config_path);
  231. config_paths.push_back(ctx->mods_path);
  232. config_paths.push_back(ctx->saves_path);
  233. config_paths.push_back(ctx->screenshots_path);
  234. for (const std::string& path: config_paths)
  235. {
  236. if (!path_exists(path))
  237. {
  238. logger->push_task("Creating directory \"" + path + "\"");
  239. if (create_directory(path))
  240. {
  241. logger->pop_task(EXIT_SUCCESS);
  242. }
  243. else
  244. {
  245. logger->pop_task(EXIT_FAILURE);
  246. }
  247. }
  248. }
  249. // Redirect logger output to log file on non-debug builds
  250. #if defined(NDEBUG)
  251. std::string log_filename = config_path + "log.txt";
  252. ctx->log_filestream.open(log_filename.c_str());
  253. ctx->log_filestream << logger->get_history();
  254. logger->redirect(&log_filestream);
  255. #endif
  256. // Scan for mods
  257. std::vector<std::string> mods;
  258. struct dirent** files = nullptr;
  259. if (int n = scandir(ctx->mods_path.c_str(), &files, NULL, alphasort); n >= 0)
  260. {
  261. for (int i = 0; i < n; ++i)
  262. {
  263. struct dirent* file = files[i];
  264. switch (file->d_type)
  265. {
  266. case DT_REG:
  267. case DT_DIR:
  268. {
  269. std::string mod_name = file->d_name;
  270. // Skip hidden files and directories
  271. if (mod_name.front() == '.')
  272. break;
  273. mods.push_back(mod_name);
  274. }
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. // Determine data package path
  281. if (ctx->option_data.has_value())
  282. {
  283. ctx->data_package_path = ctx->option_data.value();
  284. if (std::filesystem::path(ctx->data_package_path).is_relative())
  285. ctx->data_package_path = ctx->data_path + ctx->data_package_path;
  286. }
  287. else
  288. {
  289. ctx->data_package_path = ctx->data_path + "data.zip";
  290. }
  291. // Mount mods
  292. for (const std::string& mod_name: mods)
  293. ctx->resource_manager->mount(ctx->mods_path + mod_name);
  294. // Mount config path
  295. ctx->resource_manager->mount(ctx->config_path);
  296. // Mount data package
  297. ctx->resource_manager->mount(ctx->data_package_path);
  298. // Include resource search paths in order of priority
  299. ctx->resource_manager->include("/shaders/");
  300. ctx->resource_manager->include("/models/");
  301. ctx->resource_manager->include("/images/");
  302. ctx->resource_manager->include("/textures/");
  303. ctx->resource_manager->include("/materials/");
  304. ctx->resource_manager->include("/entities/");
  305. ctx->resource_manager->include("/behaviors/");
  306. ctx->resource_manager->include("/controls/");
  307. ctx->resource_manager->include("/localization/");
  308. ctx->resource_manager->include("/biomes/");
  309. ctx->resource_manager->include("/traits/");
  310. ctx->resource_manager->include("/");
  311. }
  312. void load_config(game::context* ctx)
  313. {
  314. debug::logger* logger = ctx->logger;
  315. logger->push_task("Loading config");
  316. // Load config file
  317. ctx->config = ctx->resource_manager->load<config_file>("config.txt");
  318. if (!ctx->config)
  319. {
  320. logger->pop_task(EXIT_FAILURE);
  321. return;
  322. }
  323. logger->pop_task(EXIT_SUCCESS);
  324. }
  325. void load_strings(game::context* ctx)
  326. {
  327. debug::logger* logger = ctx->logger;
  328. logger->push_task("Loading strings");
  329. ctx->string_table = ctx->resource_manager->load<string_table>("strings.csv");
  330. build_string_table_map(&ctx->string_table_map, *ctx->string_table);
  331. ctx->language_code = ctx->config->get<std::string>("language");
  332. ctx->language_index = -1;
  333. for (int i = 2; i < (*ctx->string_table)[0].size(); ++i)
  334. {
  335. if ((*ctx->string_table)[0][i] == ctx->language_code)
  336. ctx->language_index = i;
  337. }
  338. logger->log("lang index: " + std::to_string(ctx->language_index));
  339. ctx->strings = &ctx->string_table_map[ctx->language_code];
  340. logger->pop_task(EXIT_SUCCESS);
  341. }
  342. void setup_window(game::context* ctx)
  343. {
  344. debug::logger* logger = ctx->logger;
  345. logger->push_task("Setting up window");
  346. application* app = ctx->app;
  347. config_file* config = ctx->config;
  348. // Set fullscreen or windowed mode
  349. bool fullscreen = true;
  350. if (ctx->option_fullscreen.has_value())
  351. fullscreen = true;
  352. else if (ctx->option_windowed.has_value())
  353. fullscreen = false;
  354. else if (config->has("fullscreen"))
  355. fullscreen = (config->get<int>("fullscreen") != 0);
  356. app->set_fullscreen(fullscreen);
  357. // Set resolution
  358. const auto& display_dimensions = ctx->app->get_display_dimensions();
  359. int2 resolution = {display_dimensions[0], display_dimensions[1]};
  360. if (fullscreen)
  361. {
  362. if (config->has("fullscreen_resolution"))
  363. resolution = config->get<int2>("fullscreen_resolution");
  364. }
  365. else
  366. {
  367. if (config->has("windowed_resolution"))
  368. resolution = config->get<int2>("windowed_resolution");
  369. }
  370. app->resize_window(resolution.x, resolution.y);
  371. // Set v-sync
  372. bool vsync = true;
  373. if (ctx->option_vsync.has_value())
  374. vsync = (ctx->option_vsync.value() != 0);
  375. else if (config->has("vsync"))
  376. vsync = (config->get<int>("vsync") != 0);
  377. app->set_vsync(vsync);
  378. // Set title
  379. app->set_title((*ctx->strings)["title"]);
  380. logger->pop_task(EXIT_SUCCESS);
  381. }
  382. void setup_rendering(game::context* ctx)
  383. {
  384. debug::logger* logger = ctx->logger;
  385. logger->push_task("Setting up rendering");
  386. // Get rasterizer from application
  387. ctx->rasterizer = ctx->app->get_rasterizer();
  388. // Get default framebuffer
  389. const gl::framebuffer& default_framebuffer = ctx->rasterizer->get_default_framebuffer();
  390. const auto& viewport_dimensions = default_framebuffer.get_dimensions();
  391. // Create HDR framebuffer (32F color, 32F depth)
  392. ctx->framebuffer_hdr_color = new gl::texture_2d(viewport_dimensions[0], viewport_dimensions[1], gl::pixel_type::float_32, gl::pixel_format::rgb);
  393. ctx->framebuffer_hdr_color->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  394. ctx->framebuffer_hdr_color->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  395. ctx->framebuffer_hdr_color->set_max_anisotropy(0.0f);
  396. ctx->framebuffer_hdr_depth = new gl::texture_2d(viewport_dimensions[0], viewport_dimensions[1], gl::pixel_type::float_32, gl::pixel_format::ds);
  397. ctx->framebuffer_hdr_depth->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  398. ctx->framebuffer_hdr_depth->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  399. ctx->framebuffer_hdr_depth->set_max_anisotropy(0.0f);
  400. ctx->framebuffer_hdr = new gl::framebuffer(viewport_dimensions[0], viewport_dimensions[1]);
  401. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::color, ctx->framebuffer_hdr_color);
  402. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::depth, ctx->framebuffer_hdr_depth);
  403. ctx->framebuffer_hdr->attach(gl::framebuffer_attachment_type::stencil, ctx->framebuffer_hdr_depth);
  404. // Create shadow map framebuffer
  405. int shadow_map_resolution = 4096;
  406. if (ctx->config->has("shadow_map_resolution"))
  407. {
  408. shadow_map_resolution = ctx->config->get<int>("shadow_map_resolution");
  409. }
  410. ctx->shadow_map_depth_texture = new gl::texture_2d(shadow_map_resolution, shadow_map_resolution, gl::pixel_type::float_32, gl::pixel_format::d);
  411. ctx->shadow_map_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  412. ctx->shadow_map_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  413. ctx->shadow_map_depth_texture->set_max_anisotropy(0.0f);
  414. ctx->shadow_map_framebuffer = new gl::framebuffer(shadow_map_resolution, shadow_map_resolution);
  415. ctx->shadow_map_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx->shadow_map_depth_texture);
  416. // Create bloom pingpong framebuffers (16F color, no depth)
  417. int bloom_width = viewport_dimensions[0] >> 1;
  418. int bloom_height = viewport_dimensions[1] >> 1;
  419. ctx->bloom_texture = new gl::texture_2d(bloom_width, bloom_height, gl::pixel_type::float_16, gl::pixel_format::rgb);
  420. ctx->bloom_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  421. ctx->bloom_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  422. ctx->bloom_texture->set_max_anisotropy(0.0f);
  423. ctx->framebuffer_bloom = new gl::framebuffer(bloom_width, bloom_height);
  424. ctx->framebuffer_bloom->attach(gl::framebuffer_attachment_type::color, ctx->bloom_texture);
  425. // Load blue noise texture
  426. gl::texture_2d* blue_noise_map = ctx->resource_manager->load<gl::texture_2d>("blue-noise.tex");
  427. // Load fallback material
  428. ctx->fallback_material = ctx->resource_manager->load<material>("fallback.mtl");
  429. // Setup common render passes
  430. {
  431. ctx->common_bloom_pass = new bloom_pass(ctx->rasterizer, ctx->framebuffer_bloom, ctx->resource_manager);
  432. ctx->common_bloom_pass->set_source_texture(ctx->framebuffer_hdr_color);
  433. ctx->common_bloom_pass->set_brightness_threshold(1.0f);
  434. ctx->common_bloom_pass->set_blur_iterations(5);
  435. ctx->common_final_pass = new ::final_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  436. ctx->common_final_pass->set_color_texture(ctx->framebuffer_hdr_color);
  437. ctx->common_final_pass->set_bloom_texture(ctx->bloom_texture);
  438. ctx->common_final_pass->set_blue_noise_texture(blue_noise_map);
  439. }
  440. // Setup UI compositor
  441. {
  442. ctx->ui_clear_pass = new clear_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer());
  443. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  444. ctx->ui_clear_pass->set_clear_depth(0.0f);
  445. ctx->ui_material_pass = new material_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  446. ctx->ui_material_pass->set_fallback_material(ctx->fallback_material);
  447. ctx->ui_compositor = new compositor();
  448. ctx->ui_compositor->add_pass(ctx->ui_clear_pass);
  449. ctx->ui_compositor->add_pass(ctx->ui_material_pass);
  450. }
  451. // Setup underground compositor
  452. {
  453. ctx->underground_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  454. ctx->underground_clear_pass->set_cleared_buffers(true, true, false);
  455. ctx->underground_clear_pass->set_clear_color({1, 0, 1, 0});
  456. ctx->underground_clear_pass->set_clear_depth(0.0f);
  457. ctx->underground_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  458. ctx->underground_material_pass->set_fallback_material(ctx->fallback_material);
  459. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->underground_material_pass);
  460. ctx->underground_compositor = new compositor();
  461. ctx->underground_compositor->add_pass(ctx->underground_clear_pass);
  462. ctx->underground_compositor->add_pass(ctx->underground_material_pass);
  463. ctx->underground_compositor->add_pass(ctx->common_bloom_pass);
  464. ctx->underground_compositor->add_pass(ctx->common_final_pass);
  465. }
  466. // Setup surface compositor
  467. {
  468. ctx->surface_shadow_map_clear_pass = new clear_pass(ctx->rasterizer, ctx->shadow_map_framebuffer);
  469. ctx->surface_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  470. ctx->surface_shadow_map_clear_pass->set_clear_depth(1.0f);
  471. ctx->surface_shadow_map_pass = new shadow_map_pass(ctx->rasterizer, ctx->shadow_map_framebuffer, ctx->resource_manager);
  472. ctx->surface_shadow_map_pass->set_split_scheme_weight(0.75f);
  473. ctx->surface_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  474. ctx->surface_clear_pass->set_cleared_buffers(true, true, true);
  475. ctx->surface_clear_pass->set_clear_depth(0.0f);
  476. ctx->surface_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  477. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->surface_sky_pass);
  478. ctx->surface_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  479. ctx->surface_material_pass->set_fallback_material(ctx->fallback_material);
  480. ctx->surface_material_pass->shadow_map_pass = ctx->surface_shadow_map_pass;
  481. ctx->surface_material_pass->shadow_map = ctx->shadow_map_depth_texture;
  482. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->surface_material_pass);
  483. ctx->surface_outline_pass = new outline_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  484. ctx->surface_outline_pass->set_outline_width(0.25f);
  485. ctx->surface_outline_pass->set_outline_color(float4{1.0f, 1.0f, 1.0f, 1.0f});
  486. ctx->surface_compositor = new compositor();
  487. ctx->surface_compositor->add_pass(ctx->surface_shadow_map_clear_pass);
  488. ctx->surface_compositor->add_pass(ctx->surface_shadow_map_pass);
  489. ctx->surface_compositor->add_pass(ctx->surface_clear_pass);
  490. ctx->surface_compositor->add_pass(ctx->surface_sky_pass);
  491. ctx->surface_compositor->add_pass(ctx->surface_material_pass);
  492. //ctx->surface_compositor->add_pass(ctx->surface_outline_pass);
  493. ctx->surface_compositor->add_pass(ctx->common_bloom_pass);
  494. ctx->surface_compositor->add_pass(ctx->common_final_pass);
  495. }
  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. const float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  538. // Create infinite culling mask
  539. const float inf = std::numeric_limits<float>::infinity();
  540. ctx->no_cull = {{-inf, -inf, -inf}, {inf, inf, inf}};
  541. // Setup UI camera
  542. ctx->ui_camera = new scene::camera();
  543. ctx->ui_camera->set_compositor(ctx->ui_compositor);
  544. // Setup underground camera
  545. ctx->underground_camera = new scene::camera();
  546. ctx->underground_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  547. ctx->underground_camera->set_compositor(ctx->underground_compositor);
  548. ctx->underground_camera->set_composite_index(0);
  549. ctx->underground_camera->set_active(false);
  550. // Setup surface camera
  551. ctx->surface_camera = new scene::camera();
  552. ctx->surface_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  553. ctx->surface_camera->set_compositor(ctx->surface_compositor);
  554. ctx->surface_camera->set_composite_index(0);
  555. ctx->surface_camera->set_active(false);
  556. // Setup UI scene
  557. {
  558. ctx->ui_scene = new scene::collection();
  559. const gl::texture_2d* splash_texture = ctx->resource_manager->load<gl::texture_2d>("splash.tex");
  560. auto splash_dimensions = splash_texture->get_dimensions();
  561. ctx->splash_billboard_material = new material();
  562. ctx->splash_billboard_material->set_shader_program(ctx->resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  563. ctx->splash_billboard_material->add_property<const gl::texture_2d*>("background")->set_value(splash_texture);
  564. ctx->splash_billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  565. ctx->splash_billboard_material->update_tweens();
  566. ctx->splash_billboard = new scene::billboard();
  567. ctx->splash_billboard->set_material(ctx->splash_billboard_material);
  568. ctx->splash_billboard->set_scale({(float)std::get<0>(splash_dimensions) * 0.5f, (float)std::get<1>(splash_dimensions) * 0.5f, 1.0f});
  569. ctx->splash_billboard->set_translation({0.0f, 0.0f, 0.0f});
  570. ctx->splash_billboard->update_tweens();
  571. // Create depth debug billboard
  572. /*
  573. material* depth_debug_material = new material();
  574. depth_debug_material->set_shader_program(ctx->resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  575. depth_debug_material->add_property<const gl::texture_2d*>("background")->set_value(shadow_map_depth_texture);
  576. depth_debug_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  577. billboard* depth_debug_billboard = new billboard();
  578. depth_debug_billboard->set_material(depth_debug_material);
  579. depth_debug_billboard->set_scale({128, 128, 1});
  580. depth_debug_billboard->set_translation({-960 + 128, 1080 * 0.5f - 128, 0});
  581. depth_debug_billboard->update_tweens();
  582. ui_system->get_scene()->add_object(depth_debug_billboard);
  583. */
  584. ctx->ui_scene->add_object(ctx->ui_camera);
  585. }
  586. // Setup underground scene
  587. {
  588. ctx->underground_scene = new scene::collection();
  589. ctx->underground_ambient_light = new scene::ambient_light();
  590. ctx->underground_ambient_light->set_color({1, 1, 1});
  591. ctx->underground_ambient_light->set_intensity(0.1f);
  592. ctx->underground_ambient_light->update_tweens();
  593. ctx->flashlight_spot_light = new scene::spot_light();
  594. ctx->flashlight_spot_light->set_color({1, 1, 1});
  595. ctx->flashlight_spot_light->set_intensity(1.0f);
  596. ctx->flashlight_spot_light->set_attenuation({1.0f, 0.0f, 0.0f});
  597. ctx->flashlight_spot_light->set_cutoff({math::radians(10.0f), math::radians(19.0f)});
  598. ctx->underground_scene->add_object(ctx->underground_camera);
  599. ctx->underground_scene->add_object(ctx->underground_ambient_light);
  600. //ctx->underground_scene->add_object(ctx->flashlight_spot_light);
  601. }
  602. // Setup surface scene
  603. {
  604. ctx->surface_scene = new scene::collection();
  605. ctx->surface_scene->add_object(ctx->surface_camera);
  606. }
  607. // Clear active scene
  608. ctx->active_scene = nullptr;
  609. logger->pop_task(EXIT_SUCCESS);
  610. }
  611. void setup_animation(game::context* ctx)
  612. {
  613. // Setup timeline system
  614. ctx->timeline = new timeline();
  615. ctx->timeline->set_autoremove(true);
  616. // Setup animator
  617. ctx->animator = new animator();
  618. // Initialize time tween
  619. ctx->time_tween = new tween<double>(0.0);
  620. ctx->time_tween->set_interpolator(math::lerp<double, double>);
  621. // Create fade transition
  622. ctx->fade_transition = new screen_transition();
  623. ctx->fade_transition->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("fade-transition.glsl"));
  624. ctx->fade_transition_color = ctx->fade_transition->get_material()->add_property<float3>("color");
  625. ctx->fade_transition_color->set_value({0, 0, 0});
  626. ctx->ui_scene->add_object(ctx->fade_transition->get_billboard());
  627. ctx->animator->add_animation(ctx->fade_transition->get_animation());
  628. // Create inner radial transition
  629. ctx->radial_transition_inner = new screen_transition();
  630. ctx->radial_transition_inner->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("radial-transition-inner.glsl"));
  631. ctx->ui_scene->add_object(ctx->radial_transition_inner->get_billboard());
  632. ctx->animator->add_animation(ctx->radial_transition_inner->get_animation());
  633. // Create outer radial transition
  634. ctx->radial_transition_outer = new screen_transition();
  635. ctx->radial_transition_outer->get_material()->set_shader_program(ctx->resource_manager->load<gl::shader_program>("radial-transition-outer.glsl"));
  636. ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard());
  637. ctx->animator->add_animation(ctx->radial_transition_outer->get_animation());
  638. // Set material pass tweens
  639. ctx->common_final_pass->set_time_tween(ctx->time_tween);
  640. ctx->surface_sky_pass->set_time_tween(ctx->time_tween);
  641. ctx->surface_material_pass->set_time_tween(ctx->time_tween);
  642. ctx->underground_material_pass->set_time_tween(ctx->time_tween);
  643. ctx->ui_material_pass->set_time_tween(ctx->time_tween);
  644. }
  645. void setup_entities(game::context* ctx)
  646. {
  647. // Create entity registry
  648. ctx->entity_registry = new entt::registry();
  649. }
  650. void setup_systems(game::context* ctx)
  651. {
  652. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  653. const auto& viewport_dimensions = ctx->app->get_viewport_dimensions();
  654. float4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
  655. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  656. const double3 rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  657. // Setup terrain system
  658. ctx->terrain_system = new entity::system::terrain(*ctx->entity_registry);
  659. ctx->terrain_system->set_patch_subdivisions(30);
  660. ctx->terrain_system->set_patch_scene_collection(ctx->surface_scene);
  661. ctx->terrain_system->set_max_error(200.0);
  662. // Setup vegetation system
  663. //ctx->vegetation_system = new entity::system::vegetation(*ctx->entity_registry);
  664. //ctx->vegetation_system->set_terrain_patch_size(TERRAIN_PATCH_SIZE);
  665. //ctx->vegetation_system->set_vegetation_patch_resolution(VEGETATION_PATCH_RESOLUTION);
  666. //ctx->vegetation_system->set_vegetation_density(1.0f);
  667. //ctx->vegetation_system->set_vegetation_model(ctx->resource_manager->load<model>("grass-tuft.mdl"));
  668. //ctx->vegetation_system->set_scene(ctx->surface_scene);
  669. // Setup camera system
  670. ctx->camera_system = new entity::system::camera(*ctx->entity_registry);
  671. ctx->camera_system->set_viewport(viewport);
  672. event_dispatcher->subscribe<window_resized_event>(ctx->camera_system);
  673. // Setup tool system
  674. ctx->tool_system = new entity::system::tool(*ctx->entity_registry, event_dispatcher);
  675. ctx->tool_system->set_camera(ctx->surface_camera);
  676. ctx->tool_system->set_viewport(viewport);
  677. // Setup subterrain system
  678. ctx->subterrain_system = new entity::system::subterrain(*ctx->entity_registry, ctx->resource_manager);
  679. ctx->subterrain_system->set_scene(ctx->underground_scene);
  680. // Setup collision system
  681. ctx->collision_system = new entity::system::collision(*ctx->entity_registry);
  682. // Setup samara system
  683. ctx->samara_system = new entity::system::samara(*ctx->entity_registry);
  684. // Setup snapping system
  685. ctx->snapping_system = new entity::system::snapping(*ctx->entity_registry);
  686. // Setup behavior system
  687. ctx->behavior_system = new entity::system::behavior(*ctx->entity_registry);
  688. // Setup locomotion system
  689. ctx->locomotion_system = new entity::system::locomotion(*ctx->entity_registry);
  690. // Setup spatial system
  691. ctx->spatial_system = new entity::system::spatial(*ctx->entity_registry);
  692. // Setup constraint system
  693. ctx->constraint_system = new entity::system::constraint(*ctx->entity_registry);
  694. // Setup tracking system
  695. ctx->tracking_system = new entity::system::tracking(*ctx->entity_registry, event_dispatcher, ctx->resource_manager);
  696. ctx->tracking_system->set_scene(ctx->surface_scene);
  697. // Setup painting system
  698. ctx->painting_system = new entity::system::painting(*ctx->entity_registry, event_dispatcher, ctx->resource_manager);
  699. ctx->painting_system->set_scene(ctx->surface_scene);
  700. // Setup solar system
  701. ctx->orbit_system = new entity::system::orbit(*ctx->entity_registry);
  702. // Setup blackbody system
  703. ctx->blackbody_system = new entity::system::blackbody(*ctx->entity_registry);
  704. ctx->blackbody_system->set_rgb_wavelengths(rgb_wavelengths_nm);
  705. // Setup atmosphere system
  706. ctx->atmosphere_system = new entity::system::atmosphere(*ctx->entity_registry);
  707. ctx->atmosphere_system->set_rgb_wavelengths(rgb_wavelengths_nm);
  708. // Setup astronomy system
  709. ctx->astronomy_system = new entity::system::astronomy(*ctx->entity_registry);
  710. ctx->astronomy_system->set_sky_pass(ctx->surface_sky_pass);
  711. // Setup proteome system
  712. ctx->proteome_system = new entity::system::proteome(*ctx->entity_registry);
  713. // Set time scale
  714. float time_scale = 60.0f;
  715. if (ctx->config->has("time_scale"))
  716. {
  717. time_scale = ctx->config->get<float>("time_scale");
  718. }
  719. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  720. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  721. // Setup render system
  722. ctx->render_system = new entity::system::render(*ctx->entity_registry);
  723. ctx->render_system->add_layer(ctx->underground_scene);
  724. ctx->render_system->add_layer(ctx->surface_scene);
  725. ctx->render_system->add_layer(ctx->ui_scene);
  726. ctx->render_system->set_renderer(ctx->renderer);
  727. // Setup UI system
  728. ctx->ui_system = new entity::system::ui(ctx->resource_manager);
  729. ctx->ui_system->set_camera(ctx->ui_camera);
  730. ctx->ui_system->set_scene(ctx->ui_scene);
  731. ctx->ui_system->set_viewport(viewport);
  732. event_dispatcher->subscribe<mouse_moved_event>(ctx->ui_system);
  733. event_dispatcher->subscribe<window_resized_event>(ctx->ui_system);
  734. }
  735. void setup_controls(game::context* ctx)
  736. {
  737. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  738. // Setup input event routing
  739. ctx->input_event_router = new input::event_router();
  740. ctx->input_event_router->set_event_dispatcher(event_dispatcher);
  741. // Setup input mapper
  742. ctx->input_mapper = new input::mapper();
  743. ctx->input_mapper->set_event_dispatcher(event_dispatcher);
  744. // Setup input listener
  745. ctx->input_listener = new input::listener();
  746. ctx->input_listener->set_event_dispatcher(event_dispatcher);
  747. /*
  748. // Add menu control mappings
  749. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->menu_back_control, nullptr, input::game_controller_button::b));
  750. //ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_tool_menu_control(), nullptr, input::scancode::left_shift));
  751. ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_tool_menu_control(), nullptr, input::game_controller_button::x));
  752. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_select_control, nullptr, input::scancode::enter));
  753. ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_select_control, nullptr, input::scancode::space));
  754. ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_toggle_view_control(), nullptr, input::scancode::tab));
  755. ctx->control_system->get_toggle_view_control()->set_activated_callback(
  756. [ctx]()
  757. {
  758. if (ctx->active_scene == ctx->surface_scene)
  759. {
  760. ctx->active_scene = ctx->underground_scene;
  761. ctx->radial_transition_inner->transition(0.5f, false, ease<float, double>::in_quad);
  762. auto switch_cameras = [ctx]()
  763. {
  764. ctx->surface_camera->set_active(false);
  765. ctx->underground_camera->set_active(true);
  766. ctx->fade_transition->transition(0.25f, true, ease<float, double>::out_quad);
  767. };
  768. float t = ctx->timeline->get_position();
  769. ctx->timeline->add_cue({t + 0.5f, switch_cameras});
  770. }
  771. else
  772. {
  773. ctx->active_scene = ctx->surface_scene;
  774. ctx->fade_transition->transition(0.25f, false, ease<float, double>::out_quad);
  775. auto switch_cameras = [ctx]()
  776. {
  777. ctx->surface_camera->set_active(true);
  778. ctx->underground_camera->set_active(false);
  779. ctx->radial_transition_inner->transition(0.5f, true, ease<float, double>::out_quad);
  780. };
  781. float t = ctx->timeline->get_position();
  782. ctx->timeline->add_cue({t + 0.25f, switch_cameras});
  783. }
  784. });
  785. float time_scale = ctx->config->get<float>("time_scale");
  786. ctx->control_system->get_fast_forward_control()->set_activated_callback
  787. (
  788. [ctx, time_scale]()
  789. {
  790. ctx->orbit_system->set_time_scale(time_scale * 100.0f / seconds_per_day);
  791. ctx->astronomy_system->set_time_scale(time_scale * 100.0f / seconds_per_day);
  792. }
  793. );
  794. ctx->control_system->get_fast_forward_control()->set_deactivated_callback
  795. (
  796. [ctx, time_scale]()
  797. {
  798. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  799. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  800. }
  801. );
  802. ctx->control_system->get_rewind_control()->set_activated_callback
  803. (
  804. [ctx, time_scale]()
  805. {
  806. ctx->orbit_system->set_time_scale(time_scale * -100.0f / seconds_per_day);
  807. ctx->astronomy_system->set_time_scale(time_scale * -100.0f / seconds_per_day);
  808. }
  809. );
  810. ctx->control_system->get_rewind_control()->set_deactivated_callback
  811. (
  812. [ctx, time_scale]()
  813. {
  814. ctx->orbit_system->set_time_scale(time_scale / seconds_per_day);
  815. ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day);
  816. }
  817. );
  818. */
  819. }
  820. void setup_cli(game::context* ctx)
  821. {
  822. ctx->cli = new debug::cli();
  823. ctx->cli->register_command("echo", debug::cc::echo);
  824. ctx->cli->register_command("exit", std::function<std::string()>(std::bind(&debug::cc::exit, ctx)));
  825. ctx->cli->register_command("scrot", std::function<std::string()>(std::bind(&debug::cc::scrot, ctx)));
  826. ctx->cli->register_command("cue", std::function<std::string(float, std::string)>(std::bind(&debug::cc::cue, ctx, std::placeholders::_1, std::placeholders::_2)));
  827. //std::string cmd = "cue 20 exit";
  828. //logger->log(cmd);
  829. //logger->log(cli.interpret(cmd));
  830. }
  831. void setup_callbacks(game::context* ctx)
  832. {
  833. // Set update callback
  834. ctx->app->set_update_callback
  835. (
  836. [ctx](double t, double dt)
  837. {
  838. // Update controls
  839. for (const auto& control: ctx->controls)
  840. control.second->update();
  841. // Update tweens
  842. ctx->time_tween->update();
  843. ctx->surface_sky_pass->update_tweens();
  844. ctx->surface_scene->update_tweens();
  845. ctx->underground_scene->update_tweens();
  846. ctx->ui_scene->update_tweens();
  847. // Set time tween time
  848. (*ctx->time_tween)[1] = t;
  849. ctx->timeline->advance(dt);
  850. ctx->terrain_system->update(t, dt);
  851. //ctx->vegetation_system->update(t, dt);
  852. ctx->snapping_system->update(t, dt);
  853. ctx->subterrain_system->update(t, dt);
  854. ctx->collision_system->update(t, dt);
  855. ctx->samara_system->update(t, dt);
  856. ctx->behavior_system->update(t, dt);
  857. ctx->locomotion_system->update(t, dt);
  858. ctx->camera_system->update(t, dt);
  859. ctx->tool_system->update(t, dt);
  860. ctx->orbit_system->update(t, dt);
  861. ctx->blackbody_system->update(t, dt);
  862. ctx->atmosphere_system->update(t, dt);
  863. ctx->astronomy_system->update(t, dt);
  864. ctx->spatial_system->update(t, dt);
  865. ctx->constraint_system->update(t, dt);
  866. ctx->tracking_system->update(t, dt);
  867. ctx->painting_system->update(t, dt);
  868. ctx->proteome_system->update(t, dt);
  869. ctx->ui_system->update(dt);
  870. ctx->render_system->update(t, dt);
  871. ctx->animator->animate(dt);
  872. }
  873. );
  874. // Set render callback
  875. ctx->app->set_render_callback
  876. (
  877. [ctx](double alpha)
  878. {
  879. ctx->render_system->draw(alpha);
  880. }
  881. );
  882. }