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

209 lines
5.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 "entity/systems/terrain.hpp"
  20. #include "utility/fundamental-types.hpp"
  21. #include "entity/components/observer.hpp"
  22. #include "entity/components/terrain.hpp"
  23. #include "entity/components/celestial-body.hpp"
  24. #include "geom/quadtree.hpp"
  25. /**
  26. * A cube with six quadtrees as faces.
  27. */
  28. struct quadtree_cube
  29. {
  30. typedef geom::quadtree32 quadtree_type;
  31. typedef quadtree_type::node_type node_type;
  32. void clear();
  33. /**
  34. * Refines the quadtree cube.
  35. *
  36. * @param threshold Function object which, given a quadsphere face index and quadtree node, returns `true` if the node should be subdivided, and `false` otherwise.
  37. */
  38. void refine(const std::function<bool(std::uint8_t, node_type)>& threshold);
  39. quadtree_type faces[6];
  40. };
  41. void quadtree_cube::clear()
  42. {
  43. for (std::uint8_t i = 0; i < 6; ++i)
  44. faces[i].clear();
  45. }
  46. void quadtree_cube::refine(const std::function<bool(std::uint8_t, node_type)>& threshold)
  47. {
  48. for (std::uint8_t i = 0; i < 6; ++i)
  49. {
  50. for (auto it = faces[i].begin(); it != faces[i].end(); ++it)
  51. {
  52. node_type node = *it;
  53. if (threshold(i, node))
  54. faces[i].insert(quadtree_type::child(node, 0));
  55. }
  56. }
  57. }
  58. /*
  59. terrain_qtc.refine
  60. (
  61. [observer](std::uint8_t face_index, quadtree_cube_type::node_type node) -> bool
  62. {
  63. // Extract morton location code
  64. quadtree_type::node_type location = quadtree_type::location(node);
  65. quadtree_type::node_type morton_x;
  66. quadtree_type::node_type morton_y;
  67. geom::morton::decode(location, morton_x, morton_y);
  68. // Extract depth
  69. quadtree_type::node_type depth = quadtree_type::depth(node);
  70. // Determine fractional side length at depth
  71. float length = 1.0f / std::exp2(depth);
  72. // Determine fractional center of node
  73. float3 center;
  74. center.x = (static_cast<float>(morton_x) * length + length * 0.5f) * 2.0f - 1.0f;
  75. center.y = (static_cast<float>(morton_y) * length + length * 0.5f) * 2.0f - 1.0f;
  76. center.z = 1.0f;
  77. // Project node center onto unit sphere
  78. center = math::normalize(center);
  79. // Rotate projected center into sphere space
  80. center = face_rotations[face_index] * center;
  81. // Scale center by body radius
  82. center *= body_radius;
  83. // Calculate distance from observer to node center
  84. float distance = math::length(projected_center - observer_location);
  85. if (depth < 4 && distance < ...)
  86. return true;
  87. return false;
  88. }
  89. );
  90. */
  91. /**
  92. * Queries a quad sphere for a list of leaf nodes. Leaf nodes will be inserted in the set
  93. *
  94. *
  95. * 0. If observer position changed more than x amount:
  96. * 1. Clear quad sphere
  97. * 2. Insert leaves based on observer distance.
  98. * 3. Pass quad sphere to tile generation function.
  99. * 3. Iterate leaves, deriving the face, depth, and morton location from each leaf index.
  100. * 4. Face, depth, and morton location can be used to determine latitude, longitude, and generate tiles.
  101. * 5. Generated tiles cached and added to scene.
  102. * 6. Record position of observer
  103. */
  104. /**
  105. * Lat, lon determination:
  106. *
  107. * 1. Use morton location and depth to determine the x-y coordinates on a planar cube face.
  108. * 2. Project x-y coordinates onto sphere.
  109. * 3. Rotate coordinates according to face index.
  110. * 4. Convert cartesian coordinates to spherical coordinates.
  111. */
  112. namespace entity {
  113. namespace system {
  114. terrain::terrain(entity::registry& registry):
  115. updatable(registry),
  116. patch_subdivisions(0),
  117. patch_vertex_size(0),
  118. patch_vertex_count(0),
  119. patch_vertex_data(nullptr)
  120. {
  121. // position + uv + normal + tangent + barycentric
  122. patch_vertex_size = 3 + 2 + 3 + 4 + 3;
  123. set_patch_subdivisions(0);
  124. registry.on_construct<component::terrain>().connect<&terrain::on_terrain_construct>(this);
  125. registry.on_destroy<component::terrain>().connect<&terrain::on_terrain_destroy>(this);
  126. }
  127. terrain::~terrain()
  128. {}
  129. void terrain::update(double t, double dt)
  130. {
  131. // Subdivide or collapse quad sphere
  132. registry.view<component::observer>().each(
  133. [&](entity::id observer_eid, const auto& observer)
  134. {
  135. // Skip observers with null reference body
  136. if (observer.reference_body_eid == entt::null)
  137. return;
  138. // Skip observers with non-body or non-terrestrial reference bodies
  139. if (!registry.has<component::celestial_body>(observer.reference_body_eid) ||
  140. !registry.has<component::terrain>(observer.reference_body_eid))
  141. return;
  142. const auto& celestial_body = registry.get<component::celestial_body>(observer.reference_body_eid);
  143. const auto& terrain = registry.get<component::terrain>(observer.reference_body_eid);
  144. // Haversine distance to all 6 faces, then recursively to children
  145. });
  146. }
  147. void terrain::set_patch_subdivisions(std::uint8_t n)
  148. {
  149. patch_subdivisions = n;
  150. // Recalculate number of vertices per patch
  151. patch_vertex_count = static_cast<std::size_t>(std::pow(std::exp2(patch_subdivisions) + 1, 2));
  152. // Resize patch vertex data buffer
  153. delete[] patch_vertex_data;
  154. patch_vertex_data = new float[patch_vertex_count * patch_vertex_size];
  155. }
  156. void terrain::on_terrain_construct(entity::registry& registry, entity::id entity_id, component::terrain& component)
  157. {
  158. // Build quad sphere
  159. }
  160. void terrain::on_terrain_destroy(entity::registry& registry, entity::id entity_id)
  161. {
  162. // Destroy quad sphere
  163. }
  164. void terrain::generate_patch()
  165. {
  166. }
  167. } // namespace system
  168. } // namespace entity