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

603 lines
18 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 "game/states/loading.hpp"
  20. #include "application.hpp"
  21. #include "astro/illuminance.hpp"
  22. #include "color/color.hpp"
  23. #include "entity/components/atmosphere.hpp"
  24. #include "entity/components/blackbody.hpp"
  25. #include "entity/components/celestial-body.hpp"
  26. #include "entity/components/orbit.hpp"
  27. #include "entity/components/terrain.hpp"
  28. #include "entity/components/transform.hpp"
  29. #include "entity/systems/astronomy.hpp"
  30. #include "entity/systems/orbit.hpp"
  31. #include "entity/commands.hpp"
  32. #include "game/states/nuptial-flight.hpp"
  33. #include "game/states/splash.hpp"
  34. #include "game/states/forage.hpp"
  35. #include "geom/spherical.hpp"
  36. #include "gl/drawing-mode.hpp"
  37. #include "gl/vertex-array.hpp"
  38. #include "gl/vertex-attribute-type.hpp"
  39. #include "gl/vertex-buffer.hpp"
  40. #include "physics/light/photometry.hpp"
  41. #include "physics/orbit/orbit.hpp"
  42. #include "renderer/material.hpp"
  43. #include "renderer/model.hpp"
  44. #include "renderer/passes/shadow-map-pass.hpp"
  45. #include "renderer/vertex-attributes.hpp"
  46. #include "resources/resource-manager.hpp"
  47. #include "scene/ambient-light.hpp"
  48. #include "scene/directional-light.hpp"
  49. #include "resources/config-file.hpp"
  50. #include "utility/timestamp.hpp"
  51. namespace game {
  52. namespace state {
  53. namespace loading {
  54. /// Creates or loads control configuration
  55. static void load_controls(game::context* ctx);
  56. /// Creates the universe and solar system.
  57. static void cosmogenesis(game::context* ctx);
  58. /// Creates a sun.
  59. static void heliogenesis(game::context* ctx);
  60. /// Creates a planet.
  61. static void planetogenesis(game::context* ctx);
  62. /// Creates a moon.
  63. static void selenogenesis(game::context* ctx);
  64. /// Creates fixed stars.
  65. static void extrasolar_heliogenesis(game::context* ctx);
  66. /// Creates an ant colony
  67. static void colonigenesis(game::context* ctx);
  68. void enter(game::context* ctx)
  69. {
  70. // Load controls
  71. ctx->logger->push_task("Loading controls");
  72. try
  73. {
  74. load_controls(ctx);
  75. }
  76. catch (...)
  77. {
  78. ctx->logger->pop_task(EXIT_FAILURE);
  79. }
  80. ctx->logger->pop_task(EXIT_SUCCESS);
  81. // Create universe
  82. ctx->logger->push_task("Creating the universe");
  83. try
  84. {
  85. cosmogenesis(ctx);
  86. }
  87. catch (...)
  88. {
  89. ctx->logger->pop_task(EXIT_FAILURE);
  90. throw;
  91. }
  92. ctx->logger->pop_task(EXIT_SUCCESS);
  93. // Determine next game state
  94. application::state next_state;
  95. if (ctx->option_quick_start.has_value())
  96. {
  97. next_state.name = "forage";
  98. next_state.enter = std::bind(game::state::forage::enter, ctx);
  99. next_state.exit = std::bind(game::state::forage::exit, ctx);
  100. }
  101. else
  102. {
  103. next_state.name = "splash";
  104. next_state.enter = std::bind(game::state::splash::enter, ctx);
  105. next_state.exit = std::bind(game::state::splash::exit, ctx);
  106. }
  107. // Queue next game state
  108. ctx->app->queue_state(next_state);
  109. }
  110. void exit(game::context* ctx)
  111. {}
  112. void load_controls(game::context* ctx)
  113. {
  114. // Toggle fullscreen
  115. if (!ctx->controls.count("toggle_fullscreen"))
  116. {
  117. input::control* control = new input::control();
  118. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::f11));
  119. ctx->controls["toggle_fullscreen"] = control;
  120. }
  121. ctx->controls["toggle_fullscreen"]->set_activated_callback
  122. (
  123. [ctx]()
  124. {
  125. bool fullscreen = !ctx->app->is_fullscreen();
  126. ctx->app->set_fullscreen(fullscreen);
  127. if (!fullscreen)
  128. {
  129. int2 resolution = ctx->config->get<int2>("windowed_resolution");
  130. ctx->app->resize_window(resolution.x, resolution.y);
  131. }
  132. ctx->config->set<int>("fullscreen", (fullscreen) ? 1 : 0);
  133. }
  134. );
  135. // Screenshot
  136. if (!ctx->controls.count("screenshot"))
  137. {
  138. input::control* control = new input::control();
  139. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::f12));
  140. ctx->controls["screenshot"] = control;
  141. }
  142. ctx->controls["screenshot"]->set_activated_callback
  143. (
  144. [ctx]()
  145. {
  146. std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png";
  147. ctx->app->save_frame(path);
  148. }
  149. );
  150. // Menu back
  151. if (!ctx->controls.count("menu_back"))
  152. {
  153. input::control* control = new input::control();
  154. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::escape));
  155. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::backspace));
  156. ctx->controls["menu_back"] = control;
  157. }
  158. ctx->controls["menu_back"]->set_activated_callback
  159. (
  160. std::bind(&application::close, ctx->app, 0)
  161. );
  162. // Dolly forward
  163. if (!ctx->controls.count("dolly_forward"))
  164. {
  165. input::control* control = new input::control();
  166. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::w));
  167. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_y, true));
  168. ctx->controls["dolly_forward"] = control;
  169. }
  170. // Dolly backward
  171. if (!ctx->controls.count("dolly_backward"))
  172. {
  173. input::control* control = new input::control();
  174. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::s));
  175. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_y, false));
  176. ctx->controls["dolly_backward"] = control;
  177. }
  178. // Truck left
  179. if (!ctx->controls.count("truck_left"))
  180. {
  181. input::control* control = new input::control();
  182. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::a));
  183. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_x, true));
  184. ctx->controls["truck_left"] = control;
  185. }
  186. // Truck right
  187. if (!ctx->controls.count("truck_right"))
  188. {
  189. input::control* control = new input::control();
  190. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::d));
  191. ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_x, false));
  192. ctx->controls["truck_right"] = control;
  193. }
  194. // Pedestal up
  195. if (!ctx->controls.count("pedestal_up"))
  196. {
  197. input::control* control = new input::control();
  198. ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(control, nullptr, input::mouse_wheel_axis::positive_y));
  199. ctx->controls["pedestal_up"] = control;
  200. }
  201. // Pedestal down
  202. if (!ctx->controls.count("pedestal_down"))
  203. {
  204. input::control* control = new input::control();
  205. ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(control, nullptr, input::mouse_wheel_axis::negative_y));
  206. ctx->controls["pedestal_down"] = control;
  207. }
  208. // Move slow
  209. if (!ctx->controls.count("move_slow"))
  210. {
  211. input::control* control = new input::control();
  212. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_ctrl));
  213. ctx->controls["move_slow"] = control;
  214. }
  215. // Move fast
  216. if (!ctx->controls.count("move_fast"))
  217. {
  218. input::control* control = new input::control();
  219. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_shift));
  220. ctx->controls["move_fast"] = control;
  221. }
  222. // Mouse rotate
  223. if (!ctx->controls.count("mouse_rotate"))
  224. {
  225. input::control* control = new input::control();
  226. ctx->input_event_router->add_mapping(input::mouse_button_mapping(control, nullptr, 3));
  227. ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_alt));
  228. ctx->controls["mouse_rotate"] = control;
  229. }
  230. // Mouse left
  231. if (!ctx->controls.count("mouse_left"))
  232. {
  233. input::control* control = new input::control();
  234. ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_x));
  235. ctx->controls["mouse_left"] = control;
  236. }
  237. // Mouse right
  238. if (!ctx->controls.count("mouse_right"))
  239. {
  240. input::control* control = new input::control();
  241. ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_x));
  242. ctx->controls["mouse_right"] = control;
  243. }
  244. // Mouse up
  245. if (!ctx->controls.count("mouse_up"))
  246. {
  247. input::control* control = new input::control();
  248. ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_y));
  249. ctx->controls["mouse_up"] = control;
  250. }
  251. // Mouse down
  252. if (!ctx->controls.count("mouse_down"))
  253. {
  254. input::control* control = new input::control();
  255. ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_y));
  256. ctx->controls["mouse_down"] = control;
  257. }
  258. }
  259. void cosmogenesis(game::context* ctx)
  260. {
  261. // Init time
  262. const double time = 0.0;
  263. ctx->astronomy_system->set_universal_time(time);
  264. ctx->orbit_system->set_universal_time(time);
  265. // Create sun
  266. ctx->logger->push_task("Creating the sun");
  267. try
  268. {
  269. heliogenesis(ctx);
  270. }
  271. catch (...)
  272. {
  273. ctx->logger->pop_task(EXIT_FAILURE);
  274. throw;
  275. }
  276. ctx->logger->pop_task(EXIT_SUCCESS);
  277. // Create planet
  278. ctx->logger->push_task("Creating the planet");
  279. try
  280. {
  281. planetogenesis(ctx);
  282. }
  283. catch (...)
  284. {
  285. ctx->logger->pop_task(EXIT_FAILURE);
  286. throw;
  287. }
  288. ctx->logger->pop_task(EXIT_SUCCESS);
  289. // Create moon
  290. ctx->logger->push_task("Creating the moon");
  291. try
  292. {
  293. selenogenesis(ctx);
  294. }
  295. catch (...)
  296. {
  297. ctx->logger->pop_task(EXIT_FAILURE);
  298. throw;
  299. }
  300. ctx->logger->pop_task(EXIT_SUCCESS);
  301. // Create fixed stars
  302. ctx->logger->push_task("Creating fixed stars");
  303. try
  304. {
  305. extrasolar_heliogenesis(ctx);
  306. }
  307. catch (...)
  308. {
  309. ctx->logger->pop_task(EXIT_FAILURE);
  310. throw;
  311. }
  312. ctx->logger->pop_task(EXIT_SUCCESS);
  313. // Create ant colony
  314. ctx->logger->push_task("Creating ant colony");
  315. try
  316. {
  317. colonigenesis(ctx);
  318. }
  319. catch (...)
  320. {
  321. ctx->logger->pop_task(EXIT_FAILURE);
  322. throw;
  323. }
  324. ctx->logger->pop_task(EXIT_SUCCESS);
  325. }
  326. void heliogenesis(game::context* ctx)
  327. {
  328. // Create solar entity
  329. entity::id sun_eid = ctx->entity_registry->create();
  330. ctx->entities["sun"] = sun_eid;
  331. // Assign solar celestial body component
  332. entity::component::celestial_body body;
  333. body.radius = 6.957e+8;
  334. body.axial_tilt = math::radians(0.0);
  335. body.axial_rotation = math::radians(0.0);
  336. body.angular_frequency = math::radians(0.0);
  337. ctx->entity_registry->assign<entity::component::celestial_body>(sun_eid, body);
  338. // Assign solar orbit component
  339. entity::component::orbit orbit;
  340. orbit.elements.a = 0.0;
  341. orbit.elements.e = 0.0;
  342. orbit.elements.i = math::radians(0.0);
  343. orbit.elements.raan = math::radians(0.0);
  344. orbit.elements.w = math::radians(0.0);
  345. orbit.elements.ta = math::radians(0.0);
  346. ctx->entity_registry->assign<entity::component::orbit>(sun_eid, orbit);
  347. // Assign solar blackbody component
  348. entity::component::blackbody blackbody;
  349. blackbody.temperature = 5778.0;
  350. ctx->entity_registry->assign<entity::component::blackbody>(sun_eid, blackbody);
  351. // Assign solar transform component
  352. entity::component::transform transform;
  353. transform.local = math::identity_transform<float>;
  354. transform.warp = true;
  355. ctx->entity_registry->assign<entity::component::transform>(sun_eid, transform);
  356. // Create direct sun light scene object
  357. scene::directional_light* sun_direct = new scene::directional_light();
  358. // Create ambient sun light scene object
  359. scene::ambient_light* sun_ambient = new scene::ambient_light();
  360. sun_ambient->set_color({1, 1, 1});
  361. sun_ambient->set_intensity(0.0f);
  362. sun_ambient->update_tweens();
  363. // Add sun light scene objects to surface scene
  364. ctx->surface_scene->add_object(sun_direct);
  365. ctx->surface_scene->add_object(sun_ambient);
  366. // Pass direct sun light scene object to shadow map pass and astronomy system
  367. ctx->surface_shadow_map_pass->set_light(sun_direct);
  368. ctx->astronomy_system->set_sun_light(sun_direct);
  369. }
  370. void planetogenesis(game::context* ctx)
  371. {
  372. // Create planetary entity
  373. entity::id planet_eid = ctx->entity_registry->create();
  374. ctx->entities["planet"] = planet_eid;
  375. // Assign planetary celestial body component
  376. entity::component::celestial_body body;
  377. body.radius = 6.3781e6;
  378. body.axial_tilt = math::radians(23.4393);
  379. body.axial_rotation = math::radians(280.46061837504);
  380. body.angular_frequency = math::radians(360.9856122880876128);
  381. ctx->entity_registry->assign<entity::component::celestial_body>(planet_eid, body);
  382. // Assign planetary orbit component
  383. entity::component::orbit orbit;
  384. orbit.elements.a = 1.496e+11;
  385. orbit.elements.e = 0.01671123;
  386. orbit.elements.i = math::radians(-0.00001531);
  387. orbit.elements.raan = math::radians(0.0);
  388. const double longitude_periapsis = math::radians(102.93768193);
  389. orbit.elements.w = longitude_periapsis - orbit.elements.raan;
  390. orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis;
  391. ctx->entity_registry->assign<entity::component::orbit>(planet_eid, orbit);
  392. // Assign planetary terrain component
  393. entity::component::terrain terrain;
  394. terrain.elevation = [](double, double) -> double
  395. {
  396. //return math::random<double>(0.0, 1.0);
  397. return 0.0;
  398. };
  399. terrain.max_lod = 0;
  400. terrain.patch_material = nullptr;
  401. ctx->entity_registry->assign<entity::component::terrain>(planet_eid, terrain);
  402. // Assign planetary atmosphere component
  403. entity::component::atmosphere atmosphere;
  404. atmosphere.exosphere_altitude = 65e3;
  405. atmosphere.index_of_refraction = 1.000293;
  406. atmosphere.rayleigh_density = 2.545e25;
  407. atmosphere.rayleigh_scale_height = 8000.0;
  408. atmosphere.mie_density = 14.8875;
  409. atmosphere.mie_scale_height = 1200.0;
  410. atmosphere.mie_anisotropy = 0.8;
  411. ctx->entity_registry->assign<entity::component::atmosphere>(planet_eid, atmosphere);
  412. // Assign planetary transform component
  413. entity::component::transform transform;
  414. transform.local = math::identity_transform<float>;
  415. transform.warp = true;
  416. ctx->entity_registry->assign<entity::component::transform>(planet_eid, transform);
  417. // Pass planet to astronomy system as reference body
  418. ctx->astronomy_system->set_reference_body(planet_eid);
  419. // Load sky model
  420. ctx->surface_sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  421. }
  422. void selenogenesis(game::context* ctx)
  423. {
  424. // Create lunar entity
  425. entity::id moon_eid = ctx->entity_registry->create();
  426. ctx->entities["moon"] = moon_eid;
  427. // Pass moon model to sky pass
  428. ctx->surface_sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  429. }
  430. void extrasolar_heliogenesis(game::context* ctx)
  431. {
  432. // Load star catalog
  433. string_table* star_catalog = ctx->resource_manager->load<string_table>("stars.csv");
  434. // Allocate star catalog vertex data
  435. std::size_t star_count = 0;
  436. if (star_catalog->size() > 0)
  437. star_count = star_catalog->size() - 1;
  438. std::size_t star_vertex_size = 6;
  439. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  440. float* star_vertex_data = new float[star_count * star_vertex_size];
  441. float* star_vertex = star_vertex_data;
  442. // Build star catalog vertex data
  443. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  444. {
  445. const string_table_row& catalog_row = (*star_catalog)[i];
  446. double ra = 0.0;
  447. double dec = 0.0;
  448. double vmag = 0.0;
  449. double bv_color = 0.0;
  450. // Parse star catalog entry
  451. try
  452. {
  453. ra = std::stod(catalog_row[1]);
  454. dec = std::stod(catalog_row[2]);
  455. vmag = std::stod(catalog_row[3]);
  456. bv_color = std::stod(catalog_row[4]);
  457. }
  458. catch (const std::exception& e)
  459. {
  460. continue;
  461. }
  462. // Convert right ascension and declination from degrees to radians
  463. ra = math::wrap_radians(math::radians(ra));
  464. dec = math::wrap_radians(math::radians(dec));
  465. // Transform spherical equatorial coordinates to rectangular equatorial coordinates
  466. double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra});
  467. // Transform coordinates from equatorial space to inertial space
  468. physics::frame<double> bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse();
  469. double3 position_inertial = bci_to_inertial * position_bci;
  470. // Convert color index to color temperature
  471. double cct = color::index::bv_to_cct(bv_color);
  472. // Calculate XYZ color from color temperature
  473. double3 color_xyz = color::cct::to_xyz(cct);
  474. // Transform XYZ color to ACEScg colorspace
  475. double3 color_acescg = color::xyz::to_acescg(color_xyz);
  476. // Convert apparent magnitude to irradiance (W/m^2)
  477. double vmag_irradiance = std::pow(10.0, 0.4 * (-vmag - 19.0 + 0.4));
  478. // Convert irradiance to illuminance
  479. double vmag_illuminance = vmag_irradiance * (683.0 * 0.14);
  480. // Scale color by illuminance
  481. double3 scaled_color = color_acescg * vmag_illuminance;
  482. // Build vertex
  483. *(star_vertex++) = static_cast<float>(position_inertial.x);
  484. *(star_vertex++) = static_cast<float>(position_inertial.y);
  485. *(star_vertex++) = static_cast<float>(position_inertial.z);
  486. *(star_vertex++) = static_cast<float>(scaled_color.x);
  487. *(star_vertex++) = static_cast<float>(scaled_color.y);
  488. *(star_vertex++) = static_cast<float>(scaled_color.z);
  489. }
  490. // Unload star catalog
  491. ctx->resource_manager->unload("stars.csv");
  492. // Allocate stars model
  493. model* stars_model = new model();
  494. // Resize model VBO and upload vertex data
  495. gl::vertex_buffer* vbo = stars_model->get_vertex_buffer();
  496. vbo->resize(star_count * star_vertex_stride, star_vertex_data);
  497. // Free star catalog vertex data
  498. delete[] star_vertex_data;
  499. // Bind vertex attributes to model VAO
  500. gl::vertex_array* vao = stars_model->get_vertex_array();
  501. std::size_t vao_offset = 0;
  502. vao->bind_attribute(VERTEX_POSITION_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, 0);
  503. vao_offset += 3;
  504. vao->bind_attribute(VERTEX_COLOR_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, sizeof(float) * vao_offset);
  505. // Load star material
  506. material* star_material = ctx->resource_manager->load<material>("fixed-star.mtl");
  507. // Create model group
  508. model_group* stars_model_group = stars_model->add_group("stars");
  509. stars_model_group->set_material(star_material);
  510. stars_model_group->set_drawing_mode(gl::drawing_mode::points);
  511. stars_model_group->set_start_index(0);
  512. stars_model_group->set_index_count(star_count);
  513. // Pass stars model to sky pass
  514. ctx->surface_sky_pass->set_stars_model(stars_model);
  515. }
  516. void colonigenesis(game::context* ctx)
  517. {}
  518. } // namespace loading
  519. } // namespace state
  520. } // namespace game