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

160 lines
4.3 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_GAME_SYSTEM_TERRAIN_HPP
  20. #define ANTKEEPER_GAME_SYSTEM_TERRAIN_HPP
  21. #include "game/system/updatable.hpp"
  22. #include "game/component/terrain.hpp"
  23. #include "entity/id.hpp"
  24. #include "math/quaternion.hpp"
  25. #include "geom/quadtree.hpp"
  26. #include "geom/mesh.hpp"
  27. #include "utility/fundamental-types.hpp"
  28. #include "render/model.hpp"
  29. #include "render/material.hpp"
  30. #include "scene/model-instance.hpp"
  31. #include "scene/collection.hpp"
  32. #include "geom/view-frustum.hpp"
  33. #include <unordered_map>
  34. namespace game {
  35. namespace system {
  36. /**
  37. * Generates terrain patches and performs terrain patch LOD selection.
  38. */
  39. class terrain: public updatable
  40. {
  41. public:
  42. terrain(entity::registry& registry);
  43. ~terrain();
  44. virtual void update(double t, double dt);
  45. /**
  46. * Sets the size of a patch.
  47. *
  48. * @param length Side length of a patch.
  49. */
  50. void set_patch_side_length(float length);
  51. /**
  52. * Sets the number of subdivisions of a patch. Zero subdivisions results in a single quad, one subdivison results in four quads, etc.
  53. *
  54. * @param n Number of subdivisions.
  55. */
  56. void set_patch_subdivisions(std::size_t n);
  57. /**
  58. * Sets the material of each patch.
  59. *
  60. * @param material Patch material.
  61. */
  62. void set_patch_material(::render::material* material);
  63. /**
  64. * Sets the terrain elevation function.
  65. *
  66. * @param f Function which returns the terrain height (Y-coordinate) given X- and Z-coordinates.
  67. */
  68. void set_elevation_function(const std::function<float(float, float)>& f);
  69. /**
  70. * Sets the scene collection into which terrain patch model instances will be inserted.
  71. */
  72. void set_scene_collection(scene::collection* collection);
  73. private:
  74. typedef geom::quadtree32 quadtree_type;
  75. typedef quadtree_type::node_type quadtree_node_type;
  76. struct patch
  77. {
  78. geom::mesh* mesh;
  79. ::render::model* model;
  80. scene::model_instance* model_instance;
  81. };
  82. void on_terrain_construct(entity::registry& registry, entity::id entity_id);
  83. void on_terrain_update(entity::registry& registry, entity::id entity_id);
  84. void on_terrain_destroy(entity::registry& registry, entity::id entity_id);
  85. float get_patch_size(quadtree_node_type node) const;
  86. float3 get_patch_center(quadtree_node_type node) const;
  87. void rebuild_patch_base_mesh();
  88. void visit_quadtree(const geom::bounding_volume<float>& volume, quadtree_node_type node);
  89. /**
  90. * Generates a mesh for a terrain patch given the patch's quadtree node
  91. */
  92. geom::mesh* generate_patch_mesh(quadtree_node_type node) const;
  93. /**
  94. * Generates a model for a terrain patch given the patch's mesh.
  95. */
  96. ::render::model* generate_patch_model(quadtree_node_type node) const;
  97. patch* generate_patch(quadtree_node_type node);
  98. float patch_side_length;
  99. std::size_t patch_subdivisions;
  100. std::size_t patch_cell_count;
  101. std::size_t patch_triangle_count;
  102. std::size_t patch_vertex_size;
  103. std::size_t patch_vertex_stride;
  104. float* patch_vertex_data;
  105. struct patch_vertex
  106. {
  107. float3 position;
  108. float2 uv;
  109. float3 normal;
  110. float3 tangent;
  111. float3 bitangent;
  112. float bitangent_sign;
  113. };
  114. mutable std::vector<std::vector<patch_vertex>> patch_vertex_buffer;
  115. ::render::material* patch_material;
  116. std::function<float(float, float)> elevation_function;
  117. scene::collection* scene_collection;
  118. geom::mesh* patch_base_mesh;
  119. /// Quadtree describing level of detail
  120. quadtree_type quadtree;
  121. float quadtree_node_size[quadtree_type::max_depth + 1];
  122. /// Map linking quadtree nodes to terrain patches
  123. std::unordered_map<quadtree_node_type, patch*> patches;
  124. };
  125. } // namespace system
  126. } // namespace game
  127. #endif // ANTKEEPER_GAME_SYSTEM_TERRAIN_HPP