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

437 lines
13 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 "game/states/nuptial-flight.hpp"
  32. #include "game/states/splash.hpp"
  33. #include "geom/spherical.hpp"
  34. #include "gl/drawing-mode.hpp"
  35. #include "gl/vertex-array.hpp"
  36. #include "gl/vertex-attribute-type.hpp"
  37. #include "gl/vertex-buffer.hpp"
  38. #include "physics/light/photometry.hpp"
  39. #include "physics/orbit/orbit.hpp"
  40. #include "renderer/material.hpp"
  41. #include "renderer/model.hpp"
  42. #include "renderer/passes/shadow-map-pass.hpp"
  43. #include "renderer/vertex-attributes.hpp"
  44. #include "resources/resource-manager.hpp"
  45. #include "scene/ambient-light.hpp"
  46. #include "scene/directional-light.hpp"
  47. namespace game {
  48. namespace state {
  49. namespace loading {
  50. /// Creates the universe and solar system.
  51. static void cosmogenesis(game::context* ctx);
  52. /// Creates a sun.
  53. static void heliogenesis(game::context* ctx);
  54. /// Creates a planet.
  55. static void planetogenesis(game::context* ctx);
  56. /// Creates a moon.
  57. static void selenogenesis(game::context* ctx);
  58. /// Creates fixed stars.
  59. static void extrasolar_heliogenesis(game::context* ctx);
  60. /// Creates an ant colony
  61. static void colonigenesis(game::context* ctx);
  62. void enter(game::context* ctx)
  63. {
  64. // Create universe
  65. ctx->logger->push_task("Creating the universe");
  66. try
  67. {
  68. cosmogenesis(ctx);
  69. }
  70. catch (...)
  71. {
  72. ctx->logger->pop_task(EXIT_FAILURE);
  73. throw;
  74. }
  75. ctx->logger->pop_task(EXIT_SUCCESS);
  76. // Determine next game state
  77. application::state next_state;
  78. if (ctx->option_quick_start.has_value())
  79. {
  80. next_state.name = "nuptial flight";
  81. next_state.enter = std::bind(game::state::nuptial_flight::enter, ctx);
  82. next_state.exit = std::bind(game::state::nuptial_flight::exit, ctx);
  83. }
  84. else
  85. {
  86. next_state.name = "splash";
  87. next_state.enter = std::bind(game::state::splash::enter, ctx);
  88. next_state.exit = std::bind(game::state::splash::exit, ctx);
  89. }
  90. // Queue next game state
  91. ctx->app->queue_state(next_state);
  92. }
  93. void exit(game::context* ctx)
  94. {}
  95. void cosmogenesis(game::context* ctx)
  96. {
  97. // Init time
  98. const double time = 0.0;
  99. ctx->astronomy_system->set_universal_time(time);
  100. ctx->orbit_system->set_universal_time(time);
  101. // Create sun
  102. ctx->logger->push_task("Creating the sun");
  103. try
  104. {
  105. heliogenesis(ctx);
  106. }
  107. catch (...)
  108. {
  109. ctx->logger->pop_task(EXIT_FAILURE);
  110. throw;
  111. }
  112. ctx->logger->pop_task(EXIT_SUCCESS);
  113. // Create planet
  114. ctx->logger->push_task("Creating the planet");
  115. try
  116. {
  117. planetogenesis(ctx);
  118. }
  119. catch (...)
  120. {
  121. ctx->logger->pop_task(EXIT_FAILURE);
  122. throw;
  123. }
  124. ctx->logger->pop_task(EXIT_SUCCESS);
  125. // Create moon
  126. ctx->logger->push_task("Creating the moon");
  127. try
  128. {
  129. selenogenesis(ctx);
  130. }
  131. catch (...)
  132. {
  133. ctx->logger->pop_task(EXIT_FAILURE);
  134. throw;
  135. }
  136. ctx->logger->pop_task(EXIT_SUCCESS);
  137. // Create fixed stars
  138. ctx->logger->push_task("Creating fixed stars");
  139. try
  140. {
  141. extrasolar_heliogenesis(ctx);
  142. }
  143. catch (...)
  144. {
  145. ctx->logger->pop_task(EXIT_FAILURE);
  146. throw;
  147. }
  148. ctx->logger->pop_task(EXIT_SUCCESS);
  149. // Create ant colony
  150. ctx->logger->push_task("Creating ant colony");
  151. try
  152. {
  153. colonigenesis(ctx);
  154. }
  155. catch (...)
  156. {
  157. ctx->logger->pop_task(EXIT_FAILURE);
  158. throw;
  159. }
  160. ctx->logger->pop_task(EXIT_SUCCESS);
  161. }
  162. void heliogenesis(game::context* ctx)
  163. {
  164. // Create solar entity
  165. auto sun_eid = ctx->entity_registry->create();
  166. // Name solar entity
  167. ctx->named_entities["sun"] = sun_eid;
  168. // Assign solar celestial body component
  169. entity::component::celestial_body body;
  170. body.radius = 6.957e+8;
  171. body.axial_tilt = math::radians(0.0);
  172. body.axial_rotation = math::radians(0.0);
  173. body.angular_frequency = math::radians(0.0);
  174. ctx->entity_registry->assign<entity::component::celestial_body>(sun_eid, body);
  175. // Assign solar orbit component
  176. entity::component::orbit orbit;
  177. orbit.elements.a = 0.0;
  178. orbit.elements.e = 0.0;
  179. orbit.elements.i = math::radians(0.0);
  180. orbit.elements.raan = math::radians(0.0);
  181. orbit.elements.w = math::radians(0.0);
  182. orbit.elements.ta = math::radians(0.0);
  183. ctx->entity_registry->assign<entity::component::orbit>(sun_eid, orbit);
  184. // Assign solar blackbody component
  185. entity::component::blackbody blackbody;
  186. blackbody.temperature = 5778.0;
  187. ctx->entity_registry->assign<entity::component::blackbody>(sun_eid, blackbody);
  188. // Assign solar transform component
  189. entity::component::transform transform;
  190. transform.local = math::identity_transform<float>;
  191. transform.warp = true;
  192. ctx->entity_registry->assign<entity::component::transform>(sun_eid, transform);
  193. // Create direct sun light scene object
  194. scene::directional_light* sun_direct = new scene::directional_light();
  195. // Create ambient sun light scene object
  196. scene::ambient_light* sun_ambient = new scene::ambient_light();
  197. sun_ambient->set_color({1, 1, 1});
  198. sun_ambient->set_intensity(0.0f);
  199. sun_ambient->update_tweens();
  200. // Add sun light scene objects to surface scene
  201. ctx->surface_scene->add_object(sun_direct);
  202. ctx->surface_scene->add_object(sun_ambient);
  203. // Pass direct sun light scene object to shadow map pass and astronomy system
  204. ctx->surface_shadow_map_pass->set_light(sun_direct);
  205. ctx->astronomy_system->set_sun_light(sun_direct);
  206. }
  207. void planetogenesis(game::context* ctx)
  208. {
  209. // Create planetary entity
  210. auto planet_eid = ctx->entity_registry->create();
  211. // Name planetary entity
  212. ctx->named_entities["planet"] = planet_eid;
  213. // Assign planetary celestial body component
  214. entity::component::celestial_body body;
  215. body.radius = 6.3781e6;
  216. body.axial_tilt = math::radians(23.4393);
  217. body.axial_rotation = math::radians(280.46061837504);
  218. body.angular_frequency = math::radians(360.9856122880876128);
  219. ctx->entity_registry->assign<entity::component::celestial_body>(planet_eid, body);
  220. // Assign planetary orbit component
  221. entity::component::orbit orbit;
  222. orbit.elements.a = 1.496e+11;
  223. orbit.elements.e = 0.01671123;
  224. orbit.elements.i = math::radians(-0.00001531);
  225. orbit.elements.raan = math::radians(0.0);
  226. const double longitude_periapsis = math::radians(102.93768193);
  227. orbit.elements.w = longitude_periapsis - orbit.elements.raan;
  228. orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis;
  229. ctx->entity_registry->assign<entity::component::orbit>(planet_eid, orbit);
  230. // Assign planetary terrain component
  231. entity::component::terrain terrain;
  232. terrain.elevation = [](double, double) -> double
  233. {
  234. //return math::random<double>(0.0, 1.0);
  235. return 0.0;
  236. };
  237. terrain.max_lod = 0;
  238. terrain.patch_material = nullptr;
  239. ctx->entity_registry->assign<entity::component::terrain>(planet_eid, terrain);
  240. // Assign planetary atmosphere component
  241. entity::component::atmosphere atmosphere;
  242. atmosphere.exosphere_altitude = 65e3;
  243. atmosphere.index_of_refraction = 1.000293;
  244. atmosphere.rayleigh_density = 2.545e25;
  245. atmosphere.rayleigh_scale_height = 8000.0;
  246. atmosphere.mie_density = 14.8875;
  247. atmosphere.mie_scale_height = 1200.0;
  248. atmosphere.mie_anisotropy = 0.8;
  249. ctx->entity_registry->assign<entity::component::atmosphere>(planet_eid, atmosphere);
  250. // Assign planetary transform component
  251. entity::component::transform transform;
  252. transform.local = math::identity_transform<float>;
  253. transform.warp = true;
  254. ctx->entity_registry->assign<entity::component::transform>(planet_eid, transform);
  255. // Pass planet to astronomy system as reference body
  256. ctx->astronomy_system->set_reference_body(planet_eid);
  257. // Load sky model
  258. ctx->surface_sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  259. }
  260. void selenogenesis(game::context* ctx)
  261. {
  262. // Create lunar entity
  263. auto moon_eid = ctx->entity_registry->create();
  264. // Name lunar entity
  265. ctx->named_entities["moon"] = moon_eid;
  266. // Pass moon model to sky pass
  267. ctx->surface_sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  268. }
  269. void extrasolar_heliogenesis(game::context* ctx)
  270. {
  271. // Load star catalog
  272. string_table* star_catalog = ctx->resource_manager->load<string_table>("stars.csv");
  273. // Allocate star catalog vertex data
  274. std::size_t star_count = 0;
  275. if (star_catalog->size() > 0)
  276. star_count = star_catalog->size() - 1;
  277. std::size_t star_vertex_size = 6;
  278. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  279. float* star_vertex_data = new float[star_count * star_vertex_size];
  280. float* star_vertex = star_vertex_data;
  281. // Build star catalog vertex data
  282. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  283. {
  284. const string_table_row& catalog_row = (*star_catalog)[i];
  285. double ra = 0.0;
  286. double dec = 0.0;
  287. double vmag = 0.0;
  288. double bv_color = 0.0;
  289. // Parse star catalog entry
  290. try
  291. {
  292. ra = std::stod(catalog_row[1]);
  293. dec = std::stod(catalog_row[2]);
  294. vmag = std::stod(catalog_row[3]);
  295. bv_color = std::stod(catalog_row[4]);
  296. }
  297. catch (const std::exception& e)
  298. {
  299. continue;
  300. }
  301. // Convert right ascension and declination from degrees to radians
  302. ra = math::wrap_radians(math::radians(ra));
  303. dec = math::wrap_radians(math::radians(dec));
  304. // Transform spherical equatorial coordinates to rectangular equatorial coordinates
  305. double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra});
  306. // Transform coordinates from equatorial space to inertial space
  307. physics::frame<double> bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse();
  308. double3 position_inertial = bci_to_inertial * position_bci;
  309. // Convert color index to color temperature
  310. double cct = color::index::bv_to_cct(bv_color);
  311. // Calculate XYZ color from color temperature
  312. double3 color_xyz = color::cct::to_xyz(cct);
  313. // Transform XYZ color to ACEScg colorspace
  314. double3 color_acescg = color::xyz::to_acescg(color_xyz);
  315. // Convert apparent magnitude to irradiance (W/m^2)
  316. double vmag_irradiance = std::pow(10.0, 0.4 * (-vmag - 19.0 + 0.4));
  317. // Convert irradiance to illuminance
  318. double vmag_illuminance = vmag_irradiance * (683.0 * 0.14);
  319. // Scale color by illuminance
  320. double3 scaled_color = color_acescg * vmag_illuminance;
  321. // Build vertex
  322. *(star_vertex++) = static_cast<float>(position_inertial.x);
  323. *(star_vertex++) = static_cast<float>(position_inertial.y);
  324. *(star_vertex++) = static_cast<float>(position_inertial.z);
  325. *(star_vertex++) = static_cast<float>(scaled_color.x);
  326. *(star_vertex++) = static_cast<float>(scaled_color.y);
  327. *(star_vertex++) = static_cast<float>(scaled_color.z);
  328. }
  329. // Unload star catalog
  330. ctx->resource_manager->unload("stars.csv");
  331. // Allocate stars model
  332. model* stars_model = new model();
  333. // Resize model VBO and upload vertex data
  334. gl::vertex_buffer* vbo = stars_model->get_vertex_buffer();
  335. vbo->resize(star_count * star_vertex_stride, star_vertex_data);
  336. // Free star catalog vertex data
  337. delete[] star_vertex_data;
  338. // Bind vertex attributes to model VAO
  339. gl::vertex_array* vao = stars_model->get_vertex_array();
  340. std::size_t vao_offset = 0;
  341. vao->bind_attribute(VERTEX_POSITION_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, 0);
  342. vao_offset += 3;
  343. vao->bind_attribute(VERTEX_COLOR_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, sizeof(float) * vao_offset);
  344. // Load star material
  345. material* star_material = ctx->resource_manager->load<material>("fixed-star.mtl");
  346. // Create model group
  347. model_group* stars_model_group = stars_model->add_group("stars");
  348. stars_model_group->set_material(star_material);
  349. stars_model_group->set_drawing_mode(gl::drawing_mode::points);
  350. stars_model_group->set_start_index(0);
  351. stars_model_group->set_index_count(star_count);
  352. // Pass stars model to sky pass
  353. ctx->surface_sky_pass->set_stars_model(stars_model);
  354. }
  355. void colonigenesis(game::context* ctx)
  356. {
  357. // Create queen entity
  358. auto queen_eid = ctx->entity_registry->create();
  359. ctx->named_entities["queen"] = queen_eid;
  360. // Create central shaft entity
  361. auto shaft_eid = ctx->entity_registry->create();
  362. ctx->named_entities["shaft"] = queen_eid;
  363. // Create entrance "lobby" chamber entity
  364. auto lobby_eid = ctx->entity_registry->create();
  365. ctx->named_entities["lobby"] = lobby_eid;
  366. }
  367. } // namespace loading
  368. } // namespace state
  369. } // namespace game