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

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