Ray.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "Base.h"
  2. #include "Ray.h"
  3. #include "Plane.h"
  4. #include "Frustum.h"
  5. #include "BoundingSphere.h"
  6. #include "BoundingBox.h"
  7. namespace gameplay
  8. {
  9. Ray::Ray()
  10. : _direction(0, 0, 1)
  11. {
  12. }
  13. Ray::Ray(const Vector3& origin, const Vector3& direction)
  14. {
  15. set(origin, direction);
  16. }
  17. Ray::Ray(float originX, float originY, float originZ, float dirX, float dirY, float dirZ)
  18. {
  19. set(Vector3(originX, originY, originZ), Vector3(dirX, dirY, dirZ));
  20. }
  21. Ray::Ray(const Ray& copy)
  22. {
  23. set(copy);
  24. }
  25. Ray::~Ray()
  26. {
  27. }
  28. const Vector3& Ray::getOrigin() const
  29. {
  30. return _origin;
  31. }
  32. void Ray::setOrigin(const Vector3& origin)
  33. {
  34. _origin = origin;
  35. }
  36. void Ray::setOrigin(float x, float y, float z)
  37. {
  38. _origin.set(x, y, z);
  39. }
  40. const Vector3& Ray::getDirection() const
  41. {
  42. return _direction;
  43. }
  44. void Ray::setDirection(const Vector3& direction)
  45. {
  46. _direction = direction;
  47. normalize();
  48. }
  49. void Ray::setDirection(float x, float y, float z)
  50. {
  51. _direction.set(x, y, z);
  52. normalize();
  53. }
  54. float Ray::intersects(const BoundingSphere& sphere) const
  55. {
  56. return sphere.intersects(*this);
  57. }
  58. float Ray::intersects(const BoundingBox& box) const
  59. {
  60. return box.intersects(*this);
  61. }
  62. float Ray::intersects(const Frustum& frustum) const
  63. {
  64. Plane n = frustum.getNear();
  65. float nD = intersects(n);
  66. float nOD = n.distance(_origin);
  67. Plane f = frustum.getFar();
  68. float fD = intersects(f);
  69. float fOD = f.distance(_origin);
  70. Plane l = frustum.getLeft();
  71. float lD = intersects(l);
  72. float lOD = l.distance(_origin);
  73. Plane r = frustum.getRight();
  74. float rD = intersects(r);
  75. float rOD = r.distance(_origin);
  76. Plane b = frustum.getBottom();
  77. float bD = intersects(b);
  78. float bOD = b.distance(_origin);
  79. Plane t = frustum.getTop();
  80. float tD = intersects(t);
  81. float tOD = t.distance(_origin);
  82. // If the ray's origin is in the negative half-space of one of the frustum's planes
  83. // and it does not intersect that same plane, then it does not intersect the frustum.
  84. if ((nOD < 0.0f && nD < 0.0f) || (fOD < 0.0f && fD < 0.0f) ||
  85. (lOD < 0.0f && lD < 0.0f) || (rOD < 0.0f && rD < 0.0f) ||
  86. (bOD < 0.0f && bD < 0.0f) || (tOD < 0.0f && tD < 0.0f))
  87. {
  88. return Ray::INTERSECTS_NONE;
  89. }
  90. // Otherwise, the intersection distance is the minimum positive intersection distance.
  91. float d = (nD > 0.0f) ? nD : 0.0f;
  92. d = (fD > 0.0f) ? ((d == 0.0f) ? fD : min(fD, d)) : d;
  93. d = (lD > 0.0f) ? ((d == 0.0f) ? lD : min(lD, d)) : d;
  94. d = (rD > 0.0f) ? ((d == 0.0f) ? rD : min(rD, d)) : d;
  95. d = (tD > 0.0f) ? ((d == 0.0f) ? bD : min(bD, d)) : d;
  96. d = (bD > 0.0f) ? ((d == 0.0f) ? tD : min(tD, d)) : d;
  97. return d;
  98. }
  99. float Ray::intersects(const Plane& plane) const
  100. {
  101. const Vector3& normal = plane.getNormal();
  102. // If the origin of the ray is on the plane then the distance is zero.
  103. float alpha = (normal.dot(_origin) + plane.getDistance());
  104. if (fabs(alpha) < MATH_EPSILON)
  105. {
  106. return 0.0f;
  107. }
  108. float dot = normal.dot(_direction);
  109. // If the dot product of the plane's normal and this ray's direction is zero,
  110. // then the ray is parallel to the plane and does not intersect it.
  111. if (dot == 0.0f)
  112. {
  113. return INTERSECTS_NONE;
  114. }
  115. // Calculate the distance along the ray's direction vector to the point where
  116. // the ray intersects the plane (if it is negative the plane is behind the ray).
  117. float d = -alpha / dot;
  118. if (d < 0.0f)
  119. {
  120. return INTERSECTS_NONE;
  121. }
  122. return d;
  123. }
  124. void Ray::set(const Vector3& origin, const Vector3& direction)
  125. {
  126. _origin = origin;
  127. _direction = direction;
  128. normalize();
  129. }
  130. void Ray::set(const Ray& ray)
  131. {
  132. _origin = ray._origin;
  133. _direction = ray._direction;
  134. normalize();
  135. }
  136. void Ray::transform(const Matrix& matrix)
  137. {
  138. matrix.transformPoint(&_origin);
  139. matrix.transformVector(&_direction);
  140. _direction.normalize();
  141. }
  142. void Ray::normalize()
  143. {
  144. if (_direction.isZero())
  145. {
  146. GP_ERROR("Invalid ray object; a ray's direction must be non-zero.");
  147. return;
  148. }
  149. // Normalize the ray's direction vector.
  150. float normalizeFactor = 1.0f / sqrt(_direction.x * _direction.x + _direction.y * _direction.y + _direction.z * _direction.z);
  151. if (normalizeFactor != 1.0f)
  152. {
  153. _direction.x *= normalizeFactor;
  154. _direction.y *= normalizeFactor;
  155. _direction.z *= normalizeFactor;
  156. }
  157. }
  158. }