BsRay.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRay.h"
  4. #include "BsPlane.h"
  5. #include "BsSphere.h"
  6. #include "BsAABox.h"
  7. namespace BansheeEngine
  8. {
  9. void Ray::transform(const Matrix4& matrix)
  10. {
  11. Vector3 end = getPoint(1.0f);
  12. mOrigin = matrix.multiply(mOrigin);
  13. end = matrix.multiply(end);
  14. mDirection = Vector3::normalize(end - mOrigin);
  15. }
  16. void Ray::transformAffine(const Matrix4& matrix)
  17. {
  18. Vector3 end = getPoint(1.0f);
  19. mOrigin = matrix.multiplyAffine(mOrigin);
  20. end = matrix.multiplyAffine(end);
  21. mDirection = Vector3::normalize(end - mOrigin);
  22. }
  23. std::pair<bool, float> Ray::intersects(const Plane& p) const
  24. {
  25. return p.intersects(*this);
  26. }
  27. std::pair<bool, float> Ray::intersects(const Sphere& s) const
  28. {
  29. return s.intersects(*this);
  30. }
  31. std::pair<bool, float> Ray::intersects(const AABox& box) const
  32. {
  33. return box.intersects(*this);
  34. }
  35. std::pair<bool, float> Ray::intersects(const Vector3& a,
  36. const Vector3& b, const Vector3& c, const Vector3& normal,
  37. bool positiveSide, bool negativeSide) const
  38. {
  39. // Calculate intersection with plane.
  40. float t;
  41. {
  42. float denom = normal.dot(getDirection());
  43. // Check intersect side
  44. if (denom > + std::numeric_limits<float>::epsilon())
  45. {
  46. if (!negativeSide)
  47. return std::pair<bool, float>(false, 0.0f);
  48. }
  49. else if (denom < - std::numeric_limits<float>::epsilon())
  50. {
  51. if (!positiveSide)
  52. return std::pair<bool, float>(false, 0.0f);
  53. }
  54. else
  55. {
  56. // Parallel or triangle area is close to zero when
  57. // the plane normal not normalized.
  58. return std::pair<bool, float>(false, 0.0f);
  59. }
  60. t = normal.dot(a - getOrigin()) / denom;
  61. if (t < 0)
  62. {
  63. // Intersection is behind origin
  64. return std::pair<bool, float>(false, 0.0f);
  65. }
  66. }
  67. // Calculate the largest area projection plane in X, Y or Z.
  68. UINT32 i0, i1;
  69. {
  70. float n0 = Math::abs(normal[0]);
  71. float n1 = Math::abs(normal[1]);
  72. float n2 = Math::abs(normal[2]);
  73. i0 = 1; i1 = 2;
  74. if (n1 > n2)
  75. {
  76. if (n1 > n0) i0 = 0;
  77. }
  78. else
  79. {
  80. if (n2 > n0) i1 = 0;
  81. }
  82. }
  83. // Check the intersection point is inside the triangle.
  84. {
  85. float u1 = b[i0] - a[i0];
  86. float v1 = b[i1] - a[i1];
  87. float u2 = c[i0] - a[i0];
  88. float v2 = c[i1] - a[i1];
  89. float u0 = t * getDirection()[i0] + getOrigin()[i0] - a[i0];
  90. float v0 = t * getDirection()[i1] + getOrigin()[i1] - a[i1];
  91. float alpha = u0 * v2 - u2 * v0;
  92. float beta = u1 * v0 - u0 * v1;
  93. float area = u1 * v2 - u2 * v1;
  94. // Epsilon to avoid float precision errors.
  95. const float EPSILON = 1e-6f;
  96. float tolerance = - EPSILON * area;
  97. if (area > 0)
  98. {
  99. if (alpha < tolerance || beta < tolerance || alpha+beta > area-tolerance)
  100. return std::pair<bool, float>(false, 0.0f);
  101. }
  102. else
  103. {
  104. if (alpha > tolerance || beta > tolerance || alpha+beta < area-tolerance)
  105. return std::pair<bool, float>(false, 0.0f);
  106. }
  107. }
  108. return std::pair<bool, float>(true, t);
  109. }
  110. }