PageRenderTime 54ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/Rendering/ModelTests/LevelTests.cs

#
C# | 251 lines | 130 code | 21 blank | 100 comment | 4 complexity | 508c0853ea2aa8d3bc7690c689780311 MD5 | raw file
  1using System;
  2using Delta.ContentSystem.Rendering;
  3using Delta.Engine;
  4using Delta.InputSystem;
  5using Delta.Rendering.Basics.Drawing;
  6using Delta.Rendering.Cameras;
  7using Delta.Rendering.Models;
  8using Delta.Utilities.Datatypes;
  9using NUnit.Framework;
 10
 11namespace Delta.Rendering.ModelTests
 12{
 13	/// <summary>
 14	/// Level tests
 15	/// </summary>
 16	[Category("Visual")]
 17	internal class LevelTests
 18	{
 19		#region Start (Static)
 20		/// <summary>
 21		/// Start
 22		/// </summary>
 23		[Test]
 24		public static void Start()
 25		{
 26			//SimpleLevelTest();
 27			//GeneratedLevel();
 28			//CheckVsFrustum();
 29			//AdvancedFrustumTest();
 30		}
 31		#endregion
 32
 33
 34		///// <summary>
 35		///// Simple level test
 36		///// </summary>
 37		//public static void SimpleLevelTest()
 38		//{
 39		//  Level level = new Level(
 40		//    //"01ImperialPalace_Tokyo", "");
 41		//    "Jungle", "");
 42		//  //LookAtCamera cam = new LookAtCamera(new Vector(50, 370, 20),
 43		//  //	new Vector(50, 370, 0));
 44		//  LookAtCamera cam = new LookAtCamera(new Vector(5, -25, 15));
 45		//  Application.BackgroundColor = Color.Black;
 46		//  Application.Start(delegate
 47		//  {
 48		//    level.Draw();
 49		//    Grid.Draw();
 50		//  });
 51		//}
 52
 53
 54		///// <summary>
 55		///// Generated level
 56		///// </summary>
 57		//private static void GeneratedLevel()
 58		//{
 59		//  LevelMeshInfo[] meshInfos = new LevelMeshInfo[]
 60		//  {
 61		//    new LevelMeshInfo
 62		//    {
 63		//      Name = "StoneFloorB",
 64		//      WorldTransformations = new Matrix[]
 65		//      {
 66		//        Matrix.CreateTranslation(new Vector(0, 0, 0)),
 67		//      },
 68		//    },
 69		//    new LevelMeshInfo
 70		//    {
 71		//      Name = "Twinchurch",
 72		//      WorldTransformations = new Matrix[]
 73		//      {
 74		//        Matrix.CreateTranslation(new Vector(-10, 10, 0)),
 75		//        Matrix.CreateTranslation(new Vector(10, 10, 0)),
 76		//      },
 77		//    },
 78		//  };
 79
 80		//  LookAtCamera cam = new LookAtCamera(new Vector(0, -15, 15));
 81		//  Level level = new Level(new LevelData("<GeneratedLevel>", meshInfos));
 82
 83		//  Application.Start(delegate
 84		//  {
 85		//    Grid.Draw();
 86
 87		//    level.Draw();
 88		//  });
 89
 90		//}
 91
 92
 93		#region AdvancedFrustumTest (Static)
 94		/// <summary>
 95		/// Check vs frustum
 96		/// </summary>
 97		[Test]
 98		public static void AdvancedFrustumTest()
 99		{
100			// Create cameras
101			BaseCamera freeCamera = new FreeCamera(new Vector(0, 0, 1));
102			BaseCamera frustumCamera = new LookAtCamera(new Vector(0, 0, 0),
103				new Vector(0, 1, 0));
104			freeCamera.Activate();
105
106			// Create a simple material.
107			MaterialData materialData = new MaterialData
108			{
109				ShaderName = "TexturedShader3D",
110				DiffuseMapName = "",
111			};
112			// Simple sphere mesh used for the testing of the frustum.
113			Mesh sphereMesh = Mesh.CreateSphere("sphere", 0.3f, materialData);
114
115			// Variables for rotating the second camera.
116			Vector target = Vector.Zero;
117			float angle = 0.0f;
118			Application.Start(delegate
119			{
120				// Press space too rotate the frustumCamera around.
121				if (Input.Keyboard.SpaceIsPressed)
122				{
123					angle += Time.Delta * 2.0f;
124					target.X = (float)Math.Cos(angle);
125					target.Y = (float)Math.Sin(angle);
126					frustumCamera.Target = target;
127				}
128				// Set the current camera to be frustumCamera, place it nearly at Zero,
129				// then do a run on frustumCamera so the viewMatrix is correct.
130				frustumCamera.Activate();
131				frustumCamera.Position = new Vector(0.0f, 0.0f, 0.0f);
132				frustumCamera.Run();
133				// Get the ViewFrustum of the frustumCamera.
134				Plane[] frustum = frustumCamera.ViewFrustum;
135
136				// Center point on the farPlane.
137				Vector farPlanePos = frustumCamera.Position +
138				                     frustumCamera.LookDirection * frustumCamera.FarPlane;
139
140				// Center point on the nearPlane.
141				Vector nearPlanePos = frustumCamera.Position +
142				                      frustumCamera.LookDirection * frustumCamera.NearPlane;
143
144				// The height of the planes from the center.
145				float farPlaneHeight = 2.0f *
146				                       (float)Math.Tan(frustumCamera.FieldOfView / 2) *
147				                       frustumCamera.FarPlane;
148				float nearPlaneHeight = 2.0f *
149				                        (float)Math.Tan(frustumCamera.FieldOfView / 2) *
150				                        frustumCamera.NearPlane;
151				// The width of the planes from the center.
152				float farPlaneWidth = farPlaneHeight * ScreenSpace.AspectRatio;
153				float nearPlaneWidth = nearPlaneHeight * ScreenSpace.AspectRatio;
154
155				// Get the up and the right vectors from the viewMatrix.
156				Vector cameraUp = new Vector(frustumCamera.ViewMatrix.M11,
157					frustumCamera.ViewMatrix.M21, frustumCamera.ViewMatrix.M31);
158				Vector cameraRight = new Vector(frustumCamera.ViewMatrix.M12,
159					frustumCamera.ViewMatrix.M22, frustumCamera.ViewMatrix.M32);
160
161				// Get all the corners of the frustum planes.
162				// Far plane upper left corner
163				Vector farUpLeft = farPlanePos +
164				                   (cameraRight * farPlaneHeight / 2) -
165				                   (cameraUp * farPlaneWidth / 2);
166				// Far plane upper right corner
167				Vector farUpRight = farPlanePos +
168				                    (cameraRight * farPlaneHeight / 2) +
169				                    (cameraUp * farPlaneWidth / 2);
170				// Far plane lower right corner
171				Vector farDownRight = farPlanePos -
172				                      (cameraRight * farPlaneHeight / 2) +
173				                      (cameraUp * farPlaneWidth / 2);
174				// Far plane lower left corner
175				Vector farDownLeft = farPlanePos -
176				                     (cameraRight * farPlaneHeight / 2) -
177				                     (cameraUp * farPlaneWidth / 2);
178				// Near plane upper left corner
179				Vector nearUpLeft = nearPlanePos +
180				                    (cameraRight * nearPlaneHeight / 2) -
181				                    (cameraUp * nearPlaneWidth / 2);
182				// Near plane upper right corner
183				Vector nearUpRight = nearPlanePos +
184				                     (cameraRight * nearPlaneHeight / 2) +
185				                     (cameraUp * nearPlaneWidth / 2);
186				// Near plane lower right corner
187				Vector nearDownRight = nearPlanePos -
188				                       (cameraRight * nearPlaneHeight / 2) +
189				                       (cameraUp * nearPlaneWidth / 2);
190				// Near plane lower left corner
191				Vector nearDownLeft = nearPlanePos -
192				                      (cameraRight * nearPlaneHeight / 2) -
193				                      (cameraUp * nearPlaneWidth / 2);
194
195				// set current camera back to freeCamera and then do a run again too
196				// calculate viewMatrix and projection.
197				freeCamera.Activate();
198				freeCamera.Run();
199
200				// Draw lines representing the frustum.
201				// Near plane
202				Line.Draw(nearUpLeft, nearUpRight, Color.White);
203				Line.Draw(nearUpRight, nearDownRight, Color.White);
204				Line.Draw(nearDownRight, nearDownLeft, Color.White);
205				Line.Draw(nearDownLeft, nearUpLeft, Color.White);
206				// Far plane
207				Line.Draw(farUpLeft, farUpRight, Color.White);
208				Line.Draw(farUpRight, farDownRight, Color.White);
209				Line.Draw(farDownRight, farDownLeft, Color.White);
210				Line.Draw(farDownLeft, farUpLeft, Color.White);
211				// Right, left, up, down planes.
212				Line.Draw(farUpLeft, nearUpLeft, Color.White);
213				Line.Draw(farUpRight, nearUpRight, Color.White);
214				Line.Draw(farDownRight, nearDownRight, Color.White);
215				Line.Draw(farDownLeft, nearDownLeft, Color.White);
216
217				// place boxes all around the place with 2 units in between and then
218				// test too see if they are inside the frustum.
219				int startX = 25;
220				int startY = 25;
221				Vector Pos = new Vector(startX, startY, 0.0f);
222				for (int i = 0; i < startX; i++)
223				{
224					Pos.Y = startY;
225					for (int j = 0; j < startY; j++)
226					{
227						Vector boxWorldMax = sphereMesh.Geometry.Data.BoundingBox.Max + Pos;
228						Vector boxWorldMin = sphereMesh.Geometry.Data.BoundingBox.Min + Pos;
229						// if the sphere bounding box is visible then show a green box.
230						if (Level.IsInsideViewFrustum(sphereMesh, frustumCamera, Pos))
231						{
232							// Draw a green box when it is visible.
233							Box.DrawOutline(boxWorldMin, boxWorldMax, Color.Green);
234						} // if
235							// if the box is not visible then show a red box.
236						else
237						{
238							// Draw a red box when it is NOT visible.
239							Box.DrawOutline(boxWorldMin, boxWorldMax, Color.Red);
240						} // else
241						// Moving the next sphere Y.
242						Pos.Y -= 2.0f;
243					} // for
244					// Moving the next sphere X.
245					Pos.X -= 2.0f;
246				}
247			});
248		}
249		#endregion
250	}
251}