portal_rooms_bsp.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**************************************************************************/
  2. /* portal_rooms_bsp.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PORTAL_ROOMS_BSP_H
  31. #define PORTAL_ROOMS_BSP_H
  32. #include "core/local_vector.h"
  33. #include "core/math/aabb.h"
  34. #include "core/math/plane.h"
  35. class PortalRenderer;
  36. struct VSPortal;
  37. struct VSRoom;
  38. class PortalRoomsBSP {
  39. struct Node {
  40. Node() { clear(); }
  41. void clear() {
  42. leaf = false;
  43. child[0] = -1;
  44. child[1] = -1;
  45. }
  46. bool leaf;
  47. union {
  48. int32_t child[2];
  49. struct {
  50. int32_t first_id;
  51. int32_t num_ids;
  52. };
  53. };
  54. Plane plane;
  55. };
  56. LocalVector<Node, int32_t> _nodes;
  57. LocalVector<int32_t, int32_t> _room_ids;
  58. PortalRenderer *_portal_renderer = nullptr;
  59. const real_t _plane_epsilon = 0.001;
  60. public:
  61. // build the BSP on level start
  62. void create(PortalRenderer &r_portal_renderer);
  63. // clear data, and ready for a new level
  64. void clear() {
  65. _nodes.reset();
  66. _room_ids.reset();
  67. }
  68. // the main function, returns a shortlist of rooms that are possible for a test point
  69. const int32_t *find_shortlist(const Vector3 &p_pt, int &r_num_rooms) const;
  70. // This is a 'sticky' function, it prefers to stay in the previous room where possible.
  71. // This means there is a hysteresis for room choice that may occur if the user creates
  72. // overlapping rooms...
  73. int find_room_within(const PortalRenderer &p_portal_renderer, const Vector3 &p_pos, int p_previous_room_id) const;
  74. private:
  75. void build(int p_start_node_id, LocalVector<int32_t, int32_t> p_orig_room_ids);
  76. void detect_internal_room_containment(PortalRenderer &r_portal_renderer);
  77. int evaluate_portal(int p_portal_id, const LocalVector<int32_t, int32_t> &p_room_ids, LocalVector<int32_t, int32_t> *r_room_ids_back = nullptr, LocalVector<int32_t, int32_t> *r_room_ids_front = nullptr);
  78. int evaluate_room_split_plane(int p_room_a_id, int p_room_b_id, const LocalVector<int32_t, int32_t> &p_room_ids, Plane &r_plane, LocalVector<int32_t, int32_t> *r_room_ids_back = nullptr, LocalVector<int32_t, int32_t> *r_room_ids_front = nullptr);
  79. int evaluate_plane(const VSPortal *p_portal, const Plane &p_plane, const LocalVector<int32_t, int32_t> &p_room_ids, LocalVector<int32_t, int32_t> *r_room_ids_back = nullptr, LocalVector<int32_t, int32_t> *r_room_ids_front = nullptr);
  80. bool calculate_aabb_splitting_plane(const AABB &p_a, const AABB &p_b, Plane &r_plane) const;
  81. bool calculate_freeform_splitting_plane(const VSRoom &p_room_a, const VSRoom &p_room_b, Plane &r_plane) const;
  82. bool find_1d_split_point(real_t p_min_a, real_t p_max_a, real_t p_min_b, real_t p_max_b, real_t &r_split_point) const;
  83. bool test_freeform_plane(const LocalVector<Vector3, int32_t> &p_verts_a, const LocalVector<Vector3, int32_t> &p_verts_b, const Plane &p_plane) const;
  84. void debug_print_tree(int p_node_id = 0, int p_depth = 0);
  85. void _log(String p_string);
  86. };
  87. #endif // PORTAL_ROOMS_BSP_H