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

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