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

262 lines
8.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/world.hpp"
  20. #include "scene/text.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 "entity/archetype.hpp"
  33. #include "geom/spherical.hpp"
  34. #include "gl/drawing-mode.hpp"
  35. #include "gl/vertex-array.hpp"
  36. #include "gl/vertex-attribute.hpp"
  37. #include "gl/vertex-buffer.hpp"
  38. #include "physics/light/photometry.hpp"
  39. #include "physics/orbit/orbit.hpp"
  40. #include "render/material.hpp"
  41. #include "render/model.hpp"
  42. #include "render/passes/shadow-map-pass.hpp"
  43. #include "render/vertex-attribute.hpp"
  44. #include "resources/resource-manager.hpp"
  45. #include "scene/ambient-light.hpp"
  46. #include "scene/directional-light.hpp"
  47. #include "gl/texture-wrapping.hpp"
  48. #include "gl/texture-filter.hpp"
  49. #include "render/material-flags.hpp"
  50. #include "configuration.hpp"
  51. namespace game {
  52. namespace world {
  53. void create_stars(game::context& ctx)
  54. {
  55. // Load star catalog
  56. string_table* star_catalog = ctx.resource_manager->load<string_table>("stars.csv");
  57. // Allocate star catalog vertex data
  58. std::size_t star_count = 0;
  59. if (star_catalog->size() > 0)
  60. star_count = star_catalog->size() - 1;
  61. std::size_t star_vertex_size = 7;
  62. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  63. float* star_vertex_data = new float[star_count * star_vertex_size];
  64. float* star_vertex = star_vertex_data;
  65. // Build star catalog vertex data
  66. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  67. {
  68. const string_table_row& catalog_row = (*star_catalog)[i];
  69. double ra = 0.0;
  70. double dec = 0.0;
  71. double vmag = 0.0;
  72. double bv_color = 0.0;
  73. // Parse star catalog entry
  74. try
  75. {
  76. ra = std::stod(catalog_row[1]);
  77. dec = std::stod(catalog_row[2]);
  78. vmag = std::stod(catalog_row[3]);
  79. bv_color = std::stod(catalog_row[4]);
  80. }
  81. catch (const std::exception& e)
  82. {
  83. continue;
  84. }
  85. // Convert right ascension and declination from degrees to radians
  86. ra = math::wrap_radians(math::radians(ra));
  87. dec = math::wrap_radians(math::radians(dec));
  88. // Transform spherical equatorial coordinates to rectangular equatorial coordinates
  89. double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra});
  90. // Transform coordinates from equatorial space to inertial space
  91. physics::frame<double> bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse();
  92. double3 position_inertial = bci_to_inertial * position_bci;
  93. // Convert color index to color temperature
  94. double cct = color::index::bv_to_cct(bv_color);
  95. // Calculate XYZ color from color temperature
  96. double3 color_xyz = color::cct::to_xyz(cct);
  97. // Transform XYZ color to ACEScg colorspace
  98. double3 color_acescg = color::xyz::to_acescg(color_xyz);
  99. // Convert apparent magnitude to irradiance (W/m^2)
  100. double vmag_irradiance = std::pow(10.0, 0.4 * (-vmag - 19.0 + 0.4));
  101. // Convert irradiance to illuminance
  102. double vmag_illuminance = vmag_irradiance * (683.0 * 0.14);
  103. // Scale color by illuminance
  104. double3 scaled_color = color_acescg * vmag_illuminance;
  105. // Build vertex
  106. *(star_vertex++) = static_cast<float>(position_inertial.x);
  107. *(star_vertex++) = static_cast<float>(position_inertial.y);
  108. *(star_vertex++) = static_cast<float>(position_inertial.z);
  109. *(star_vertex++) = static_cast<float>(scaled_color.x);
  110. *(star_vertex++) = static_cast<float>(scaled_color.y);
  111. *(star_vertex++) = static_cast<float>(scaled_color.z);
  112. *(star_vertex++) = static_cast<float>(vmag);
  113. }
  114. // Unload star catalog
  115. ctx.resource_manager->unload("stars.csv");
  116. // Allocate stars model
  117. render::model* stars_model = new render::model();
  118. // Get model VBO and VAO
  119. gl::vertex_buffer* vbo = stars_model->get_vertex_buffer();
  120. gl::vertex_array* vao = stars_model->get_vertex_array();
  121. // Resize model VBO and upload vertex data
  122. vbo->resize(star_count * star_vertex_stride, star_vertex_data);
  123. // Free star catalog vertex data
  124. delete[] star_vertex_data;
  125. std::size_t attribute_offset = 0;
  126. // Define position vertex attribute
  127. gl::vertex_attribute position_attribute;
  128. position_attribute.buffer = vbo;
  129. position_attribute.offset = attribute_offset;
  130. position_attribute.stride = star_vertex_stride;
  131. position_attribute.type = gl::vertex_attribute_type::float_32;
  132. position_attribute.components = 3;
  133. attribute_offset += position_attribute.components * sizeof(float);
  134. // Define color vertex attribute
  135. gl::vertex_attribute color_attribute;
  136. color_attribute.buffer = vbo;
  137. color_attribute.offset = attribute_offset;
  138. color_attribute.stride = star_vertex_stride;
  139. color_attribute.type = gl::vertex_attribute_type::float_32;
  140. color_attribute.components = 4;
  141. attribute_offset += color_attribute.components * sizeof(float);
  142. // Bind vertex attributes to VAO
  143. vao->bind(render::vertex_attribute::position, position_attribute);
  144. vao->bind(render::vertex_attribute::color, color_attribute);
  145. // Load star material
  146. render::material* star_material = ctx.resource_manager->load<render::material>("fixed-star.mtl");
  147. // Create model group
  148. render::model_group* stars_model_group = stars_model->add_group("stars");
  149. stars_model_group->set_material(star_material);
  150. stars_model_group->set_drawing_mode(gl::drawing_mode::points);
  151. stars_model_group->set_start_index(0);
  152. stars_model_group->set_index_count(star_count);
  153. // Pass stars model to sky pass
  154. ctx.surface_sky_pass->set_stars_model(stars_model);
  155. }
  156. void create_sun(game::context& ctx)
  157. {
  158. // Create sun entity
  159. entity::archetype* sun_archetype = ctx.resource_manager->load<entity::archetype>("sun.ent");
  160. entity::id sun_eid = sun_archetype->create(*ctx.entity_registry);
  161. ctx.entities["sun"] = sun_eid;
  162. // Create direct sun light scene object
  163. scene::directional_light* sun_direct = new scene::directional_light();
  164. // Create ambient sun light scene object
  165. scene::ambient_light* sun_ambient = new scene::ambient_light();
  166. sun_ambient->set_color({1, 1, 1});
  167. sun_ambient->set_intensity(0.0f);
  168. sun_ambient->update_tweens();
  169. // Add sun light scene objects to surface scene
  170. ctx.surface_scene->add_object(sun_direct);
  171. ctx.surface_scene->add_object(sun_ambient);
  172. // Pass direct sun light scene object to shadow map pass and astronomy system
  173. ctx.surface_shadow_map_pass->set_light(sun_direct);
  174. ctx.astronomy_system->set_sun_light(sun_direct);
  175. }
  176. void create_planet(game::context& ctx)
  177. {
  178. // Create planet entity
  179. entity::archetype* planet_archetype = ctx.resource_manager->load<entity::archetype>("planet.ent");
  180. entity::id planet_eid = planet_archetype->create(*ctx.entity_registry);
  181. ctx.entities["planet"] = planet_eid;
  182. // Assign planetary terrain component
  183. entity::component::terrain terrain;
  184. terrain.elevation = [](double, double) -> double
  185. {
  186. //return math::random<double>(0.0, 1.0);
  187. return 0.0;
  188. };
  189. terrain.max_lod = 0;
  190. terrain.patch_material = nullptr;
  191. ctx.entity_registry->assign<entity::component::terrain>(planet_eid, terrain);
  192. // Pass planet to astronomy system as reference body
  193. ctx.astronomy_system->set_reference_body(planet_eid);
  194. // Load sky model
  195. ctx.surface_sky_pass->set_sky_model(ctx.resource_manager->load<render::model>("sky-dome.mdl"));
  196. }
  197. void create_moon(game::context& ctx)
  198. {
  199. // Create lunar entity
  200. entity::id moon_eid = ctx.entity_registry->create();
  201. ctx.entities["moon"] = moon_eid;
  202. // Pass moon model to sky pass
  203. ctx.surface_sky_pass->set_moon_model(ctx.resource_manager->load<render::model>("moon.mdl"));
  204. }
  205. void set_time(game::context& ctx, double t)
  206. {
  207. ctx.astronomy_system->set_universal_time(t);
  208. ctx.orbit_system->set_universal_time(t);
  209. }
  210. void set_time_scale(game::context& ctx, double scale)
  211. {
  212. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  213. scale /= seconds_per_day;
  214. ctx.orbit_system->set_time_scale(scale);
  215. ctx.astronomy_system->set_time_scale(scale);
  216. }
  217. } // namespace world
  218. } // namespace game