Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DECLARATION:
- class OrientedBoundingBox : public BoundingBox
- {
- public:
- mat3 rotation;
- public:
- OrientedBoundingBox( vec3 minimum, vec3 maximum, vec3 position = vec3(0.0f), mat3 rotation = mat3() ) : BoundingBox( minimum, maximum, position )
- {
- this->rotation = rotation;
- }
- };
- // IMPLEMENTATION #1:
- BoundingVolume::TestResult Frustum::testIntersection( shared_ptr<const OrientedBoundingBox> box ) const
- {
- const vec3 center = box->position+(box->maximum+box->minimum)*0.5f;
- const vec3 extents = box->position+(box->maximum-box->minimum)*0.5f;
- vec3 extentsRotated = box->rotation * extents;
- extentsRotated.x = fabsf( extentsRotated.x );
- extentsRotated.y = fabsf( extentsRotated.y );
- extentsRotated.z = fabsf( extentsRotated.z );
- for( uint i = 0; i < 6; ++i )
- {
- vec3 positiveVertex = center;
- const vec3 normal = vec3(m_planes[i]);
- positiveVertex.x += ( normal.x < 0.0f ) ? (-extentsRotated.x) : extentsRotated.x;
- positiveVertex.y += ( normal.y < 0.0f ) ? (-extentsRotated.y) : extentsRotated.y;
- positiveVertex.z += ( normal.z < 0.0f ) ? (-extentsRotated.z) : extentsRotated.z;
- if( (glm::dot(normal, positiveVertex)+m_planes[i].w) < 0.0f )
- return TEST_OUTSIDE;
- }
- return TEST_INTERSECT;
- }
- // IMPLEMENTATION #2:
- BoundingVolume::TestResult Frustum::testIntersection( shared_ptr<const OrientedBoundingBox> box ) const
- {
- const vec3 center = box->position+(box->maximum+box->minimum)*0.5f;
- const vec3 extents = box->position+(box->maximum-box->minimum)*0.5f;
- for( uint i = 0; i < 6; i++ )
- {
- const vec3 normal = vec3(m_planes[i]);
- float x = glm::dot(box->rotation[0], normal) <= 0.f ? extents.x : -extents.x;
- float y = glm::dot(box->rotation[1], normal) <= 0.f ? extents.y : -extents.y;
- float z = glm::dot(box->rotation[2], normal) <= 0.f ? extents.z : -extents.z;
- const vec3 diag = x*box->rotation[0] + y*box->rotation[1] + z*box->rotation[2];
- const vec3 nPoint = center - diag;
- if( glm::dot(nPoint, normal) + m_planes[i].w <= 0.f )
- return TEST_OUTSIDE;
- }
- return TEST_INTERSECT;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement