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

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