PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Rendering/ModelTests/LevelTests.cs

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