/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
- using System;
- using Delta.ContentSystem.Rendering;
- using Delta.Engine;
- using Delta.InputSystem;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Cameras;
- using Delta.Rendering.Models;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Graphics;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.Rendering.ModelTests
- {
- internal class MeshTests
- {
- #region TestCreateBox
- /// <summary>
- /// Test the create box method of MeshData.
- /// </summary>
- [Test, Category("Visual")]
- public static void TestCreateBox()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- GeometryData data = GeometryData.CreateBox(
- new VertexFormat(new[]
- {
- new VertexElement(VertexElementType.Position3D),
- new VertexElement(VertexElementType.Normal),
- new VertexElement(VertexElementType.Tangent),
- }), 10, 10, 10, false, Color.White);
- Color faceColor = Color.Red;
- Color normalColor = Color.Yellow;
- Color tangentColor = Color.Purple;
- bool showTangent = false;
- float angle = 0f;
- Application.Start(delegate
- {
- angle += Time.Delta * 20f;
- cam.Position = new Vector(25f * MathHelper.Cos(angle),
- 20f * MathHelper.Sin(angle), cam.Position.Z);
-
- Grid.Draw();
-
- for (int i = 0; i < data.Indices.Length / 3; i++)
- {
- int startIndex = i * 3;
- Vector pos1 = data.GetPosition(
- data.Indices[startIndex]);
- Vector pos2 = data.GetPosition(
- data.Indices[startIndex + 1]);
- Vector pos3 = data.GetPosition(
- data.Indices[startIndex + 2]);
-
- Vector normal = new Vector(data.reader);
- Vector tangent = new Vector(data.reader);
-
- Line.Draw(pos1, pos2, faceColor);
- Line.Draw(pos2, pos3, faceColor);
- Line.Draw(pos3, pos1, faceColor);
-
- Vector center = (pos1 + pos2 + pos3) / 3f;
-
- if (showTangent)
- {
- Application.Window.Title = "Tangents";
- Line.Draw(center, center + tangent, tangentColor);
- }
- else
- {
- Application.Window.Title = "Normals";
- Line.Draw(center, center + normal, normalColor);
- }
- }
-
- if (Input.Mouse.LeftButtonReleased)
- {
- showTangent = !showTangent;
- }
- });
- }
- #endregion
-
- #region Start (EntryPoint from Main)
- /// <summary>
- /// Start
- /// </summary>
- [Test]
- public static void Start()
- {
- //DrawCreatePlane();
- //DrawCreateBox();
- //DrawCreateSphere();
- //DrawMesh();
- DrawAnimatedMesh();
- //SimpleAnimatedPlane();
-
- //ImportColladaTestBox();
- //DrawColladaTestBox();
- //DrawColladaBox2UVs();
- //DrawModel();
- //DrawMap();
- //DrawMayaModel();
- //Save();
- //TestModelWithTextureTiling();
- //DrawGoblin();
- //DrawAnimatedModel();
- //TestMultipleAnimations();
- //TestSmoothPlayAnimation();
- //TestFrameRate();
- }
-
- // Start()
- #endregion
-
- #region DrawCreatePlane
- /// <summary>
- /// Draw create plane
- /// </summary>
- [Test]
- public static void DrawCreatePlane()
- {
- // First we have define the material
- MaterialData materialData = new MaterialData
- {
- ShaderName = "TexturedShader3D",
- //unused: Ambient = Color.Yellow,
- //DiffuseMapName = "",
- //DiffuseMapName = "TestPlaneHighDiffuse",
- DiffuseMapName = "FountainHighDiffuse",
- };
- // And the model we wanna draw
- Mesh plane = Mesh.CreatePlane("ModelTests.DrawCreatePlane", 5, 5,
- materialData);
-
- // Initialize the camera which we wanna use in the scene
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
- Application.Start(delegate
- {
- plane.Draw();
- });
- }
-
- // DrawCreatePlane()
- #endregion
-
- #region DrawBox
- /// <summary>
- /// Draw create box
- /// </summary>
- [Test]
- public static void DrawBox()
- {
- Mesh box = Mesh.CreateBox("MeshTests.DrawBox", 5, 5, 5,
- MaterialData.Default);
-
- // Initialize the camera which we wanna use in the scene
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
- Application.Start(delegate
- {
- box.Draw();
- });
- }
-
- // DrawCreateBox()
- #endregion
-
- #region DrawColoredBox
- /// <summary>
- /// Draw colored box
- /// </summary>
- [Test]
- public static void DrawColoredBox()
- {
- // First we have define the material
- MaterialData materialData = new MaterialData
- {
- ShaderName = "TexturedColoredShader3D",
- Diffuse = Color.Yellow,
- DiffuseMapName = "",
- };
- // And the model we wanna draw
- Mesh box = Mesh.CreateBox("MeshTests.DrawColoredBox", 5, 5, 5,
- materialData);
-
- // Initialize the camera which we wanna use in the scene
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
- Application.Start(delegate
- {
- box.Draw();
- });
- }
-
- // DrawCreateBox()
- #endregion
-
- #region DrawCreateSphere
- /// <summary>
- /// Draw create sphere
- /// </summary>
- [Test]
- public static void DrawCreateSphere()
- {
- // First we have define the material
- MaterialData materialData = new MaterialData
- {
- ShaderName = "TexturedShader3D",
- Ambient = Color.Yellow,
- DiffuseMapName = "",
- };
- // And the model we wanna draw
- Mesh sphere = Mesh.CreateSphere("ModelTests.DrawCreateSphere", 0.1f,
- materialData);
-
- // Initialize the camera which we wanna use in the scene
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
-
- Application.Start(delegate
- {
- Grid.Draw();
-
- sphere.Draw();
- });
- }
-
- // DrawCreateSphere()
- #endregion
-
- #region DrawMesh
- /// <summary>
- /// Draw mesh
- /// </summary>
- [Test]
- public static void DrawMesh()
- {
- Mesh testMesh = new Mesh(
- //"Cock131"); // Strange name for Soulcraft level part test mesh!
- //"Twinchurch");
- //"TestPlane");
- //"Fountain");
- //"Statue");
- //"Colosseum");
- //"Bridge");
- //"BridgeFloor");
- //"BridgeEnd");
- //Note: Many models are messed up right now, but the level ones work!
- //"Bench");
- //"Column");
- //"LampB");
- //Note: This still has duplicate content bug!
- //"Skydome");
- //Note 2011-02-12: All three stopped working, but does not matter,
- // the animated ones work for the tech demo ...
- //"Abomination");
- //"Angel");
- //"Woman");
- "Firemage");
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
- // Test if we have loaded the normal correctly!
- //finally all works fine, last mipmaps were just missing (Marcels code)!
- //Material testMat = new Material(
- //now working, quadratic:
- //"AbominationHighNormal");
- //"AngelHighNormal");
- //now working, quadratic:
- //"WomanHighNormal");
- //not working, not quadratic:
- //"LampHighMediumLowNormal");
- //"InnerGroundBestHighMediumLowNormal");
- //"StatueHighMediumNormal");
- //not working, not quadratic:
- //"ObeliskHighMediumLowNormal");
-
- Application.Start(delegate
- {
- // Show the normal map in the top left corner.
- //testMat.Draw(new Rectangle(ScreenSpace.Area.Left, ScreenSpace.Area.Top,
- // 0.1f, 0.1f));
- //Font.Default.WriteTopLeft("Used shader: " + testMesh.Material);
- testMesh.Draw();
- //Grid.Draw();
- //Light.Draw();
- //disabled, not always available: Sky.Draw();
- });
- }
-
- // DrawMesh()
- #endregion
-
- #region DrawAnimatedMesh
- /// <summary>
- /// Draw animated mesh
- /// </summary>
- [Test]
- public static void DrawAnimatedMesh()
- {
- Mesh testMesh = new Mesh(
- //"Abomination");
- //"Angel");
- //"Woman");
- "Firemage");
- //"Demon");
- LookAtCamera cam = new LookAtCamera(new Vector(10, 10, 10));
- Application.Start(delegate
- {
- testMesh.Draw();
- Grid.Draw();
- //Light.Draw();
- //disabled, not always available: Sky.Draw();
- });
- }
-
- // DrawAnimatedMesh()
- #endregion
-
- #region SimpleAnimatedPlane
- /// <summary>
- /// Simple animated plane
- /// </summary>
- [Test]
- private static void SimpleAnimatedPlane()
- {
- MaterialData planeMaterial = new MaterialData
- {
- // Skinned shader
- ShaderName = "SkinnedTextured",
- // Default diffuse map
- DiffuseMapName = "",
- };
- // And the model we wanna draw
- Mesh planeMesh = Mesh.CreatePlane("<SimpleAnimatedPlane>", 2, 2,
- planeMaterial);
-
- GeometryData planeData = planeMesh.Geometry.Data;
-
- // Animate only the right plane side
- planeData.SetSkinData(0, new Point(0, 0), new Point(1.0f, 0.0f));
- planeData.SetSkinData(1, new Point(1.0f, 1.0f), new Point(0.5f, 0.5f));
- planeData.SetSkinData(2, new Point(0, 0), new Point(1.0f, 0.0f));
- planeData.SetSkinData(3, new Point(1.0f, 1.0f), new Point(0.5f, 0.5f));
-
- Animation testAnimation = new Animation(planeMesh, null) //TODO?
- {
- Duration = 1,
- };
-
- float frameCount = 30;
- float degreeIncrease = 90 / frameCount;
- float currentDegree = -45f;
- Matrix[][] frames = new Matrix[(int)frameCount][];
- for (int frame = 0; frame < frameCount; frame++)
- {
- // we want to inverse the direction of the animation after the half
- // of the frames so we get a cycle animation.
- if (frame == frameCount / 2)
- {
- degreeIncrease *= -1;
- }
-
- currentDegree += degreeIncrease;
- frames[frame] = new[]
- {
- // The left plane side has no animation part.
- Matrix.Identity,
- // Rotate the right plane side up and down.
- Matrix.CreateRotationY(currentDegree),
- };
- }
-
- testAnimation.Frames = frames;
-
- // Let's view the scene now
- LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 5));
- Application.Start(delegate
- {
- Grid.Draw();
- testAnimation.Update();
- planeMesh.Draw();
- });
- }
-
- // SimpleAnimatedPlane()
- #endregion
-
- #region LoadContentMesh
- /// <summary>
- /// Load content mesh
- /// </summary>
- [Test]
- public static void LoadContentMesh()
- {
- string testMeshName = "Rocks";
- Mesh meshInstance1 = new Mesh(testMeshName);
- Assert.NotNull(meshInstance1.Geometry);
- Assert.NotNull(meshInstance1.Material);
-
- Mesh meshInstance2 = new Mesh(testMeshName);
- Assert.NotNull(meshInstance2.Geometry);
- Assert.NotNull(meshInstance2.Material);
- }
- #endregion
-
- #region DrawNoTexturedGeometry
- /// <summary>
- /// This test show the NoTexturing feature rendering with 3D geometry.
- /// </summary>
- [Test]
- public static void DrawNoTexturedGeometry()
- {
- BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
-
- MaterialData materialData = new MaterialData
- {
- ShaderName = "LineShader3D"
- };
-
- Mesh mesh1 = Mesh.CreateCube("Mesh1", 3.0f, Color.Red, materialData);
- Mesh mesh2 = Mesh.CreateCube("Mesh2", 3.0f, Color.Green, materialData);
- Mesh mesh3 = Mesh.CreateCube("Mesh3", 3.0f, Color.Yellow, materialData);
- Mesh mesh4 = Mesh.CreateCube("Mesh4", 3.0f, Color.Blue, materialData);
- Mesh mesh5 = Mesh.CreateCube("Mesh5", 3.0f, Color.Teal, materialData);
-
- Matrix mesh2Matrix = Matrix.CreateTranslation(0, -5, 0);
- Matrix mesh3Matrix = Matrix.CreateTranslation(0, -10, 0);
- Matrix mesh4Matrix = Matrix.CreateTranslation(0, 5, 0);
- Matrix mesh5Matrix = Matrix.CreateTranslation(0, 10, 0);
-
- Application.Start(delegate
- {
- mesh1.Draw();
- mesh2.Draw(ref mesh2Matrix);
- mesh3.Draw(ref mesh3Matrix);
- mesh4.Draw(ref mesh4Matrix);
- mesh5.Draw(ref mesh5Matrix);
- });
- }
- #endregion
-
- #region CreateInnerBox
- /// <summary>
- /// Shows how to create inner box.
- /// </summary>
- [Test]
- public static void CreateInnerBox()
- {
- BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
-
- MaterialData materialData = new MaterialData
- {
- DiffuseMapName = "DeltaEngineLogo",
- ShaderName = "TexturedShader3D"
- };
-
- MaterialData materialDataColored = new MaterialData
- {
- DiffuseMapName = "DeltaEngineLogo",
- ShaderName = "TexturedColoredShader3D"
- };
-
- Mesh mesh1 = Mesh.CreateInnerBox("Mesh1", 3.0f, 3.0f, 3.0f,
- materialData);
-
- Mesh mesh2 = Mesh.CreateInnerBox("Mesh2", 3.0f, 3.0f, 3.0f,
- Color.Green, materialDataColored);
-
- Matrix mesh2Matrix = Matrix.CreateTranslation(0, -5, 0);
-
- Application.Start(delegate
- {
- mesh1.Draw();
- mesh2.Draw(ref mesh2Matrix);
- });
- }
- #endregion
-
- #region CreateInnerSphere
- /// <summary>
- /// Shows how to create inner sphere.
- /// </summary>
- [Test]
- public static void CreateInnerSphere()
- {
- BaseCamera cam = new LookAtCamera(new Vector(10, 10, 15));
-
- MaterialData materialData = new MaterialData
- {
- DiffuseMapName = "DeltaEngineLogo",
- ShaderName = "TexturedShader3D"
- };
-
- MaterialData materialDataColored = new MaterialData
- {
- DiffuseMapName = "DeltaEngineLogo",
- ShaderName = "TexturedColoredShader3D"
- };
-
- Mesh mesh1 = Mesh.CreateInnerSphere("Mesh1", 3.0f,
- materialData);
-
- Mesh mesh2 = Mesh.CreateInnerSphere("Mesh2", 3.0f,
- Color.Green, materialDataColored);
-
- Matrix mesh2Matrix = Matrix.CreateTranslation(0, -10, 0);
-
- Application.Start(delegate
- {
- mesh1.Draw();
- mesh2.Draw(ref mesh2Matrix);
- });
- }
- #endregion
-
- }
- }