portal_tracer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* portal_tracer.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_TRACER_H
  31. #define PORTAL_TRACER_H
  32. #include "core/bitfield_dynamic.h"
  33. #include "core/local_vector.h"
  34. #include "portal_occlusion_culler.h"
  35. #include "portal_types.h"
  36. #ifdef TOOLS_ENABLED
  37. // use this for checking for instance lifetime errors, disable normally
  38. //#define PORTAL_RENDERER_STORE_MOVING_RIDS
  39. #endif
  40. struct CameraMatrix;
  41. class PortalRenderer;
  42. struct VSRoom;
  43. class PortalTracer {
  44. public:
  45. // a bitfield for which statics have been hit this time,
  46. // and a list of showing statics
  47. class TraceResult {
  48. public:
  49. void create(int p_num_statics) {
  50. bf_visible_statics.create(p_num_statics);
  51. }
  52. void clear() {
  53. bf_visible_statics.blank();
  54. visible_static_ids.clear();
  55. visible_roamer_pool_ids.clear();
  56. }
  57. BitFieldDynamic bf_visible_statics;
  58. LocalVector<uint32_t> visible_static_ids;
  59. LocalVector<uint32_t> visible_roamer_pool_ids;
  60. };
  61. struct TraceParams {
  62. int start_room_id;
  63. bool use_pvs;
  64. uint8_t *decompressed_room_pvs;
  65. };
  66. // The recursive visibility function needs to allocate lists of planes each time a room is traversed.
  67. // Instead of doing this allocation on the fly we will use a pool which should be much faster and nearer
  68. // constant time.
  69. // Note this simple pool isn't super optimal but should be fine for now.
  70. class PlanesPool {
  71. public:
  72. // maximum number of vectors in the pool
  73. const static int POOL_MAX = 32;
  74. void reset();
  75. // request a new vector of planes .. returns the pool id, or -1 if pool is empty
  76. unsigned int request();
  77. // return pool id to the pool
  78. void free(unsigned int ui);
  79. LocalVector<Plane> &get(unsigned int ui) { return _planes[ui]; }
  80. PlanesPool();
  81. private:
  82. LocalVector<Plane> _planes[POOL_MAX];
  83. // list of pool ids that are free and can be allocated
  84. uint8_t _freelist[POOL_MAX];
  85. uint32_t _num_free;
  86. };
  87. // for debugging, instead of doing a normal trace, show the objects that are sprawled from the current room
  88. void trace_debug_sprawl(PortalRenderer &p_portal_renderer, const Vector3 &p_pos, int p_start_room_id, TraceResult &r_result);
  89. // trace statics, dynamics and roaming
  90. void trace(PortalRenderer &p_portal_renderer, const Vector3 &p_pos, const LocalVector<Plane> &p_planes, int p_start_room_id, TraceResult &r_result);
  91. // globals are handled separately as they don't care about the rooms
  92. int trace_globals(const LocalVector<Plane> &p_planes, VSInstance **p_result_array, int first_result, int p_result_max, uint32_t p_mask, bool p_override_camera);
  93. void set_depth_limit(int p_limit) { _depth_limit = p_limit; }
  94. int get_depth_limit() const { return _depth_limit; }
  95. // special function for occlusion culling only that does not use portals / rooms,
  96. // but allows using occluders with the main scene
  97. int occlusion_cull(PortalRenderer &p_portal_renderer, const Vector3 &p_point, const Vector3 &p_cam_dir, const CameraMatrix &p_cam_matrix, const Vector<Plane> &p_convex, VSInstance **p_result_array, int p_num_results);
  98. PortalOcclusionCuller &get_occlusion_culler() { return _occlusion_culler; }
  99. const PortalOcclusionCuller &get_occlusion_culler() const { return _occlusion_culler; }
  100. private:
  101. // main tracing function is recursive
  102. void trace_recursive(const TraceParams &p_params, int p_depth, int p_room_id, const LocalVector<Plane> &p_planes, int p_from_external_room_id = -1);
  103. // use pvs to cull instead of dynamically using portals
  104. // this is a faster trace but less accurate. Only possible if PVS has been generated.
  105. void trace_pvs(int p_source_room_id, const LocalVector<Plane> &p_planes);
  106. // debug version
  107. void trace_debug_sprawl_recursive(int p_depth, int p_room_id);
  108. void cull_statics(const VSRoom &p_room, const LocalVector<Plane> &p_planes);
  109. void cull_statics_debug_sprawl(const VSRoom &p_room);
  110. void cull_roamers(const VSRoom &p_room, const LocalVector<Plane> &p_planes);
  111. // if an aabb is in front of any of the culling planes, it can't be seen so returns false
  112. bool test_cull_inside(const AABB &p_aabb, const LocalVector<Plane> &p_planes, bool p_test_explicit_near_plane = true) const {
  113. for (unsigned int p = 0; p < p_planes.size(); p++) {
  114. real_t r_min, r_max;
  115. p_aabb.project_range_in_plane(p_planes[p], r_min, r_max);
  116. if (r_min > 0.0) {
  117. return false;
  118. }
  119. }
  120. if (p_test_explicit_near_plane) {
  121. real_t r_min, r_max;
  122. p_aabb.project_range_in_plane(_near_and_far_planes[0], r_min, r_max);
  123. if (r_min > 0.0) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. // local versions to prevent passing around the recursive functions
  130. PortalRenderer *_portal_renderer = nullptr;
  131. Vector3 _trace_start_point;
  132. TraceResult *_result = nullptr;
  133. Plane _near_and_far_planes[2];
  134. PlanesPool _planes_pool;
  135. int _depth_limit = 16;
  136. PortalOcclusionCuller _occlusion_culler;
  137. // keep a tick count for each trace, to avoid adding a visible
  138. // object to the hit list more than once per tick
  139. // (this makes more sense than bitfield for moving objects)
  140. uint32_t _tick = 0;
  141. };
  142. #endif // PORTAL_TRACER_H