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

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