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

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