PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Rendering/ModelTests/MeshTests.cs

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