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

268 lines
7.8 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 "game/states/play.hpp"
  21. #include "game/states/splash.hpp"
  22. #include "entity/components/celestial-body.hpp"
  23. #include "entity/components/orbit.hpp"
  24. #include "entity/components/blackbody.hpp"
  25. #include "entity/components/terrain.hpp"
  26. #include "entity/components/atmosphere.hpp"
  27. #include "entity/components/transform.hpp"
  28. #include "entity/systems/astronomy.hpp"
  29. #include "entity/systems/orbit.hpp"
  30. #include "scene/directional-light.hpp"
  31. #include "scene/ambient-light.hpp"
  32. #include "resources/resource-manager.hpp"
  33. #include "application.hpp"
  34. #include "renderer/passes/shadow-map-pass.hpp"
  35. namespace game {
  36. namespace state {
  37. namespace loading {
  38. /// Creates the universe and solar system.
  39. static void cosmogenesis(game::context* ctx);
  40. /// Creates a sun.
  41. static void heliogenesis(game::context* ctx);
  42. /// Creates a planet.
  43. static void planetogenesis(game::context* ctx);
  44. /// Creates a moon.
  45. static void selenogenesis(game::context* ctx);
  46. void enter(game::context* ctx)
  47. {
  48. // Create universe
  49. ctx->logger->push_task("Creating the universe");
  50. try
  51. {
  52. cosmogenesis(ctx);
  53. }
  54. catch (...)
  55. {
  56. ctx->logger->pop_task(EXIT_FAILURE);
  57. throw;
  58. }
  59. ctx->logger->pop_task(EXIT_SUCCESS);
  60. // Determine next game state
  61. application::state next_state;
  62. if (ctx->option_quick_start.has_value())
  63. {
  64. next_state.name = "play";
  65. next_state.enter = std::bind(game::state::play::enter, ctx);
  66. next_state.exit = std::bind(game::state::play::exit, ctx);
  67. }
  68. else
  69. {
  70. next_state.name = "splash";
  71. next_state.enter = std::bind(game::state::splash::enter, ctx);
  72. next_state.exit = std::bind(game::state::splash::exit, ctx);
  73. }
  74. // Queue next game state
  75. ctx->app->queue_state(next_state);
  76. }
  77. void exit(game::context* ctx)
  78. {}
  79. void cosmogenesis(game::context* ctx)
  80. {
  81. // Init time
  82. const double time = 0.0;
  83. ctx->astronomy_system->set_universal_time(time);
  84. ctx->orbit_system->set_universal_time(time);
  85. // Create sun
  86. ctx->logger->push_task("Creating the sun");
  87. try
  88. {
  89. heliogenesis(ctx);
  90. }
  91. catch (...)
  92. {
  93. ctx->logger->pop_task(EXIT_FAILURE);
  94. throw;
  95. }
  96. ctx->logger->pop_task(EXIT_SUCCESS);
  97. // Create planet
  98. ctx->logger->push_task("Creating the planet");
  99. try
  100. {
  101. planetogenesis(ctx);
  102. }
  103. catch (...)
  104. {
  105. ctx->logger->pop_task(EXIT_FAILURE);
  106. throw;
  107. }
  108. ctx->logger->pop_task(EXIT_SUCCESS);
  109. // Create moon
  110. ctx->logger->push_task("Creating the moon");
  111. try
  112. {
  113. selenogenesis(ctx);
  114. }
  115. catch (...)
  116. {
  117. ctx->logger->pop_task(EXIT_FAILURE);
  118. throw;
  119. }
  120. ctx->logger->pop_task(EXIT_SUCCESS);
  121. }
  122. void heliogenesis(game::context* ctx)
  123. {
  124. // Create solar entity
  125. auto sun_eid = ctx->entity_registry->create();
  126. // Name solar entity
  127. ctx->named_entities["sun"] = sun_eid;
  128. // Assign solar celestial body component
  129. entity::component::celestial_body body;
  130. body.radius = 6.957e+8;
  131. body.axial_tilt = math::radians(0.0);
  132. body.axial_rotation = math::radians(0.0);
  133. body.angular_frequency = math::radians(0.0);
  134. ctx->entity_registry->assign<entity::component::celestial_body>(sun_eid, body);
  135. // Assign solar orbit component
  136. entity::component::orbit orbit;
  137. orbit.elements.a = 0.0;
  138. orbit.elements.e = 0.0;
  139. orbit.elements.i = math::radians(0.0);
  140. orbit.elements.raan = math::radians(0.0);
  141. orbit.elements.w = math::radians(0.0);
  142. orbit.elements.ta = math::radians(0.0);
  143. ctx->entity_registry->assign<entity::component::orbit>(sun_eid, orbit);
  144. // Assign solar blackbody component
  145. entity::component::blackbody blackbody;
  146. blackbody.temperature = 5778.0;
  147. ctx->entity_registry->assign<entity::component::blackbody>(sun_eid, blackbody);
  148. // Assign solar transform component
  149. entity::component::transform transform;
  150. transform.local = math::identity_transform<float>;
  151. transform.warp = true;
  152. ctx->entity_registry->assign<entity::component::transform>(sun_eid, transform);
  153. // Create direct sun light scene object
  154. scene::directional_light* sun_direct = new scene::directional_light();
  155. // Create ambient sun light scene object
  156. scene::ambient_light* sun_ambient = new scene::ambient_light();
  157. sun_ambient->set_color({1, 1, 1});
  158. sun_ambient->set_intensity(0.0f);
  159. sun_ambient->update_tweens();
  160. // Add sun light scene objects to overworld scene
  161. ctx->overworld_scene->add_object(sun_direct);
  162. ctx->overworld_scene->add_object(sun_ambient);
  163. // Pass direct sun light scene object to shadow map pass and astronomy system
  164. ctx->overworld_shadow_map_pass->set_light(sun_direct);
  165. ctx->astronomy_system->set_sun_light(sun_direct);
  166. }
  167. void planetogenesis(game::context* ctx)
  168. {
  169. // Create planetary entity
  170. auto planet_eid = ctx->entity_registry->create();
  171. // Name planetary entity
  172. ctx->named_entities["planet"] = planet_eid;
  173. // Assign planetary celestial body component
  174. entity::component::celestial_body body;
  175. body.radius = 6.3781e6;
  176. body.axial_tilt = math::radians(23.4393);
  177. body.axial_rotation = math::radians(280.46061837504);
  178. body.angular_frequency = math::radians(360.9856122880876128);
  179. ctx->entity_registry->assign<entity::component::celestial_body>(planet_eid, body);
  180. // Assign planetary orbit component
  181. entity::component::orbit orbit;
  182. orbit.elements.a = 1.496e+11;
  183. orbit.elements.e = 0.01671123;
  184. orbit.elements.i = math::radians(-0.00001531);
  185. orbit.elements.raan = math::radians(0.0);
  186. const double longitude_periapsis = math::radians(102.93768193);
  187. orbit.elements.w = longitude_periapsis - orbit.elements.raan;
  188. orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis;
  189. ctx->entity_registry->assign<entity::component::orbit>(planet_eid, orbit);
  190. // Assign planetary terrain component
  191. entity::component::terrain terrain;
  192. terrain.elevation = [](double, double) -> double
  193. {
  194. //return math::random<double>(0.0, 1.0);
  195. return 0.0;
  196. };
  197. terrain.max_lod = 18;
  198. terrain.patch_material = ctx->resource_manager->load<material>("desert-terrain.mtl");
  199. ctx->entity_registry->assign<entity::component::terrain>(planet_eid, terrain);
  200. // Assign planetary atmosphere component
  201. entity::component::atmosphere atmosphere;
  202. atmosphere.exosphere_altitude = 65e3;
  203. atmosphere.index_of_refraction = 1.000293;
  204. atmosphere.rayleigh_density = 2.545e25;
  205. atmosphere.rayleigh_scale_height = 8000.0;
  206. atmosphere.mie_density = 14.8875;
  207. atmosphere.mie_scale_height = 1200.0;
  208. atmosphere.mie_anisotropy = 0.8;
  209. ctx->entity_registry->assign<entity::component::atmosphere>(planet_eid, atmosphere);
  210. // Assign planetary transform component
  211. entity::component::transform transform;
  212. transform.local = math::identity_transform<float>;
  213. transform.warp = true;
  214. ctx->entity_registry->assign<entity::component::transform>(planet_eid, transform);
  215. // Pass planet to astronomy system as reference body
  216. ctx->astronomy_system->set_reference_body(planet_eid);
  217. // Load sky model
  218. ctx->overworld_sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  219. }
  220. void selenogenesis(game::context* ctx)
  221. {
  222. // Create lunar entity
  223. auto moon_eid = ctx->entity_registry->create();
  224. // Name lunar entity
  225. ctx->named_entities["moon"] = moon_eid;
  226. // Pass moon model to sky pass
  227. ctx->overworld_sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  228. }
  229. } // namespace loading
  230. } // namespace state
  231. } // namespace game