static_raycaster.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**************************************************************************/
  2. /* static_raycaster.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 STATIC_RAYCASTER_H
  31. #define STATIC_RAYCASTER_H
  32. #include "core/object/ref_counted.h"
  33. #if !defined(__aligned)
  34. #if defined(_WIN32) && defined(_MSC_VER)
  35. #define __aligned(...) __declspec(align(__VA_ARGS__))
  36. #else
  37. #define __aligned(...) __attribute__((aligned(__VA_ARGS__)))
  38. #endif
  39. #endif
  40. class StaticRaycaster : public RefCounted {
  41. GDCLASS(StaticRaycaster, RefCounted)
  42. protected:
  43. static StaticRaycaster *(*create_function)();
  44. public:
  45. // compatible with embree3 rays
  46. struct __aligned(16) Ray {
  47. const static unsigned int INVALID_GEOMETRY_ID = ((unsigned int)-1); // from rtcore_common.h
  48. /*! Default construction does nothing. */
  49. _FORCE_INLINE_ Ray() :
  50. geomID(INVALID_GEOMETRY_ID) {}
  51. /*! Constructs a ray from origin, direction, and ray segment. Near
  52. * has to be smaller than far. */
  53. _FORCE_INLINE_ Ray(const Vector3 &p_org,
  54. const Vector3 &p_dir,
  55. float p_tnear = 0.0f,
  56. float p_tfar = INFINITY) :
  57. org(p_org),
  58. tnear(p_tnear),
  59. dir(p_dir),
  60. time(0.0f),
  61. tfar(p_tfar),
  62. mask(-1),
  63. u(0.0),
  64. v(0.0),
  65. primID(INVALID_GEOMETRY_ID),
  66. geomID(INVALID_GEOMETRY_ID),
  67. instID(INVALID_GEOMETRY_ID) {}
  68. /*! Tests if we hit something. */
  69. _FORCE_INLINE_ explicit operator bool() const { return geomID != INVALID_GEOMETRY_ID; }
  70. public:
  71. Vector3 org; //!< Ray origin + tnear
  72. float tnear; //!< Start of ray segment
  73. Vector3 dir; //!< Ray direction + tfar
  74. float time; //!< Time of this ray for motion blur.
  75. float tfar; //!< End of ray segment
  76. unsigned int mask; //!< used to mask out objects during traversal
  77. unsigned int id; //!< ray ID
  78. unsigned int flags; //!< ray flags
  79. Vector3 normal; //!< Not normalized geometry normal
  80. float u; //!< Barycentric u coordinate of hit
  81. float v; //!< Barycentric v coordinate of hit
  82. unsigned int primID; //!< primitive ID
  83. unsigned int geomID; //!< geometry ID
  84. unsigned int instID; //!< instance ID
  85. };
  86. virtual bool intersect(Ray &p_ray) = 0;
  87. virtual void intersect(Vector<Ray> &r_rays) = 0;
  88. virtual void add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) = 0;
  89. virtual void commit() = 0;
  90. virtual void set_mesh_filter(const HashSet<int> &p_mesh_ids) = 0;
  91. virtual void clear_mesh_filter() = 0;
  92. static Ref<StaticRaycaster> create();
  93. };
  94. #endif // STATIC_RAYCASTER_H