PageRenderTime 56ms CodeModel.GetById 30ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Rendering/ModelTests/Tutorials.cs

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