Advertisement
vexator

Untitled

Oct 9th, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. // DECLARATION:
  2.  
  3. class OrientedBoundingBox : public BoundingBox
  4. {
  5.     public:
  6.    
  7.         mat3 rotation;
  8.  
  9.     public:
  10.  
  11.         OrientedBoundingBox( vec3 minimum, vec3 maximum, vec3 position = vec3(0.0f), mat3 rotation = mat3() ) : BoundingBox( minimum, maximum, position )
  12.         {
  13.             this->rotation = rotation;
  14.         }
  15. };
  16.  
  17. // IMPLEMENTATION #1:
  18.  
  19. BoundingVolume::TestResult Frustum::testIntersection( shared_ptr<const OrientedBoundingBox> box ) const
  20. {
  21.     const vec3 center = box->position+(box->maximum+box->minimum)*0.5f;
  22.     const vec3 extents = box->position+(box->maximum-box->minimum)*0.5f;
  23.  
  24.     vec3 extentsRotated = box->rotation * extents;
  25.  
  26.     extentsRotated.x = fabsf( extentsRotated.x );
  27.     extentsRotated.y = fabsf( extentsRotated.y );
  28.     extentsRotated.z = fabsf( extentsRotated.z );
  29.  
  30.     for( uint i = 0; i < 6; ++i )
  31.     {
  32.         vec3 positiveVertex = center;
  33.         const vec3 normal = vec3(m_planes[i]);
  34.  
  35.         positiveVertex.x += ( normal.x < 0.0f ) ? (-extentsRotated.x) : extentsRotated.x;
  36.         positiveVertex.y += ( normal.y < 0.0f ) ? (-extentsRotated.y) : extentsRotated.y;
  37.         positiveVertex.z += ( normal.z < 0.0f ) ? (-extentsRotated.z) : extentsRotated.z;
  38.  
  39.         if( (glm::dot(normal, positiveVertex)+m_planes[i].w) < 0.0f )
  40.             return TEST_OUTSIDE;
  41.     }
  42.  
  43.     return TEST_INTERSECT;
  44. }
  45.  
  46. // IMPLEMENTATION #2:
  47.  
  48. BoundingVolume::TestResult Frustum::testIntersection( shared_ptr<const OrientedBoundingBox> box ) const
  49. {
  50.     const vec3 center = box->position+(box->maximum+box->minimum)*0.5f;
  51.     const vec3 extents = box->position+(box->maximum-box->minimum)*0.5f;
  52.  
  53.     for( uint i = 0; i < 6; i++ )
  54.     {  
  55.         const vec3 normal = vec3(m_planes[i]);
  56.  
  57.         float x = glm::dot(box->rotation[0], normal) <= 0.f ? extents.x : -extents.x;      
  58.         float y = glm::dot(box->rotation[1], normal) <= 0.f ? extents.y : -extents.y;      
  59.         float z = glm::dot(box->rotation[2], normal) <= 0.f ? extents.z : -extents.z;      
  60.    
  61.         const vec3 diag = x*box->rotation[0] + y*box->rotation[1] + z*box->rotation[2];    
  62.  
  63.         const vec3 nPoint = center - diag; 
  64.  
  65.         if( glm::dot(nPoint, normal) + m_planes[i].w <= 0.f )
  66.             return TEST_OUTSIDE;
  67.     }
  68.  
  69.     return TEST_INTERSECT;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement