bool Frustum::IntersectsBox(const AlignedBox& box, bool precise) const { MATH_FUNCTION_TIMER(); if ( fabs((box.maximum - box.minimum).Length()) < LinearIntersectionError ) { return IntersectsPoint( box.Center() ); } else { f32 m, n; Vector3 c = box.Center(); Vector3 d = box.maximum - c; for (i32 i=0; i<6; i++) { const Plane& p = (*this)[i]; m = (c.x * p.A()) + (c.y * p.B()) + (c.z * p.C()) + p.D(); n = (d.x * abs(p.A())) + (d.y * abs(p.B())) + (d.z * abs(p.C())); if (m + n < 0) { return false; } } if ( precise ) { #pragma TODO("This precise test should be using separated axis testing instead of triangle decimation") V_Vector3 vertices; vertices.reserve(8); box.GetVertices(vertices); V_Vector3 triangleList; triangleList.reserve(36); AlignedBox::GetTriangulated(vertices, triangleList); for ( int i=0; i<36; i+=3 ) { if ( IntersectsTriangle( triangleList[i], triangleList[i+1], triangleList[i+2] ) ) { return true; } } return false; } } return true; }
Frustum::Frustum(const AlignedBox& b) { V_Vector3 v; b.GetVertices(v); //top 3047 top = Plane(v[3], (v[3] - v[0]).Cross(v[4] - v[0])); //bottom 6512 bottom = Plane(v[6], (v[6] - v[5]).Vector3::Cross(v[1] - v[5])); //left 6237 left = Plane(v[6], (v[6] - v[2]).Vector3::Cross(v[3] - v[2])); //right 1540 right = Plane(v[1], (v[1] - v[5]).Cross(v[4] - v[5])); //front 2103 front = Plane(v[2], (v[2] - v[1]).Cross(v[0] - v[1])); //back 5674 back = Plane(v[5], (v[5] - v[6]).Cross(v[7] - v[6])); }
bool Frustum::Contains(const AlignedBox& box) const { MATH_FUNCTION_TIMER(); V_Vector3 verts; box.GetVertices(verts); for (size_t i=0; i<verts.size(); i++) { // for each plane for (i32 j=0; j<6; j++) { const Plane& p = (*this)[j]; // we need this vert to be above each plane if ((verts[i].x * p.A()) + (verts[i].y * p.B()) + (verts[i].z * p.C()) + p.D() < 0) { return false; } } } return true; }