PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Rendering/ModelTests/Tutorials.cs

#
C# | 498 lines | 289 code | 36 blank | 173 comment | 6 complexity | 575f212d43fda818accafbcbd6c91f55 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.ContentSystem.Rendering;
  3. using Delta.Engine;
  4. using Delta.Graphics.Basics;
  5. using Delta.Rendering.Basics.Drawing;
  6. using Delta.Rendering.Cameras;
  7. using Delta.Rendering.Models;
  8. using Delta.Utilities.Datatypes;
  9. using Delta.Utilities.Graphics;
  10. using NUnit.Framework;
  11. namespace Delta.Rendering.ModelTests
  12. {
  13. [Category("Visual")]
  14. public class Tutorials
  15. {
  16. #region DrawGeneratedBox (Static)
  17. /// <summary>
  18. /// Tutorial: 3D Model Rendering Tutorial 1: Draw Generated Box
  19. /// Difficulty: Easy
  20. /// Description: Draw a 3d generated box.
  21. /// Image: http://DeltaEngine.net/Icons/ModelTutorial1.png
  22. /// </summary>
  23. [Test]
  24. public static void DrawGeneratedBox()
  25. {
  26. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  27. Mesh box = Mesh.CreateBox("Box", 5f, 5f, 5f, MaterialData.Default);
  28. Application.Start(delegate
  29. {
  30. box.Draw();
  31. });
  32. }
  33. #endregion
  34. #region DrawGeneratedSphere (Static)
  35. /// <summary>
  36. /// Tutorial: 3D Model Rendering Tutorial 2: Draw Generated Sphere
  37. /// Difficulty: Easy
  38. /// Description: Draw a 3d generated sphere.
  39. /// Image: http://DeltaEngine.net/Icons/ModelTutorial2.png
  40. /// </summary>
  41. [Test]
  42. public static void DrawGeneratedSphere()
  43. {
  44. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  45. Mesh sphere = Mesh.CreateSphere("Sphere", 5f, MaterialData.Default);
  46. Application.Start(delegate
  47. {
  48. sphere.Draw();
  49. });
  50. }
  51. #endregion
  52. #region DrawGeneratedPlane (Static)
  53. /// <summary>
  54. /// Tutorial: 3D Model Rendering Tutorial 3: Draw Generated Plane
  55. /// Difficulty: Easy
  56. /// Description: Draw a 3d generated plane.
  57. /// Image: http://DeltaEngine.net/Icons/ModelTutorial3.png
  58. /// </summary>
  59. [Test]
  60. public static void DrawGeneratedPlane()
  61. {
  62. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  63. Mesh plane = Mesh.CreatePlane("Plane", 15f, 15f, MaterialData.Default);
  64. Application.Start(delegate
  65. {
  66. plane.Draw();
  67. });
  68. }
  69. #endregion
  70. #region DrawCustomGeometry (Static)
  71. /// <summary>
  72. /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry.
  73. /// Difficulty: Medium
  74. /// Description: Draw a customly generated geometry out of vectors.
  75. /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
  76. /// </summary>
  77. [Test]
  78. public static void DrawCustomGeometry()
  79. {
  80. // Create a bunch of custom positions for the vertices
  81. List<Vector> positions = new List<Vector>();
  82. positions.Add(new Vector(0.1f, 0.1f, 0.0f));
  83. positions.Add(new Vector(0.7f, 0.2f, 0.0f));
  84. positions.Add(new Vector(0.8f, 0.9f, 0.0f));
  85. positions.Add(new Vector(0.2f, 0.8f, 0.0f));
  86. // For each vertex also store a texture uv.
  87. List<Point> uvs = new List<Point>();
  88. uvs.Add(new Point(0.0f, 0.0f));
  89. uvs.Add(new Point(1.0f, 0.0f));
  90. uvs.Add(new Point(1.0f, 1.0f));
  91. uvs.Add(new Point(0.0f, 1.0f));
  92. // Plus their indices for a simple quad
  93. List<ushort> indices = new List<ushort>();
  94. indices.Add(1);
  95. indices.Add(2);
  96. indices.Add(0);
  97. indices.Add(2);
  98. indices.Add(3);
  99. indices.Add(0);
  100. // Simply create a geometry from the vertices and indices and pass it to
  101. // a mesh to be rendered.
  102. Mesh customMesh = new Mesh(new MeshData(
  103. GeometryData.Create(positions, uvs, indices),
  104. MaterialData.Default, Vector.Zero));
  105. // Have a look at camera, which looks at the center from above
  106. LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
  107. // At runtime all we need to do is to draw ..
  108. Application.Start(delegate
  109. {
  110. Grid.Draw();
  111. customMesh.Draw();
  112. });
  113. }
  114. #endregion
  115. #region DrawCustomGeometryColored (Static)
  116. /// <summary>
  117. /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry with
  118. /// colored vertices.
  119. /// Difficulty: Medium
  120. /// Description: Draw a customly generated geometry out of vectors.
  121. /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
  122. /// </summary>
  123. [Test]
  124. public static void DrawCustomGeometryColored()
  125. {
  126. // Create a bunch of custom positions for the vertices
  127. List<Vector> positions = new List<Vector>();
  128. positions.Add(new Vector(0.1f, 0.1f, 0.0f));
  129. positions.Add(new Vector(0.7f, 0.2f, 0.0f));
  130. positions.Add(new Vector(0.8f, 0.9f, 0.0f));
  131. positions.Add(new Vector(0.2f, 0.8f, 0.0f));
  132. // For each vertex also store a color (we don't use texturing here)
  133. List<Color> colors = new List<Color>();
  134. colors.Add(Color.Red);
  135. colors.Add(Color.Red);
  136. colors.Add(Color.Red);
  137. colors.Add(Color.Red);
  138. // Plus their indices for a simple quad
  139. List<ushort> indices = new List<ushort>();
  140. indices.Add(1);
  141. indices.Add(2);
  142. indices.Add(0);
  143. indices.Add(2);
  144. indices.Add(3);
  145. indices.Add(0);
  146. // Also create a material to just use colors, no texturing
  147. MaterialData materialData = new MaterialData();
  148. // Note: If texture is used this is much easier as we can just create a
  149. // new material with the given texture name, rest will be created for us.
  150. materialData.DiffuseMapName = "";
  151. materialData.ShaderName = "LineShader3D";
  152. // Simply create a geometry from the vertices and indices and pass it to
  153. // a mesh to be rendered.
  154. Mesh customMesh = new Mesh(new MeshData(
  155. GeometryData.Create(positions, colors, indices),
  156. materialData, Vector.Zero));
  157. // Have a look at camera, which looks at the center from above
  158. LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
  159. // At runtime all we need to do is to draw ..
  160. Application.Start(delegate
  161. {
  162. Grid.Draw();
  163. customMesh.Draw();
  164. });
  165. }
  166. #endregion
  167. #region DrawCustomGeometryManually (Static)
  168. /// <summary>
  169. /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry manually
  170. /// Difficulty: Hard
  171. /// Description: Draw a customly generated geometry out of vectors by doing
  172. /// all the required steps one by one for maximum flexibility.
  173. /// See DrawCustomGeometry for an easier and less flexible way.
  174. /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
  175. /// </summary>
  176. [Test]
  177. public static void DrawCustomGeometryManually()
  178. {
  179. // Create a bunch of custom points
  180. List<Vector> positions = new List<Vector>();
  181. positions.Add(new Vector(0.1f, 0.1f, 0.0f));
  182. positions.Add(new Vector(0.7f, 0.2f, 0.0f));
  183. positions.Add(new Vector(0.8f, 0.9f, 0.0f));
  184. positions.Add(new Vector(0.2f, 0.8f, 0.0f));
  185. // Plus their indices for a simple quad
  186. List<ushort> indices = new List<ushort>();
  187. indices.Add(1);
  188. indices.Add(2);
  189. indices.Add(0);
  190. indices.Add(2);
  191. indices.Add(3);
  192. indices.Add(0);
  193. // Used color, each position could have its own color.
  194. Color usedColor = Color.Red;
  195. // Have a look at camera, which looks at the center from above
  196. LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
  197. // Next get the shader for the required vertex format. Because we don't
  198. // use a texture here, use LineShader3D for just 3D vertices with color.
  199. Shader shader = Shader.Create("LineShader3D");
  200. // Also create a material for the mesh below (makes rendering easier)
  201. MaterialData materialData = new MaterialData();
  202. // Note: If texture is used this is much easier as we can just create a
  203. // new material with the given texture name, rest will be created for us.
  204. materialData.DiffuseMapName = "";
  205. materialData.ShaderName = "LineShader3D";
  206. // And finally create the geometry out of the custom positions
  207. GeometryData geometry = new GeometryData(
  208. "<DrawCustomGeometry>", positions.Count, shader.VertexFormat,
  209. indices.Count, true, false);
  210. // Add all vertices from the positions
  211. for (int num = 0; num < positions.Count; num++)
  212. {
  213. // Note: Only position and color is used here, rest is ignored.
  214. geometry.SetVertexData(num, positions[num], Point.Zero, usedColor,
  215. Vector.UnitZ, Vector.UnitZ);
  216. }
  217. // Set the indices
  218. geometry.Indices = indices.ToArray();
  219. // And finally create the mesh out of all this
  220. // Note: You must change the MeshData constructor to public here!
  221. Mesh customMesh = new Mesh(
  222. new MeshData(geometry, materialData, Vector.Zero));
  223. // At runtime all we need to do is to draw ..
  224. Application.Start(delegate
  225. {
  226. Grid.Draw();
  227. customMesh.Draw();
  228. });
  229. }
  230. #endregion
  231. #region TestSimpleMinecraftClone (Static)
  232. /// <summary>
  233. /// Tutorial: 3D Model Rendering Tutorial 5: Test simple Minecraft clone
  234. /// Difficulty: Easy
  235. /// Description: Draw a 5x5x5 grid of 3D boxes with grass, mud and sand as
  236. /// a very simple prototype for a Minecraft-based 3D game.
  237. /// Image: http://DeltaEngine.net/Icons/ModelTutorial5.png
  238. /// </summary>
  239. [Test]
  240. public static void TestSimpleMinecraftClone()
  241. {
  242. var grass = Mesh.CreateBox("Grass", 1, 1, 1,
  243. new MaterialData()
  244. {
  245. DiffuseMapName = "GrassTile"
  246. });
  247. var mud = Mesh.CreateBox("Mud", 1, 1, 1,
  248. new MaterialData()
  249. {
  250. DiffuseMapName = "MudTile"
  251. });
  252. var sand = Mesh.CreateBox("Sand", 1, 1, 1,
  253. new MaterialData()
  254. {
  255. DiffuseMapName = "SandTile"
  256. });
  257. Mesh unused = null;
  258. // Note: Real games are obviously more complex than this ^^
  259. var boxes = new[, ,]
  260. {
  261. {
  262. {
  263. grass, grass, grass, grass, grass,
  264. },
  265. {
  266. grass, sand, sand, sand, mud,
  267. },
  268. {
  269. sand, sand, grass, sand, mud,
  270. },
  271. {
  272. sand, grass, grass, mud, mud,
  273. },
  274. {
  275. sand, sand, sand, mud, mud,
  276. },
  277. },
  278. {
  279. {
  280. grass, grass, grass, grass, grass,
  281. },
  282. {
  283. grass, sand, sand, sand, mud,
  284. },
  285. {
  286. sand, sand, grass, sand, mud,
  287. },
  288. {
  289. sand, grass, grass, mud, mud,
  290. },
  291. {
  292. sand, sand, sand, mud, mud,
  293. },
  294. },
  295. {
  296. {
  297. grass, grass, grass, grass, grass,
  298. },
  299. {
  300. grass, sand, sand, sand, mud,
  301. },
  302. {
  303. sand, sand, grass, sand, mud,
  304. },
  305. {
  306. sand, grass, grass, mud, mud,
  307. },
  308. {
  309. sand, sand, sand, mud, mud,
  310. },
  311. },
  312. {
  313. {
  314. grass, grass, grass, grass, grass,
  315. },
  316. {
  317. grass, unused, unused, unused, mud,
  318. },
  319. {
  320. unused, unused, grass, unused, mud,
  321. },
  322. {
  323. unused, grass, grass, mud, mud,
  324. },
  325. {
  326. unused, unused, unused, mud, mud,
  327. },
  328. },
  329. {
  330. {
  331. grass, grass, grass, grass, grass,
  332. },
  333. {
  334. grass, unused, unused, unused, unused,
  335. },
  336. {
  337. unused, unused, grass, unused, unused,
  338. },
  339. {
  340. unused, grass, grass, unused, unused,
  341. },
  342. {
  343. unused, unused, unused, unused, unused,
  344. },
  345. },
  346. };
  347. LookAtCamera cam = new LookAtCamera(new Vector(7, 7, 7));
  348. Application.Start(delegate
  349. {
  350. // Show a 5x5x5 box grid
  351. for (int z = 0; z < boxes.GetLength(0); z++)
  352. {
  353. for (int y = 0; y < boxes.GetLength(1); y++)
  354. {
  355. for (int x = 0; x < boxes.GetLength(2); x++)
  356. {
  357. if (boxes[z, y, x] == null)
  358. {
  359. continue;
  360. }
  361. Matrix renderMatrix = Matrix.CreateTranslation(x, y, z);
  362. boxes[z, y, x].Draw(ref renderMatrix);
  363. }
  364. }
  365. }
  366. });
  367. }
  368. #endregion
  369. /*TODO: we don't have uploaded mesh content for this yet!
  370. #region DrawStaticModel (Static)
  371. /// <summary>
  372. /// Tutorial: 3D Model Rendering Tutorial 4: Draw Static Model
  373. /// Difficulty: Easy
  374. /// Description: Draw a static model in 3d.
  375. /// Note: Content is currently missing!
  376. /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
  377. /// </summary>
  378. [Test]
  379. public static void DrawStaticModel()
  380. {
  381. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  382. //Note: Content is currently missing!
  383. Mesh angel = new Mesh("Angel");
  384. Application.Start(delegate
  385. {
  386. angel.Draw();
  387. });
  388. }
  389. #endregion
  390. */
  391. /*TODO: we don't have uploaded mesh content for this yet!
  392. #region DrawTransformedStaticModel (Static)
  393. /// <summary>
  394. /// Tutorial: 3D Model Rendering Tutorial 5: Draw Transformed Static Model
  395. /// Difficulty: Easy
  396. /// Description: Draw a static model in 3d with transformation.
  397. /// Note: Content is currently missing!
  398. /// Image: http://DeltaEngine.net/Icons/ModelTutorial5.png
  399. /// </summary>
  400. [Test]
  401. public static void DrawTransformedStaticModel()
  402. {
  403. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  404. Mesh angel = new Mesh("Angel");
  405. Matrix transformation = Matrix.CreateScale(1f, 2f, 0.5f) *
  406. Matrix.CreateTranslation(0f, 5f, 0f);
  407. Application.Start(delegate
  408. {
  409. angel.Draw(ref transformation);
  410. });
  411. }
  412. #endregion
  413. */
  414. /*TODO: we don't have uploaded mesh content for this yet!
  415. #region DrawAnimatedModel (Static)
  416. /// <summary>
  417. /// Tutorial: 3D Model Rendering Tutorial 6: Draw Animated Model
  418. /// Difficulty: Easy
  419. /// Description: Draw an animated model in 3d.
  420. /// Note: Content is currently missing!
  421. /// Image: http://DeltaEngine.net/Icons/ModelTutorial6.png
  422. /// </summary>
  423. [Test]
  424. public static void DrawAnimatedModel()
  425. {
  426. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  427. Mesh angel = new Mesh("AngelAnimated");
  428. Application.Start(delegate
  429. {
  430. angel.Draw();
  431. });
  432. }
  433. #endregion
  434. */
  435. /*TODO: we don't have uploaded mesh content for this yet!
  436. #region DrawTransformedAnimatedModel (Static)
  437. /// <summary>
  438. /// Tutorial: 3D Model Rendering Tutorial 7: Draw Transformed Animated Model
  439. /// Difficulty: Easy
  440. /// Description: Draw an animated model in 3d with transformation.
  441. /// Note: Content is currently missing!
  442. /// Image: http://DeltaEngine.net/Icons/ModelTutorial7.png
  443. /// </summary>
  444. [Test]
  445. public static void DrawTransformedAnimatedModel()
  446. {
  447. LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
  448. Mesh angel = new Mesh("AngelAnimated");
  449. Matrix transformation = Matrix.CreateScale(1f, 2f, 0.5f) *
  450. Matrix.CreateTranslation(0f, 5f, 0f);
  451. Application.Start(delegate
  452. {
  453. angel.Draw(ref transformation);
  454. });
  455. }
  456. #endregion
  457. */
  458. }
  459. }