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

137 lines
4.1 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. #ifndef ANTKEEPER_ENTITY_SYSTEM_TERRAIN_HPP
  20. #define ANTKEEPER_ENTITY_SYSTEM_TERRAIN_HPP
  21. #include "entity/systems/updatable.hpp"
  22. #include "entity/components/terrain.hpp"
  23. #include "entity/id.hpp"
  24. #include "math/quaternion-type.hpp"
  25. #include "geom/quadtree.hpp"
  26. #include "geom/mesh.hpp"
  27. #include "utility/fundamental-types.hpp"
  28. #include "renderer/model.hpp"
  29. #include "renderer/material.hpp"
  30. #include "scene/model-instance.hpp"
  31. #include "scene/collection.hpp"
  32. #include <unordered_map>
  33. namespace entity {
  34. namespace system {
  35. /**
  36. * Generates and manages terrain with LOD based on distance to observers.
  37. */
  38. class terrain: public updatable
  39. {
  40. public:
  41. terrain(entity::registry& registry);
  42. ~terrain();
  43. virtual void update(double t, double dt);
  44. /**
  45. * Sets the number of subdivisions for a patch. Zero subdivisions results in a single quad, one subdivison results in four quads, etc.
  46. *
  47. * @param n Number of subdivisions.
  48. */
  49. void set_patch_subdivisions(std::uint8_t n);
  50. /**
  51. * Sets the scene collection into which terrain patch model instances will be inserted.
  52. */
  53. void set_patch_scene_collection(scene::collection* collection);
  54. /**
  55. * Sets the maximum tolerable screen-space error.
  56. *
  57. * If the screen-space error of a terrain patch exceeds the maximum tolerable value, it will be subdivided.
  58. *
  59. * @param error Maximum tolerable screen-space error.
  60. */
  61. void set_max_error(double error);
  62. private:
  63. typedef geom::quadtree64 quadtree_type;
  64. typedef quadtree_type::node_type quadtree_node_type;
  65. struct terrain_patch
  66. {
  67. geom::mesh* mesh;
  68. model* model;
  69. scene::model_instance* model_instance;
  70. float error;
  71. float morph;
  72. };
  73. /// Single face of a terrain quadsphere
  74. struct terrain_quadsphere_face
  75. {
  76. /// Quadtree describing level of detail
  77. quadtree_type quadtree;
  78. /// Map linking quadtree nodes to terrain patches
  79. std::unordered_map<quadtree_node_type, terrain_patch*> patches;
  80. };
  81. /// A terrain quadsphere with six faces.
  82. struct terrain_quadsphere
  83. {
  84. /// Array of six terrain quadsphere faces, in the order of +x, -x, +y, -y, +z, -z.
  85. terrain_quadsphere_face faces[6];
  86. };
  87. static double screen_space_error(double horizontal_fov, double horizontal_resolution, double distance, double geometric_error);
  88. void on_terrain_construct(entity::registry& registry, entity::id entity_id, entity::component::terrain& component);
  89. void on_terrain_destroy(entity::registry& registry, entity::id entity_id);
  90. /**
  91. * Generates a mesh for a terrain patch given the patch's quadtree node
  92. */
  93. geom::mesh* generate_patch_mesh(std::uint8_t face_index, quadtree_node_type node, double body_radius, const std::function<double(double, double)>& elevation) const;
  94. /**
  95. * Generates a model for a terrain patch given the patch's mesh.
  96. */
  97. model* generate_patch_model(const geom::mesh& patch_mesh, material* patch_material) const;
  98. /// @TODO horizon culling
  99. std::uint8_t patch_subdivisions;
  100. std::size_t patch_vertex_size;
  101. std::size_t patch_vertex_stride;
  102. std::size_t patch_vertex_count;
  103. float* patch_vertex_data;
  104. math::quaternion<double> face_rotations[6];
  105. geom::mesh* patch_base_mesh;
  106. scene::collection* patch_scene_collection;
  107. double max_error;
  108. std::unordered_map<entity::id, terrain_quadsphere*> terrain_quadspheres;
  109. };
  110. } // namespace system
  111. } // namespace entity
  112. #endif // ANTKEEPER_ENTITY_SYSTEM_TERRAIN_HPP