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

301 lines
10 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 "resource-loader.hpp"
  20. #include "resource-manager.hpp"
  21. #include "render/model.hpp"
  22. #include "entity/components/atmosphere.hpp"
  23. #include "entity/components/behavior.hpp"
  24. #include "entity/components/collision.hpp"
  25. #include "entity/components/diffuse-reflector.hpp"
  26. #include "entity/components/terrain.hpp"
  27. #include "entity/components/transform.hpp"
  28. #include "entity/components/model.hpp"
  29. #include "entity/components/orbit.hpp"
  30. #include "entity/components/blackbody.hpp"
  31. #include "entity/components/celestial-body.hpp"
  32. #include "entity/archetype.hpp"
  33. #include "entity/ebt.hpp"
  34. #include "physics/orbit/elements.hpp"
  35. #include "resources/json.hpp"
  36. #include <stdexcept>
  37. static bool load_component_atmosphere(entity::archetype& archetype, const json& element)
  38. {
  39. entity::component::atmosphere component;
  40. if (element.contains("upper_limit"))
  41. component.upper_limit = element["upper_limit"].get<double>();
  42. if (element.contains("index_of_refraction"))
  43. component.index_of_refraction = element["index_of_refraction"].get<double>();
  44. if (element.contains("rayleigh_concentration"))
  45. component.rayleigh_concentration = element["rayleigh_concentration"].get<double>();
  46. if (element.contains("rayleigh_scale_height"))
  47. component.rayleigh_scale_height = element["rayleigh_scale_height"].get<double>();
  48. if (element.contains("mie_concentration"))
  49. component.mie_concentration = element["mie_concentration"].get<double>();
  50. if (element.contains("mie_scale_height"))
  51. component.mie_scale_height = element["mie_scale_height"].get<double>();
  52. if (element.contains("mie_anisotropy"))
  53. component.mie_anisotropy = element["mie_anisotropy"].get<double>();
  54. if (element.contains("mie_albedo"))
  55. component.mie_albedo = element["mie_albedo"].get<double>();
  56. if (element.contains("ozone_concentration"))
  57. component.ozone_concentration = element["ozone_concentration"].get<double>();
  58. if (element.contains("ozone_lower_limit"))
  59. component.ozone_lower_limit = element["ozone_lower_limit"].get<double>();
  60. if (element.contains("ozone_upper_limit"))
  61. component.ozone_upper_limit = element["ozone_upper_limit"].get<double>();
  62. if (element.contains("ozone_mode"))
  63. component.ozone_mode = element["ozone_mode"].get<double>();
  64. if (element.contains("airglow_illuminance"))
  65. {
  66. const auto& airglow_illuminance = element["airglow_illuminance"];
  67. component.airglow_illuminance.x = airglow_illuminance[0].get<double>();
  68. component.airglow_illuminance.y = airglow_illuminance[1].get<double>();
  69. component.airglow_illuminance.z = airglow_illuminance[2].get<double>();
  70. }
  71. archetype.set<entity::component::atmosphere>(component);
  72. return true;
  73. }
  74. static bool load_component_behavior(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  75. {
  76. entity::component::behavior component;
  77. component.behavior_tree = nullptr;
  78. if (element.contains("file"))
  79. {
  80. component.behavior_tree = resource_manager.load<entity::ebt::node>(element["file"].get<std::string>());
  81. }
  82. archetype.set<entity::component::behavior>(component);
  83. return (component.behavior_tree != nullptr);
  84. }
  85. static bool load_component_blackbody(entity::archetype& archetype, const json& element)
  86. {
  87. entity::component::blackbody component;
  88. component.temperature = 0.0;
  89. if (element.contains("temperature"))
  90. component.temperature = element["temperature"].get<double>();
  91. archetype.set<entity::component::blackbody>(component);
  92. return true;
  93. }
  94. static bool load_component_celestial_body(entity::archetype& archetype, const json& element)
  95. {
  96. entity::component::celestial_body component;
  97. if (element.contains("radius"))
  98. component.radius = element["radius"].get<double>();
  99. if (element.contains("mass"))
  100. component.mass = element["mass"].get<double>();
  101. if (element.contains("pole_ra"))
  102. {
  103. component.pole_ra.clear();
  104. auto& pole_ra_element = element["pole_ra"];
  105. for (auto it = pole_ra_element.rbegin(); it != pole_ra_element.rend(); ++it)
  106. component.pole_ra.push_back(math::radians(it->get<double>()));
  107. }
  108. if (element.contains("pole_dec"))
  109. {
  110. component.pole_dec.clear();
  111. auto& pole_dec_element = element["pole_dec"];
  112. for (auto it = pole_dec_element.rbegin(); it != pole_dec_element.rend(); ++it)
  113. component.pole_dec.push_back(math::radians(it->get<double>()));
  114. }
  115. if (element.contains("prime_meridian"))
  116. {
  117. component.prime_meridian.clear();
  118. auto& prime_meridian_element = element["prime_meridian"];
  119. for (auto it = prime_meridian_element.rbegin(); it != prime_meridian_element.rend(); ++it)
  120. component.prime_meridian.push_back(math::radians(it->get<double>()));
  121. }
  122. if (element.contains("albedo"))
  123. component.albedo = element["albedo"].get<double>();
  124. archetype.set<entity::component::celestial_body>(component);
  125. return true;
  126. }
  127. static bool load_component_collision(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  128. {
  129. entity::component::collision component;
  130. component.mesh = nullptr;
  131. if (element.contains("file"))
  132. {
  133. component.mesh = resource_manager.load<geom::mesh>(element["file"].get<std::string>());
  134. }
  135. archetype.set<entity::component::collision>(component);
  136. return (component.mesh != nullptr);
  137. }
  138. static bool load_component_diffuse_reflector(entity::archetype& archetype, const json& element)
  139. {
  140. entity::component::diffuse_reflector component;
  141. component.albedo = 0.0;
  142. if (element.contains("albedo"))
  143. component.albedo = element["albedo"].get<double>();
  144. archetype.set<entity::component::diffuse_reflector>(component);
  145. return true;
  146. }
  147. static bool load_component_model(entity::archetype& archetype, resource_manager& resource_manager, const json& element)
  148. {
  149. entity::component::model component;
  150. component.instance_count = 0;
  151. //component.layers = ~0;
  152. component.layers = 1;
  153. if (element.contains("file"))
  154. {
  155. component.render_model = resource_manager.load<render::model>(element["file"].get<std::string>());
  156. }
  157. archetype.set<entity::component::model>(component);
  158. return true;
  159. }
  160. static bool load_component_orbit(entity::archetype& archetype, const json& element)
  161. {
  162. entity::component::orbit component;
  163. component.parent = entt::null;
  164. component.ephemeris_index = -1;
  165. component.scale = 1.0;
  166. component.position = {0, 0, 0};
  167. if (element.contains("ephemeris_index"))
  168. component.ephemeris_index = element["ephemeris_index"].get<int>();
  169. if (element.contains("scale"))
  170. component.scale = element["scale"].get<double>();
  171. archetype.set<entity::component::orbit>(component);
  172. return true;
  173. }
  174. static bool load_component_transform(entity::archetype& archetype, const json& element)
  175. {
  176. entity::component::transform component;
  177. component.local = math::transform<float>::identity;
  178. component.warp = true;
  179. if (element.contains("translation"))
  180. {
  181. auto translation = element["translation"];
  182. component.local.translation.x = translation[0].get<float>();
  183. component.local.translation.y = translation[1].get<float>();
  184. component.local.translation.z = translation[2].get<float>();
  185. }
  186. if (element.contains("rotation"))
  187. {
  188. auto translation = element["rotation"];
  189. component.local.rotation.w = translation[0].get<float>();
  190. component.local.rotation.x = translation[1].get<float>();
  191. component.local.rotation.y = translation[2].get<float>();
  192. component.local.rotation.z = translation[3].get<float>();
  193. }
  194. if (element.contains("scale"))
  195. {
  196. auto translation = element["scale"];
  197. component.local.scale.x = translation[0].get<float>();
  198. component.local.scale.y = translation[1].get<float>();
  199. component.local.scale.z = translation[2].get<float>();
  200. }
  201. component.world = component.local;
  202. archetype.set<entity::component::transform>(component);
  203. return true;
  204. }
  205. static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, json::const_iterator element)
  206. {
  207. if (element.key() == "atmosphere")
  208. return load_component_atmosphere(archetype, element.value());
  209. if (element.key() == "behavior")
  210. return load_component_behavior(archetype, resource_manager, element.value());
  211. if (element.key() == "blackbody")
  212. return load_component_blackbody(archetype, element.value());
  213. if (element.key() == "celestial_body")
  214. return load_component_celestial_body(archetype, element.value());
  215. if (element.key() == "collision")
  216. return load_component_collision(archetype, resource_manager, element.value());
  217. if (element.key() == "diffuse_reflector")
  218. return load_component_diffuse_reflector(archetype, element.value());
  219. if (element.key() == "model")
  220. return load_component_model(archetype, resource_manager, element.value());
  221. if (element.key() == "orbit")
  222. return load_component_orbit(archetype, element.value());
  223. if (element.key() == "transform")
  224. return load_component_transform(archetype, element.value());
  225. //throw std::runtime_error("Unknown component type \"" + element.key() + "\"");
  226. return true;
  227. }
  228. template <>
  229. entity::archetype* resource_loader<entity::archetype>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
  230. {
  231. // Allocate archetype
  232. entity::archetype* archetype = new entity::archetype(resource_manager->get_archetype_registry());
  233. // Read file into buffer
  234. std::size_t size = static_cast<int>(PHYSFS_fileLength(file));
  235. std::string buffer;
  236. buffer.resize(size);
  237. PHYSFS_readBytes(file, &buffer[0], size);
  238. // Parse JSON data from file buffer
  239. json data = nlohmann::json::parse(buffer, nullptr, true, true);
  240. // Load components from table rows
  241. for (json::const_iterator element = data.cbegin(); element != data.cend(); ++element)
  242. {
  243. if (!load_component(*archetype, *resource_manager, element))
  244. {
  245. throw std::runtime_error("Failed to load component \"" + element.key() + "\"");
  246. }
  247. }
  248. return archetype;
  249. }