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

250 lines
6.9 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/load.hpp"
  20. #include "game/world.hpp"
  21. #include "application.hpp"
  22. #include "debug/logger.hpp"
  23. #include "resources/json.hpp"
  24. #include "resources/resource-manager.hpp"
  25. #include "render/model.hpp"
  26. #include "render/material.hpp"
  27. #include "render/passes/sky-pass.hpp"
  28. #include "render/passes/ground-pass.hpp"
  29. #include "game/system/astronomy.hpp"
  30. #include "game/system/terrain.hpp"
  31. #include "math/random.hpp"
  32. #include "math/noise/noise.hpp"
  33. #include <fstream>
  34. #include <iostream>
  35. #include <stb/stb_image_write.h>
  36. #include "resources/image.hpp"
  37. #include <algorithm>
  38. #include <execution>
  39. namespace game {
  40. namespace load {
  41. void biome(game::context& ctx, const std::filesystem::path& path)
  42. {
  43. ctx.logger->push_task("Loading biome from \"" + path.string() + "\"");
  44. image img;
  45. img.format(1, 1);
  46. img.resize(2048, 2048);
  47. float frequency = 10.0f;
  48. std::size_t octaves = 4;
  49. float lacunarity = 2.0f;
  50. float gain = 0.5f;
  51. auto hash = static_cast<std::uint32_t(*)(const math::vector<float, 2>&)>(math::noise::hash::pcg3d_1);
  52. auto noise = static_cast<float(*)(const math::vector<float, 2>&, decltype(hash))>(math::noise::simplex);
  53. auto fbm = [&](const float2& x)
  54. {
  55. return math::noise::fbm
  56. (
  57. x,
  58. octaves,
  59. lacunarity,
  60. gain,
  61. noise,
  62. hash
  63. );
  64. };
  65. auto width = img.get_width();
  66. auto height = img.get_height();
  67. unsigned char* pixels = (unsigned char*)img.data();
  68. float scale_x = 1.0f / static_cast<float>(width - 1) * frequency;
  69. float scale_y = 1.0f / static_cast<float>(height - 1) * frequency;
  70. std::for_each
  71. (
  72. std::execution::par_unseq,
  73. img.begin<unsigned char>(),
  74. img.end<unsigned char>(),
  75. [pixels, width, height, scale_x, scale_y, &fbm](auto& pixel)
  76. {
  77. std::size_t i = &pixel - pixels;
  78. std::size_t y = i / width;
  79. std::size_t x = i % width;
  80. float2 position =
  81. {
  82. static_cast<float>(x) * scale_x,
  83. static_cast<float>(y) * scale_y
  84. };
  85. //float n = math::noise::simplex<float, 2>(position, &math::noise::hash::pcg3d_1);
  86. float n = fbm(position);
  87. pixel = static_cast<unsigned char>((n * 0.5f + 0.5f) * 255.0f);
  88. }
  89. );
  90. stbi_flip_vertically_on_write(1);
  91. stbi_write_png((ctx.config_path / "gallery" / "simplex-noise.png").string().c_str(), img.get_width(), img.get_height(), img.get_channel_count(), img.data(), img.get_width() * img.get_channel_count());
  92. try
  93. {
  94. json* data = ctx.resource_manager->load<json>(path);
  95. // Load location
  96. if (auto location = data->find("location"); location != data->end())
  97. {
  98. double elevation = 0.0;
  99. double latitude = 0.0;
  100. double longitude = 0.0;
  101. if (auto location_ele = location->find("elevation"); location_ele != location->end())
  102. elevation = location_ele->get<double>();
  103. else
  104. ctx.logger->warning("Biome elevation undefined");
  105. if (auto location_lat = location->find("latitude"); location_lat != location->end())
  106. latitude = math::radians<double>(location_lat->get<double>());
  107. else
  108. ctx.logger->warning("Biome latitude undefined");
  109. if (auto location_lon = location->find("longitude"); location_lon != location->end())
  110. longitude = math::radians<double>(location_lon->get<double>());
  111. else
  112. ctx.logger->warning("Biome longitude undefined");
  113. // Set location
  114. game::world::set_location(ctx, elevation, latitude, longitude);
  115. }
  116. else
  117. {
  118. ctx.logger->warning("Biome location undefined");
  119. }
  120. // Setup sky
  121. ctx.sky_pass->set_sky_model(ctx.resource_manager->load<render::model>("celestial-hemisphere.mdl"));
  122. // Load terrain
  123. if (auto terrain = data->find("terrain"); terrain != data->end())
  124. {
  125. if (auto material = terrain->find("material"); material != terrain->end())
  126. {
  127. render::material* terrain_material = ctx.resource_manager->load<render::material>(material->get<std::string>());
  128. ctx.terrain_system->set_patch_material(terrain_material);
  129. }
  130. else
  131. {
  132. ctx.logger->warning("Biome terrain material undefined");
  133. }
  134. if (auto material = terrain->find("horizon_material"); material != terrain->end())
  135. {
  136. render::model* terrestrial_hemisphere_model = ctx.resource_manager->load<render::model>("terrestrial-hemisphere.mdl");
  137. (*terrestrial_hemisphere_model->get_groups())[0]->set_material(ctx.resource_manager->load<render::material>(material->get<std::string>()));
  138. ctx.ground_pass->set_ground_model(terrestrial_hemisphere_model);
  139. }
  140. else
  141. {
  142. ctx.logger->warning("Biome terrain horizon material undefined");
  143. }
  144. // Terrain elevation function
  145. ctx.terrain_system->set_elevation_function
  146. (
  147. [](float x, float z) -> float
  148. {
  149. float angle = math::radians(30.0f);
  150. float c = std::cos(angle);
  151. float s = std::sin(angle);
  152. x = x * c - z * s;
  153. z = x * s + z * c;
  154. float frequency = 0.01f;
  155. std::size_t octaves = 4;
  156. float lacunarity = 3.0f;
  157. float gain = 0.5f;
  158. auto noise = static_cast<float(*)(const math::vector<float, 2>&, decltype(hash))>(math::noise::simplex);
  159. auto hash = static_cast<std::uint32_t(*)(const math::vector<float, 2>&)>(math::noise::hash::pcg3d_1);
  160. float2 position = float2{x, z} * frequency;
  161. float n = math::noise::fbm
  162. (
  163. position,
  164. octaves,
  165. lacunarity,
  166. gain,
  167. noise,
  168. hash
  169. );
  170. return 2.0f * n;
  171. }
  172. );
  173. // Setup lighting
  174. double3 terrain_albedo = {0, 0, 0};
  175. if (terrain->contains("albedo"))
  176. {
  177. const auto& albedo_element = (*terrain)["albedo"];
  178. terrain_albedo[0] = albedo_element[0].get<double>();
  179. terrain_albedo[1]= albedo_element[1].get<double>();
  180. terrain_albedo[2] = albedo_element[2].get<double>();
  181. }
  182. ctx.astronomy_system->set_bounce_albedo(terrain_albedo);
  183. }
  184. else
  185. {
  186. ctx.logger->warning("Biome terrain undefined");
  187. }
  188. }
  189. catch (...)
  190. {
  191. ctx.logger->pop_task(EXIT_FAILURE);
  192. }
  193. ctx.logger->pop_task(EXIT_SUCCESS);
  194. }
  195. void colony(game::context& ctx, const std::filesystem::path& path)
  196. {
  197. ctx.logger->push_task("Loading colony from \"" + path.string() + "\"");
  198. try
  199. {
  200. json* data = ctx.resource_manager->load<json>(path);
  201. }
  202. catch (...)
  203. {
  204. ctx.logger->pop_task(EXIT_FAILURE);
  205. }
  206. ctx.logger->pop_task(EXIT_SUCCESS);
  207. }
  208. } // namespace load
  209. } // namespace game