static public Frustum Transform(Frustum frustum, Matrix4X4 matrix) { Frustum transformedFrustum = new Frustum(); transformedFrustum.plane = new Plane[frustum.plane.Length]; for (int i = 0; i < frustum.plane.Length; ++i) { Vector3 planeNormal = frustum.plane[i].planeNormal; double distanceToPlane = frustum.plane[i].distanceToPlaneFromOrigin; transformedFrustum.plane[i].planeNormal = Vector3.TransformNormal(planeNormal, matrix); Vector3 pointOnPlane = planeNormal * distanceToPlane; Vector3 pointOnTransformedPlane = Vector3.TransformNormal(pointOnPlane, matrix); transformedFrustum.plane[i].distanceToPlaneFromOrigin = Vector3.Dot(transformedFrustum.plane[i].planeNormal, pointOnTransformedPlane); } return transformedFrustum; }
public void FrustumIntersetAABBTests() { { Frustum frustum = new Frustum( new Plane(new Vector3(1, 0, 0), 20), new Plane(new Vector3(-1, 0, 0), 20), new Plane(new Vector3(0, 1, 0), 20), new Plane(new Vector3(0, -1, 0), 20), new Plane(new Vector3(0, 0, 1), 20), new Plane(new Vector3(0, 0, -1), 20)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-30, -10, -10), new Vector3(-25, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-25, 0, -10), new Vector3(-15, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -5), new Vector3(5, 5, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { Frustum frustum = new Frustum( new Plane(new Vector3(-1, -1, 0), 0), new Plane(new Vector3(1, -1, 0), 0), new Plane(new Vector3(0, -1, -1), 0), new Plane(new Vector3(0, -1, 1), 0), new Plane(new Vector3(0, -1, 0), 0), new Plane(new Vector3(0, 1, 0), 10000)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, 100, -5), new Vector3(5, 110, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { // looking down -z Frustum frustum5PlaneNegZ = new Frustum( new Vector3(-1, 0, 1), new Vector3(-1, 0, 1), new Vector3(0, 1, 1), new Vector3(0, -1, 1), new Vector3(0, 0, -1), 10000); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -110), new Vector3(5, 5, -100)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } }
public void FrustumIntersetAABBTests() { { Frustum frustum = new Frustum( new Plane(new Vector3(1, 0, 0), 20), new Plane(new Vector3(-1, 0, 0), 20), new Plane(new Vector3(0, 1, 0), 20), new Plane(new Vector3(0, -1, 0), 20), new Plane(new Vector3(0, 0, 1), 20), new Plane(new Vector3(0, 0, -1), 20)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-30, -10, -10), new Vector3(-25, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-25, 0, -10), new Vector3(-15, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // not intersect { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-25, 0, 30), new Vector3(-15, 10, 35)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -5), new Vector3(5, 5, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { Frustum frustum = new Frustum( new Plane(new Vector3(-1, -1, 0), 0), new Plane(new Vector3(1, -1, 0), 0), new Plane(new Vector3(0, -1, -1), 0), new Plane(new Vector3(0, -1, 1), 0), new Plane(new Vector3(0, -1, 0), 0), new Plane(new Vector3(0, 1, 0), 10000)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, 100, -5), new Vector3(5, 110, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { // looking down -z Frustum frustum5PlaneNegZ = new Frustum( new Vector3(-1, 0, 1), new Vector3(-1, 0, 1), new Vector3(0, 1, 1), new Vector3(0, -1, 1), new Vector3(0, 0, -1), 10000); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -110), new Vector3(5, 5, -100)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } }