diff --git a/ANX.Framework.TestCenter/Strukturen/BoundingFrustumTest.cs b/ANX.Framework.TestCenter/Strukturen/BoundingFrustumTest.cs index 7c29888c..98042585 100644 --- a/ANX.Framework.TestCenter/Strukturen/BoundingFrustumTest.cs +++ b/ANX.Framework.TestCenter/Strukturen/BoundingFrustumTest.cs @@ -216,6 +216,50 @@ namespace ANX.Framework.TestCenter.Strukturen Assert.Fail(String.Format("ContainsPoint failed: xna({0}) anx({1})", containsXNA.ToString(), containsANX.ToString())); } + [Test, TestCaseSource("thirtytwofloats")] + public void GetCornerArray( + float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44, + float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) + { + Microsoft.Xna.Framework.Matrix xnaMatrix = new Microsoft.Xna.Framework.Matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44); + Microsoft.Xna.Framework.BoundingFrustum xnaFrustum = new Microsoft.Xna.Framework.BoundingFrustum(xnaMatrix); + + ANX.Framework.Matrix anxMatrix = new ANX.Framework.Matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44); + ANX.Framework.BoundingFrustum anxFrustum = new ANX.Framework.BoundingFrustum(anxMatrix); + + XNAVector3[] xna = new XNAVector3[8]; + ANXVector3[] anx = new ANXVector3[8]; + + xnaFrustum.GetCorners(xna); + anxFrustum.GetCorners(anx); + + AssertHelper.ConvertEquals(xna, anx, "GetCornerArray"); + } + + [Test, TestCaseSource("thirtytwofloats"), ExpectedException(typeof(ArgumentNullException))] + public void GetCornerArray02( + float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44, + float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) + { + ANX.Framework.Matrix anxMatrix = new ANX.Framework.Matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44); + ANX.Framework.BoundingFrustum anxFrustum = new ANX.Framework.BoundingFrustum(anxMatrix); + + anxFrustum.GetCorners(null); + } + + [Test, TestCaseSource("thirtytwofloats"), ExpectedException(typeof(ArgumentOutOfRangeException))] + public void GetCornerArray03( + float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44, + float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) + { + ANX.Framework.Matrix anxMatrix = new ANX.Framework.Matrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44); + ANX.Framework.BoundingFrustum anxFrustum = new ANX.Framework.BoundingFrustum(anxMatrix); + + Vector3[] corners = new Vector3[2]; + + anxFrustum.GetCorners(corners); + } + [Test, TestCaseSource("thirtytwofloats")] public void GetCorners( float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44, diff --git a/ANX.Framework/BoundingFrustum.cs b/ANX.Framework/BoundingFrustum.cs index 69cf3ef7..7231ff37 100644 --- a/ANX.Framework/BoundingFrustum.cs +++ b/ANX.Framework/BoundingFrustum.cs @@ -561,7 +561,15 @@ namespace ANX.Framework public void GetCorners(Vector3[] corners) { - corners = this.corners; + if (corners == null) + { + throw new ArgumentNullException("corners"); + } + if (corners.Length < 8) + { + throw new ArgumentOutOfRangeException("corners", "The array to be filled with corner vertices needs at least have a length of 8 Vector3"); + } + this.corners.CopyTo(corners, 0); } public override int GetHashCode() @@ -824,38 +832,38 @@ namespace ANX.Framework #endregion #region private methods - //algorithm from: http://crazyjoke.free.fr/doc/3D/plane%20extraction.pdf + //algorithm based on but normals were pointing to outside instead of inside: http://crazyjoke.free.fr/doc/3D/plane%20extraction.pdf private void CreatePlanes() { - this.left.Normal.X = this.matrix.M14 + this.matrix.M11; - this.left.Normal.Y = this.matrix.M24 + this.matrix.M21; - this.left.Normal.Z = this.matrix.M34 + this.matrix.M31; - this.left.D = this.matrix.M44 + this.matrix.M41; + this.near.Normal.X = -this.matrix.M13; + this.near.Normal.Y = -this.matrix.M23; + this.near.Normal.Z = -this.matrix.M33; + this.near.D = -this.matrix.M43; - this.right.Normal.X = this.matrix.M14 - this.matrix.M11; - this.right.Normal.Y = this.matrix.M24 - this.matrix.M21; - this.right.Normal.Z = this.matrix.M34 - this.matrix.M31; - this.right.D = this.matrix.M44 - this.matrix.M41; + this.far.Normal.X = this.matrix.M14 - this.matrix.M13; + this.far.Normal.Y = this.matrix.M24 - this.matrix.M23; + this.far.Normal.Z = this.matrix.M34 - this.matrix.M33; + this.far.D = this.matrix.M44 - this.matrix.M43; - this.bottom.Normal.X = this.matrix.M14 + this.matrix.M12; - this.bottom.Normal.Y = this.matrix.M24 + this.matrix.M22; - this.bottom.Normal.Z = this.matrix.M34 + this.matrix.M32; - this.bottom.D = this.matrix.M44 + this.matrix.M42; + this.left.Normal.X = -this.matrix.M14 - this.matrix.M11; + this.left.Normal.Y = -this.matrix.M24 - this.matrix.M21; + this.left.Normal.Z = -this.matrix.M34 - this.matrix.M31; + this.left.D = -this.matrix.M44 - this.matrix.M41; - this.top.Normal.X = this.matrix.M14 - this.matrix.M12; - this.top.Normal.Y = this.matrix.M24 - this.matrix.M22; - this.top.Normal.Z = this.matrix.M34 - this.matrix.M32; - this.top.D = this.matrix.M44 - this.matrix.M42; + this.right.Normal.X = -this.matrix.M14 + this.matrix.M11; + this.right.Normal.Y = -this.matrix.M24 + this.matrix.M21; + this.right.Normal.Z = -this.matrix.M34 + this.matrix.M31; + this.right.D = -this.matrix.M44 + this.matrix.M41; - this.near.Normal.X = this.matrix.M13; - this.near.Normal.Y = this.matrix.M23; - this.near.Normal.Z = this.matrix.M33; - this.near.D = this.matrix.M43; + this.top.Normal.X = -this.matrix.M14 + this.matrix.M12; + this.top.Normal.Y = -this.matrix.M24 + this.matrix.M22; + this.top.Normal.Z = -this.matrix.M34 + this.matrix.M32; + this.top.D = -this.matrix.M44 + this.matrix.M42; - this.far.Normal.X = this.matrix.M14 - this.matrix.M13; - this.far.Normal.Y = this.matrix.M24 - this.matrix.M23; - this.far.Normal.Z = this.matrix.M34 - this.matrix.M33; - this.far.D = this.matrix.M44 - this.matrix.M43; + this.bottom.Normal.X = -this.matrix.M14 - this.matrix.M12; + this.bottom.Normal.Y = -this.matrix.M24 - this.matrix.M22; + this.bottom.Normal.Z = -this.matrix.M34 - this.matrix.M32; + this.bottom.D = -this.matrix.M44 - this.matrix.M42; NormalizePlane(ref this.left); NormalizePlane(ref this.right);