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

136 lines
3.6 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_SUBTERRAIN_HPP
  20. #define ANTKEEPER_ENTITY_SYSTEM_SUBTERRAIN_HPP
  21. #include "entity/systems/updatable.hpp"
  22. #include "geom/mesh.hpp"
  23. #include "geom/aabb.hpp"
  24. #include "scene/collection.hpp"
  25. #include "scene/model-instance.hpp"
  26. #include "utility/fundamental-types.hpp"
  27. #include <unordered_map>
  28. class resource_manager;
  29. class model;
  30. class model_group;
  31. class material;
  32. namespace entity {
  33. namespace system {
  34. struct cube_tree;
  35. template <std::int64_t Mantissa, std::int64_t Exponent>
  36. struct epsilon
  37. {
  38. static const double value;
  39. };
  40. template <std::int64_t Mantissa, std::int64_t Exponent>
  41. const double epsilon<Mantissa, Exponent>::value = static_cast<double>(Mantissa) * std::pow(10.0, Exponent);
  42. typedef epsilon<1, -5> epsilon_1en5;
  43. template <class Epsilon, class T, std::size_t N>
  44. struct vector_hasher
  45. {
  46. typedef math::vector<T, N> vector_type;
  47. std::size_t operator()(const vector_type& v) const noexcept
  48. {
  49. static const T inverse_epsilon = T(1) / Epsilon::value;
  50. std::size_t hash = 0;
  51. for (std::size_t i = 0; i < N; ++i)
  52. {
  53. std::int64_t j = static_cast<std::int64_t>(v[i] * inverse_epsilon);
  54. hash ^= std::hash<std::int64_t>()(j) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
  55. }
  56. return hash;
  57. }
  58. };
  59. template <class Epsilon, class T, std::size_t N>
  60. struct vector_equals
  61. {
  62. typedef math::vector<T, N> vector_type;
  63. bool operator()(const vector_type& a, const vector_type& b) const noexcept
  64. {
  65. for (std::size_t i = 0; i < N; ++i)
  66. {
  67. if (std::fabs(b[i] - a[i]) >= Epsilon::value)
  68. return false;
  69. }
  70. return true;
  71. }
  72. };
  73. class subterrain: public updatable
  74. {
  75. public:
  76. subterrain(entity::registry& registry, ::resource_manager* resource_manager);
  77. ~subterrain();
  78. virtual void update(double t, double dt);
  79. void set_scene(scene::collection* collection);
  80. private:
  81. void regenerate_subterrain_mesh();
  82. void march(cube_tree* node);
  83. void regenerate_subterrain_model();
  84. void dig(const float3&position, float radius);
  85. float distance(const cube_tree& node, const float3& sample) const;
  86. resource_manager* resource_manager;
  87. geom::mesh* subterrain_mesh;
  88. model* subterrain_model;
  89. material* subterrain_inside_material;
  90. material* subterrain_outside_material;
  91. model_group* subterrain_inside_group;
  92. model_group* subterrain_outside_group;
  93. int subterrain_model_vertex_size;
  94. int subterrain_model_vertex_stride;
  95. geom::aabb<float> subterrain_bounds;
  96. cube_tree* cube_tree;
  97. std::vector<float3> subterrain_vertices;
  98. std::vector<std::array<std::uint_fast32_t, 3>> subterrain_triangles;
  99. float isosurface_resolution;
  100. bool first_run;
  101. int merged;
  102. std::unordered_map<
  103. float3,
  104. std::uint_fast32_t,
  105. vector_hasher<epsilon_1en5, float, 3>,
  106. vector_equals<epsilon_1en5, float, 3>> subterrain_vertex_map;
  107. scene::collection* collection;
  108. scene::model_instance* subterrain_model_instance;
  109. };
  110. } // namespace system
  111. } // namespace entity
  112. #endif // ANTKEEPER_ENTITY_SYSTEM_SUBTERRAIN_HPP