portal_occlusion_culler.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**************************************************************************/
  2. /* portal_occlusion_culler.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_OCCLUSION_CULLER_H
  31. #define PORTAL_OCCLUSION_CULLER_H
  32. class PortalRenderer;
  33. #include "core/math/camera_matrix.h"
  34. #include "core/math/geometry.h"
  35. #include "portal_types.h"
  36. class PortalOcclusionCuller {
  37. enum {
  38. MAX_SPHERES = 64,
  39. MAX_POLYS = 64,
  40. };
  41. class Clipper {
  42. public:
  43. real_t clip_and_find_poly_area(const Plane *p_verts, int p_num_verts);
  44. private:
  45. enum Boundary {
  46. B_LEFT,
  47. B_RIGHT,
  48. B_TOP,
  49. B_BOTTOM,
  50. B_NEAR,
  51. B_FAR,
  52. };
  53. bool is_inside(const Plane &p_pt, Boundary p_boundary);
  54. Plane intersect(const Plane &p_a, const Plane &p_b, Boundary p_boundary);
  55. void debug_print_points(String p_string);
  56. Plane interpolate(const Plane &p_a, const Plane &p_b, real_t p_t) const;
  57. bool clip_to_plane(real_t a, real_t b, real_t c, real_t d);
  58. LocalVectori<Plane> _pts_in;
  59. LocalVectori<Plane> _pts_out;
  60. // after perspective divide
  61. LocalVectori<Vector3> _pts_final;
  62. template <typename T>
  63. int sgn(T val) {
  64. return (T(0) < val) - (val < T(0));
  65. }
  66. };
  67. public:
  68. PortalOcclusionCuller();
  69. void prepare_camera(const CameraMatrix &p_cam_matrix, const Vector3 &p_cam_dir) {
  70. _matrix_camera = p_cam_matrix;
  71. _pt_cam_dir = p_cam_dir;
  72. }
  73. void prepare(PortalRenderer &p_portal_renderer, const VSRoom &p_room, const Vector3 &pt_camera, const LocalVector<Plane> &p_planes, const Plane *p_near_plane) {
  74. if (p_near_plane) {
  75. static LocalVector<Plane> local_planes;
  76. int size_wanted = p_planes.size() + 1;
  77. if ((int)local_planes.size() != size_wanted) {
  78. local_planes.resize(size_wanted);
  79. }
  80. for (int n = 0; n < (int)p_planes.size(); n++) {
  81. local_planes[n] = p_planes[n];
  82. }
  83. local_planes[size_wanted - 1] = *p_near_plane;
  84. prepare_generic(p_portal_renderer, p_room._occluder_pool_ids, pt_camera, local_planes);
  85. } else {
  86. prepare_generic(p_portal_renderer, p_room._occluder_pool_ids, pt_camera, p_planes);
  87. }
  88. }
  89. void prepare_generic(PortalRenderer &p_portal_renderer, const LocalVector<uint32_t, uint32_t> &p_occluder_pool_ids, const Vector3 &pt_camera, const LocalVector<Plane> &p_planes);
  90. bool cull_aabb(const AABB &p_aabb) const {
  91. if (!_occluders_present) {
  92. return false;
  93. }
  94. if (cull_aabb_to_polys(p_aabb)) {
  95. return true;
  96. }
  97. return cull_sphere(p_aabb.get_center(), p_aabb.size.length() * 0.5, -1, false);
  98. }
  99. bool cull_sphere(const Vector3 &p_occludee_center, real_t p_occludee_radius, int p_ignore_sphere = -1, bool p_cull_to_polys = true) const;
  100. Geometry::MeshData debug_get_current_polys() const;
  101. static bool _redraw_gizmo;
  102. private:
  103. bool cull_sphere_to_spheres(const Vector3 &p_occludee_center, real_t p_occludee_radius, const Vector3 &p_ray_dir, real_t p_dist_to_occludee, int p_ignore_sphere) const;
  104. bool cull_sphere_to_polys(const Vector3 &p_occludee_center, real_t p_occludee_radius) const;
  105. bool cull_aabb_to_polys(const AABB &p_aabb) const;
  106. // experimental
  107. bool cull_aabb_to_polys_ex(const AABB &p_aabb) const;
  108. bool _is_poly_of_interest_to_split_plane(const Plane *p_poly_split_plane, int p_poly_id) const;
  109. // if a sphere is entirely in front of any of the culling planes, it can't be seen so returns false
  110. bool is_sphere_culled(const Vector3 &p_pos, real_t p_radius, const LocalVector<Plane> &p_planes) const {
  111. for (unsigned int p = 0; p < p_planes.size(); p++) {
  112. real_t dist = p_planes[p].distance_to(p_pos);
  113. if (dist > p_radius) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. bool is_aabb_culled(const AABB &p_aabb, const LocalVector<Plane> &p_planes) const {
  120. const Vector3 &size = p_aabb.size;
  121. Vector3 half_extents = size * 0.5;
  122. Vector3 ofs = p_aabb.position + half_extents;
  123. for (unsigned int i = 0; i < p_planes.size(); i++) {
  124. const Plane &p = p_planes[i];
  125. Vector3 point(
  126. (p.normal.x > 0) ? -half_extents.x : half_extents.x,
  127. (p.normal.y > 0) ? -half_extents.y : half_extents.y,
  128. (p.normal.z > 0) ? -half_extents.z : half_extents.z);
  129. point += ofs;
  130. if (p.is_point_over(point)) {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. bool calculate_poly_goodness_of_fit(const VSOccluder_Poly &p_opoly, real_t &r_fit);
  137. void whittle_polys();
  138. void precalc_poly_edge_planes(const Vector3 &p_pt_camera);
  139. // If all the points of the poly are beyond one of the planes (e.g. frustum), it is completely culled.
  140. bool is_poly_culled(const Occlusion::PolyPlane &p_opoly, const LocalVector<Plane> &p_planes) const {
  141. for (unsigned int p = 0; p < p_planes.size(); p++) {
  142. const Plane &plane = p_planes[p];
  143. int points_outside = 0;
  144. for (int n = 0; n < p_opoly.num_verts; n++) {
  145. const Vector3 &pt = p_opoly.verts[n];
  146. if (!plane.is_point_over(pt)) {
  147. break;
  148. } else {
  149. points_outside++;
  150. }
  151. }
  152. if (points_outside == p_opoly.num_verts) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. // All the points of the poly must be within ALL the planes to return true.
  159. struct PlaneSet;
  160. bool is_poly_inside_occlusion_volume(const Occlusion::Poly &p_test_poly, const Plane &p_occluder_plane, const PlaneSet &p_planeset) const {
  161. // first test against the occluder poly plane
  162. for (int n = 0; n < p_test_poly.num_verts; n++) {
  163. const Vector3 &pt = p_test_poly.verts[n];
  164. if (p_occluder_plane.is_point_over(pt)) {
  165. return false;
  166. }
  167. }
  168. for (int p = 0; p < p_planeset.num_planes; p++) {
  169. const Plane &plane = p_planeset.planes[p];
  170. for (int n = 0; n < p_test_poly.num_verts; n++) {
  171. const Vector3 &pt = p_test_poly.verts[n];
  172. if (plane.is_point_over(pt)) {
  173. return false;
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. bool is_poly_touching_hole(const Occlusion::Poly &p_opoly, const PlaneSet &p_planeset) const {
  180. if (!p_opoly.num_verts) {
  181. // should not happen?
  182. return false;
  183. }
  184. // find aabb
  185. AABB bb;
  186. bb.position = p_opoly.verts[0];
  187. for (int n = 1; n < p_opoly.num_verts; n++) {
  188. bb.expand_to(p_opoly.verts[n]);
  189. }
  190. // if the AABB is totally outside any edge, it is safe for a hit
  191. real_t omin, omax;
  192. for (int e = 0; e < p_planeset.num_planes; e++) {
  193. // edge plane to camera
  194. const Plane &plane = p_planeset.planes[e];
  195. bb.project_range_in_plane(plane, omin, omax);
  196. // if inside the hole, no longer a hit on this poly
  197. if (omin > 0.0) {
  198. return false;
  199. }
  200. } // for e
  201. return true;
  202. }
  203. void log(String p_string, int p_depth = 0) const;
  204. // only a number of the spheres in the scene will be chosen to be
  205. // active based on their distance to the camera, screen space etc.
  206. Occlusion::Sphere _spheres[MAX_SPHERES];
  207. real_t _sphere_distances[MAX_SPHERES];
  208. real_t _sphere_closest_dist = 0.0;
  209. int _num_spheres = 0;
  210. int _max_spheres = 8;
  211. struct SortPoly {
  212. enum SortPolyFlags {
  213. SPF_FACES_CAMERA = 1,
  214. SPF_DONE = 2,
  215. SPF_TESTED_AS_OCCLUDER = 4,
  216. SPF_HAS_HOLES = 8,
  217. };
  218. Occlusion::PolyPlane poly;
  219. uint32_t flags;
  220. #ifdef TOOLS_ENABLED
  221. uint32_t poly_source_id;
  222. #endif
  223. uint32_t mesh_source_id;
  224. real_t goodness_of_fit;
  225. };
  226. struct PlaneSet {
  227. void flip() {
  228. for (int n = 0; n < num_planes; n++) {
  229. planes[n] = -planes[n];
  230. }
  231. }
  232. // pre-calculated edge planes to the camera
  233. int num_planes = 0;
  234. Plane planes[PortalDefines::OCCLUSION_POLY_MAX_VERTS];
  235. };
  236. struct PreCalcedPoly {
  237. void flip() {
  238. edge_planes.flip();
  239. for (int n = 0; n < num_holes; n++) {
  240. hole_edge_planes[n].flip();
  241. }
  242. }
  243. int num_holes = 0;
  244. PlaneSet edge_planes;
  245. PlaneSet hole_edge_planes[PortalDefines::OCCLUSION_POLY_MAX_HOLES];
  246. Occlusion::Poly hole_polys[PortalDefines::OCCLUSION_POLY_MAX_HOLES];
  247. };
  248. SortPoly _polys[MAX_POLYS];
  249. PreCalcedPoly _precalced_poly[MAX_POLYS];
  250. int _num_polys = 0;
  251. int _max_polys = 8;
  252. #ifdef TOOLS_ENABLED
  253. uint32_t _poly_checksum = 0;
  254. #endif
  255. Vector3 _pt_camera;
  256. Vector3 _pt_cam_dir;
  257. CameraMatrix _matrix_camera;
  258. PortalRenderer *_portal_renderer = nullptr;
  259. Clipper _clipper;
  260. bool _occluders_present = false;
  261. static bool _debug_log;
  262. };
  263. #endif // PORTAL_OCCLUSION_CULLER_H