Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. bool BoundingBox::intersects(const raytracer::Ray& ray, float& distance) const
  2.     {
  3.         //Lamda to calculate the intersection distance
  4.         const auto calculateT = [&](float invDir, float origin, float min, float max, float& tMin, float& tMax)
  5.         {
  6.             if (invDir >= 0)
  7.             {
  8.                 tMin = invDir * (min - origin);
  9.                 tMax = invDir * (max - origin);
  10.             }
  11.             else
  12.             {
  13.                 tMin = invDir * (max - origin);
  14.                 tMax = invDir * (min - origin);
  15.             }
  16.         };
  17.  
  18.         if(!b_hasVolume)
  19.         {
  20.             return false;
  21.         }
  22.  
  23.         vec3 tMin, tMax;
  24.  
  25.         const vec3& inverseDirection = ray.getInverseDirection();
  26.         const vec3 origin = ray.getOrigin();
  27.  
  28.         //Calculate all the min and max intersections.
  29.         calculateT(inverseDirection.m_X, origin.m_X, min.m_X, max.m_X, tMin.m_X, tMax.m_X);
  30.         calculateT(inverseDirection.m_Y, origin.m_Y, min.m_Y, max.m_Y, tMin.m_Y, tMax.m_Y);
  31.         calculateT(inverseDirection.m_Z, origin.m_Z, min.m_Z, max.m_Z, tMin.m_Z, tMax.m_Z);
  32.  
  33.        
  34.         /*
  35.          * Get the maximum min distance, and the minimum max distane.
  36.          * These are the bounds at which all three axes are within the shape along the ray.
  37.          * If max <= min, no intersection occurs.
  38.          */
  39.         const float minIntersectDistance = std::max(tMin.m_X, std::max(tMin.m_Y, tMin.m_Z));
  40.         const float maxIntersectDistance = std::min(tMax.m_X, std::min(tMax.m_Y, tMax.m_Z));
  41.  
  42.         //There is segment along the line that is within the box
  43.         if(minIntersectDistance < maxIntersectDistance)
  44.         {
  45.             //Min intersect is negative.
  46.             if(minIntersectDistance < 0.f)
  47.             {
  48.                 //Max intersect is also negative, so there is no intersection (all happens behind the ray origin).
  49.                 if(maxIntersectDistance < 0.f)
  50.                 {
  51.                     return false;
  52.                 }
  53.  
  54.                 //There is an intersection, but the ray originates within this box.
  55.                 //That means that it should ALWAYS be checked before other boxes that may be inside it.
  56.                 //Therefore, distance is 0.
  57.                 distance = 0.f; //maxIntersectDistance (normally you'd want to return this).
  58.                 return true;
  59.             }
  60.             distance = minIntersectDistance;
  61.             return true;
  62.         }
  63.  
  64.         return false;
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement