PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Rendering/ModelTests/MeshTests.cs

#
C# | 515 lines | 320 code | 61 blank | 134 comment | 6 complexity | 346c65fbb9296d7b9992c6c8837a73f3 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;
  9. using Delta.Utilities.Datatypes;
  10. using Delta.Utilities.Graphics;
  11. using Delta.Utilities.Helpers;
  12. using NUnit.Framework;
  13. namespace Delta.Rendering.ModelTests
  14. {
  15. internal class MeshTests
  16. {
  17. #region TestCreateBox
  18. /// <summary>
  19. /// Test the create box method of MeshData.
  20. /// </summary>
  21. [Test, Category("Visual")]
  22. public static void TestCreateBox()
  23. {
  24. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  25. GeometryData data = GeometryData.CreateBox(
  26. new VertexFormat(new[]
  27. {
  28. new VertexElement(VertexElementType.Position3D),
  29. new VertexElement(VertexElementType.Normal),
  30. new VertexElement(VertexElementType.Tangent),
  31. }), 10, 10, 10, false, Color.White);
  32. Color faceColor = Color.Red;
  33. Color normalColor = Color.Yellow;
  34. Color tangentColor = Color.Purple;
  35. bool showTangent = false;
  36. float angle = 0f;
  37. Application.Start(delegate
  38. {
  39. angle += Time.Delta * 20f;
  40. cam.Position = new Vector(25f * MathHelper.Cos(angle),
  41. 20f * MathHelper.Sin(angle), cam.Position.Z);
  42. Grid.Draw();
  43. for (int i = 0; i < data.Indices.Length / 3; i++)
  44. {
  45. int startIndex = i * 3;
  46. Vector pos1 = data.GetPosition(
  47. data.Indices[startIndex]);
  48. Vector pos2 = data.GetPosition(
  49. data.Indices[startIndex + 1]);
  50. Vector pos3 = data.GetPosition(
  51. data.Indices[startIndex + 2]);
  52. Vector normal = new Vector(data.reader);
  53. Vector tangent = new Vector(data.reader);
  54. Line.Draw(pos1, pos2, faceColor);
  55. Line.Draw(pos2, pos3, faceColor);
  56. Line.Draw(pos3, pos1, faceColor);
  57. Vector center = (pos1 + pos2 + pos3) / 3f;
  58. if (showTangent)
  59. {
  60. Application.Window.Title = "Tangents";
  61. Line.Draw(center, center + tangent, tangentColor);
  62. }
  63. else
  64. {
  65. Application.Window.Title = "Normals";
  66. Line.Draw(center, center + normal, normalColor);
  67. }
  68. }
  69. if (Input.Mouse.LeftButtonReleased)
  70. {
  71. showTangent = !showTangent;
  72. }
  73. });
  74. }
  75. #endregion
  76. #region Start (EntryPoint from Main)
  77. /// <summary>
  78. /// Start
  79. /// </summary>
  80. [Test]
  81. public static void Start()
  82. {
  83. //DrawCreatePlane();
  84. //DrawCreateBox();
  85. //DrawCreateSphere();
  86. //DrawMesh();
  87. DrawAnimatedMesh();
  88. //SimpleAnimatedPlane();
  89. //ImportColladaTestBox();
  90. //DrawColladaTestBox();
  91. //DrawColladaBox2UVs();
  92. //DrawModel();
  93. //DrawMap();
  94. //DrawMayaModel();
  95. //Save();
  96. //TestModelWithTextureTiling();
  97. //DrawGoblin();
  98. //DrawAnimatedModel();
  99. //TestMultipleAnimations();
  100. //TestSmoothPlayAnimation();
  101. //TestFrameRate();
  102. }
  103. // Start()
  104. #endregion
  105. #region DrawCreatePlane
  106. /// <summary>
  107. /// Draw create plane
  108. /// </summary>
  109. [Test]
  110. public static void DrawCreatePlane()
  111. {
  112. // First we have define the material
  113. MaterialData materialData = new MaterialData
  114. {
  115. ShaderName = "TexturedShader3D",
  116. //unused: Ambient = Color.Yellow,
  117. //DiffuseMapName = "",
  118. //DiffuseMapName = "TestPlaneHighDiffuse",
  119. DiffuseMapName = "FountainHighDiffuse",
  120. };
  121. // And the model we wanna draw
  122. Mesh plane = Mesh.CreatePlane("ModelTests.DrawCreatePlane", 5, 5,
  123. materialData);
  124. // Initialize the camera which we wanna use in the scene
  125. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  126. Application.Start(delegate
  127. {
  128. plane.Draw();
  129. });
  130. }
  131. // DrawCreatePlane()
  132. #endregion
  133. #region DrawBox
  134. /// <summary>
  135. /// Draw create box
  136. /// </summary>
  137. [Test]
  138. public static void DrawBox()
  139. {
  140. Mesh box = Mesh.CreateBox("MeshTests.DrawBox", 5, 5, 5,
  141. MaterialData.Default);
  142. // Initialize the camera which we wanna use in the scene
  143. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  144. Application.Start(delegate
  145. {
  146. box.Draw();
  147. });
  148. }
  149. // DrawCreateBox()
  150. #endregion
  151. #region DrawColoredBox
  152. /// <summary>
  153. /// Draw colored box
  154. /// </summary>
  155. [Test]
  156. public static void DrawColoredBox()
  157. {
  158. // First we have define the material
  159. MaterialData materialData = new MaterialData
  160. {
  161. ShaderName = "TexturedColoredShader3D",
  162. Diffuse = Color.Yellow,
  163. DiffuseMapName = "",
  164. };
  165. // And the model we wanna draw
  166. Mesh box = Mesh.CreateBox("MeshTests.DrawColoredBox", 5, 5, 5,
  167. materialData);
  168. // Initialize the camera which we wanna use in the scene
  169. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  170. Application.Start(delegate
  171. {
  172. box.Draw();
  173. });
  174. }
  175. // DrawCreateBox()
  176. #endregion
  177. #region DrawCreateSphere
  178. /// <summary>
  179. /// Draw create sphere
  180. /// </summary>
  181. [Test]
  182. public static void DrawCreateSphere()
  183. {
  184. // First we have define the material
  185. MaterialData materialData = new MaterialData
  186. {
  187. ShaderName = "TexturedShader3D",
  188. Ambient = Color.Yellow,
  189. DiffuseMapName = "",
  190. };
  191. // And the model we wanna draw
  192. Mesh sphere = Mesh.CreateSphere("ModelTests.DrawCreateSphere", 0.1f,
  193. materialData);
  194. // Initialize the camera which we wanna use in the scene
  195. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  196. Application.Start(delegate
  197. {
  198. Grid.Draw();
  199. sphere.Draw();
  200. });
  201. }
  202. // DrawCreateSphere()
  203. #endregion
  204. #region DrawMesh
  205. /// <summary>
  206. /// Draw mesh
  207. /// </summary>
  208. [Test]
  209. public static void DrawMesh()
  210. {
  211. Mesh testMesh = new Mesh(
  212. //"Cock131"); // Strange name for Soulcraft level part test mesh!
  213. //"Twinchurch");
  214. //"TestPlane");
  215. //"Fountain");
  216. //"Statue");
  217. //"Colosseum");
  218. //"Bridge");
  219. //"BridgeFloor");
  220. //"BridgeEnd");
  221. //Note: Many models are messed up right now, but the level ones work!
  222. //"Bench");
  223. //"Column");
  224. //"LampB");
  225. //Note: This still has duplicate content bug!
  226. //"Skydome");
  227. //Note 2011-02-12: All three stopped working, but does not matter,
  228. // the animated ones work for the tech demo ...
  229. //"Abomination");
  230. //"Angel");
  231. //"Woman");
  232. "Firemage");
  233. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  234. // Test if we have loaded the normal correctly!
  235. //finally all works fine, last mipmaps were just missing (Marcels code)!
  236. //Material testMat = new Material(
  237. //now working, quadratic:
  238. //"AbominationHighNormal");
  239. //"AngelHighNormal");
  240. //now working, quadratic:
  241. //"WomanHighNormal");
  242. //not working, not quadratic:
  243. //"LampHighMediumLowNormal");
  244. //"InnerGroundBestHighMediumLowNormal");
  245. //"StatueHighMediumNormal");
  246. //not working, not quadratic:
  247. //"ObeliskHighMediumLowNormal");
  248. Application.Start(delegate
  249. {
  250. // Show the normal map in the top left corner.
  251. //testMat.Draw(new Rectangle(ScreenSpace.Area.Left, ScreenSpace.Area.Top,
  252. // 0.1f, 0.1f));
  253. //Font.Default.WriteTopLeft("Used shader: " + testMesh.Material);
  254. testMesh.Draw();
  255. //Grid.Draw();
  256. //Light.Draw();
  257. //disabled, not always available: Sky.Draw();
  258. });
  259. }
  260. // DrawMesh()
  261. #endregion
  262. #region DrawAnimatedMesh
  263. /// <summary>
  264. /// Draw animated mesh
  265. /// </summary>
  266. [Test]
  267. public static void DrawAnimatedMesh()
  268. {
  269. Mesh testMesh = new Mesh(
  270. //"Abomination");
  271. //"Angel");
  272. //"Woman");
  273. "Firemage");
  274. //"Demon");
  275. LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  276. Application.Start(delegate
  277. {
  278. testMesh.Draw();
  279. Grid.Draw();
  280. //Light.Draw();
  281. //disabled, not always available: Sky.Draw();
  282. });
  283. }
  284. // DrawAnimatedMesh()
  285. #endregion
  286. #region SimpleAnimatedPlane
  287. /// <summary>
  288. /// Simple animated plane
  289. /// </summary>
  290. [Test]
  291. private static void SimpleAnimatedPlane()
  292. {
  293. MaterialData planeMaterial = new MaterialData
  294. {
  295. // Skinned shader
  296. ShaderName = "SkinnedTextured",
  297. // Default diffuse map
  298. DiffuseMapName = "",
  299. };
  300. // And the model we wanna draw
  301. Mesh planeMesh = Mesh.CreatePlane("<SimpleAnimatedPlane>", 2, 2,
  302. planeMaterial);
  303. GeometryData planeData = planeMesh.Geometry.Data;
  304. // Animate only the right plane side
  305. planeData.SetSkinData(0, new Point(0, 0), new Point(1.0f, 0.0f));
  306. planeData.SetSkinData(1, new Point(1.0f, 1.0f), new Point(0.5f, 0.5f));
  307. planeData.SetSkinData(2, new Point(0, 0), new Point(1.0f, 0.0f));
  308. planeData.SetSkinData(3, new Point(1.0f, 1.0f), new Point(0.5f, 0.5f));
  309. Animation testAnimation = new Animation(planeMesh, null) //TODO?
  310. {
  311. Duration = 1,
  312. };
  313. float frameCount = 30;
  314. float degreeIncrease = 90 / frameCount;
  315. float currentDegree = -45f;
  316. Matrix[][] frames = new Matrix[(int)frameCount][];
  317. for (int frame = 0; frame < frameCount; frame++)
  318. {
  319. // we want to inverse the direction of the animation after the half
  320. // of the frames so we get a cycle animation.
  321. if (frame == frameCount / 2)
  322. {
  323. degreeIncrease *= -1;
  324. }
  325. currentDegree += degreeIncrease;
  326. frames[frame] = new[]
  327. {
  328. // The left plane side has no animation part.
  329. Matrix.Identity,
  330. // Rotate the right plane side up and down.
  331. Matrix.CreateRotationY(currentDegree),
  332. };
  333. }
  334. testAnimation.Frames = frames;
  335. // Let's view the scene now
  336. LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 5));
  337. Application.Start(delegate
  338. {
  339. Grid.Draw();
  340. testAnimation.Update();
  341. planeMesh.Draw();
  342. });
  343. }
  344. // SimpleAnimatedPlane()
  345. #endregion
  346. #region LoadContentMesh
  347. /// <summary>
  348. /// Load content mesh
  349. /// </summary>
  350. [Test]
  351. public static void LoadContentMesh()
  352. {
  353. string testMeshName = "Rocks";
  354. Mesh meshInstance1 = new Mesh(testMeshName);
  355. Assert.NotNull(meshInstance1.Geometry);
  356. Assert.NotNull(meshInstance1.Material);
  357. Mesh meshInstance2 = new Mesh(testMeshName);
  358. Assert.NotNull(meshInstance2.Geometry);
  359. Assert.NotNull(meshInstance2.Material);
  360. }
  361. #endregion
  362. #region DrawNoTexturedGeometry
  363. /// <summary>
  364. /// This test show the NoTexturing feature rendering with 3D geometry.
  365. /// </summary>
  366. [Test]
  367. public static void DrawNoTexturedGeometry()
  368. {
  369. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
  370. MaterialData materialData = new MaterialData
  371. {
  372. ShaderName = "LineShader3D"
  373. };
  374. Mesh mesh1 = Mesh.CreateCube("Mesh1", 3.0f, Color.Red, materialData);
  375. Mesh mesh2 = Mesh.CreateCube("Mesh2", 3.0f, Color.Green, materialData);
  376. Mesh mesh3 = Mesh.CreateCube("Mesh3", 3.0f, Color.Yellow, materialData);
  377. Mesh mesh4 = Mesh.CreateCube("Mesh4", 3.0f, Color.Blue, materialData);
  378. Mesh mesh5 = Mesh.CreateCube("Mesh5", 3.0f, Color.Teal, materialData);
  379. Matrix mesh2Matrix = Matrix.CreateTranslation(0, -5, 0);
  380. Matrix mesh3Matrix = Matrix.CreateTranslation(0, -10, 0);
  381. Matrix mesh4Matrix = Matrix.CreateTranslation(0, 5, 0);
  382. Matrix mesh5Matrix = Matrix.CreateTranslation(0, 10, 0);
  383. Application.Start(delegate
  384. {
  385. mesh1.Draw();
  386. mesh2.Draw(ref mesh2Matrix);
  387. mesh3.Draw(ref mesh3Matrix);
  388. mesh4.Draw(ref mesh4Matrix);
  389. mesh5.Draw(ref mesh5Matrix);
  390. });
  391. }
  392. #endregion
  393. #region CreateInnerBox
  394. /// <summary>
  395. /// Shows how to create inner box.
  396. /// </summary>
  397. [Test]
  398. public static void CreateInnerBox()
  399. {
  400. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
  401. MaterialData materialData = new MaterialData
  402. {
  403. DiffuseMapName = "DeltaEngineLogo",
  404. ShaderName = "TexturedShader3D"
  405. };
  406. MaterialData materialDataColored = new MaterialData
  407. {
  408. DiffuseMapName = "DeltaEngineLogo",
  409. ShaderName = "TexturedColoredShader3D"
  410. };
  411. Mesh mesh1 = Mesh.CreateInnerBox("Mesh1", 3.0f, 3.0f, 3.0f,
  412. materialData);
  413. Mesh mesh2 = Mesh.CreateInnerBox("Mesh2", 3.0f, 3.0f, 3.0f,
  414. Color.Green, materialDataColored);
  415. Matrix mesh2Matrix = Matrix.CreateTranslation(0, -5, 0);
  416. Application.Start(delegate
  417. {
  418. mesh1.Draw();
  419. mesh2.Draw(ref mesh2Matrix);
  420. });
  421. }
  422. #endregion
  423. #region CreateInnerSphere
  424. /// <summary>
  425. /// Shows how to create inner sphere.
  426. /// </summary>
  427. [Test]
  428. public static void CreateInnerSphere()
  429. {
  430. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
  431. MaterialData materialData = new MaterialData
  432. {
  433. DiffuseMapName = "DeltaEngineLogo",
  434. ShaderName = "TexturedShader3D"
  435. };
  436. MaterialData materialDataColored = new MaterialData
  437. {
  438. DiffuseMapName = "DeltaEngineLogo",
  439. ShaderName = "TexturedColoredShader3D"
  440. };
  441. Mesh mesh1 = Mesh.CreateInnerSphere("Mesh1", 3.0f,
  442. materialData);
  443. Mesh mesh2 = Mesh.CreateInnerSphere("Mesh2", 3.0f,
  444. Color.Green, materialDataColored);
  445. Matrix mesh2Matrix = Matrix.CreateTranslation(0, -10, 0);
  446. Application.Start(delegate
  447. {
  448. mesh1.Draw();
  449. mesh2.Draw(ref mesh2Matrix);
  450. });
  451. }
  452. #endregion
  453. }
  454. }