/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
- using System.Collections.Generic;
- using Delta.ContentSystem.Rendering;
- using Delta.Engine;
- using Delta.Graphics.Basics;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Cameras;
- using Delta.Rendering.Models;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Graphics;
- using NUnit.Framework;
-
- namespace Delta.Rendering.ModelTests
- {
- [Category("Visual")]
- public class Tutorials
- {
- #region DrawGeneratedBox (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 1: Draw Generated Box
- /// Difficulty: Easy
- /// Description: Draw a 3d generated box.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial1.png
- /// </summary>
- [Test]
- public static void DrawGeneratedBox()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh box = Mesh.CreateBox("Box", 5f, 5f, 5f, MaterialData.Default);
-
- Application.Start(delegate
- {
- box.Draw();
- });
- }
- #endregion
-
- #region DrawGeneratedSphere (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 2: Draw Generated Sphere
- /// Difficulty: Easy
- /// Description: Draw a 3d generated sphere.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial2.png
- /// </summary>
- [Test]
- public static void DrawGeneratedSphere()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh sphere = Mesh.CreateSphere("Sphere", 5f, MaterialData.Default);
-
- Application.Start(delegate
- {
- sphere.Draw();
- });
- }
- #endregion
-
- #region DrawGeneratedPlane (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 3: Draw Generated Plane
- /// Difficulty: Easy
- /// Description: Draw a 3d generated plane.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial3.png
- /// </summary>
- [Test]
- public static void DrawGeneratedPlane()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh plane = Mesh.CreatePlane("Plane", 15f, 15f, MaterialData.Default);
-
- Application.Start(delegate
- {
- plane.Draw();
- });
- }
- #endregion
-
- #region DrawCustomGeometry (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry.
- /// Difficulty: Medium
- /// Description: Draw a customly generated geometry out of vectors.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
- /// </summary>
- [Test]
- public static void DrawCustomGeometry()
- {
- // Create a bunch of custom positions for the vertices
- List<Vector> positions = new List<Vector>();
- positions.Add(new Vector(0.1f, 0.1f, 0.0f));
- positions.Add(new Vector(0.7f, 0.2f, 0.0f));
- positions.Add(new Vector(0.8f, 0.9f, 0.0f));
- positions.Add(new Vector(0.2f, 0.8f, 0.0f));
-
- // For each vertex also store a texture uv.
- List<Point> uvs = new List<Point>();
- uvs.Add(new Point(0.0f, 0.0f));
- uvs.Add(new Point(1.0f, 0.0f));
- uvs.Add(new Point(1.0f, 1.0f));
- uvs.Add(new Point(0.0f, 1.0f));
-
- // Plus their indices for a simple quad
- List<ushort> indices = new List<ushort>();
- indices.Add(1);
- indices.Add(2);
- indices.Add(0);
- indices.Add(2);
- indices.Add(3);
- indices.Add(0);
-
- // Simply create a geometry from the vertices and indices and pass it to
- // a mesh to be rendered.
- Mesh customMesh = new Mesh(new MeshData(
- GeometryData.Create(positions, uvs, indices),
- MaterialData.Default, Vector.Zero));
-
- // Have a look at camera, which looks at the center from above
- LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
-
- // At runtime all we need to do is to draw ..
- Application.Start(delegate
- {
- Grid.Draw();
- customMesh.Draw();
- });
- }
- #endregion
-
- #region DrawCustomGeometryColored (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry with
- /// colored vertices.
- /// Difficulty: Medium
- /// Description: Draw a customly generated geometry out of vectors.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
- /// </summary>
- [Test]
- public static void DrawCustomGeometryColored()
- {
- // Create a bunch of custom positions for the vertices
- List<Vector> positions = new List<Vector>();
- positions.Add(new Vector(0.1f, 0.1f, 0.0f));
- positions.Add(new Vector(0.7f, 0.2f, 0.0f));
- positions.Add(new Vector(0.8f, 0.9f, 0.0f));
- positions.Add(new Vector(0.2f, 0.8f, 0.0f));
-
- // For each vertex also store a color (we don't use texturing here)
- List<Color> colors = new List<Color>();
- colors.Add(Color.Red);
- colors.Add(Color.Red);
- colors.Add(Color.Red);
- colors.Add(Color.Red);
-
- // Plus their indices for a simple quad
- List<ushort> indices = new List<ushort>();
- indices.Add(1);
- indices.Add(2);
- indices.Add(0);
- indices.Add(2);
- indices.Add(3);
- indices.Add(0);
-
- // Also create a material to just use colors, no texturing
- MaterialData materialData = new MaterialData();
- // Note: If texture is used this is much easier as we can just create a
- // new material with the given texture name, rest will be created for us.
- materialData.DiffuseMapName = "";
- materialData.ShaderName = "LineShader3D";
-
- // Simply create a geometry from the vertices and indices and pass it to
- // a mesh to be rendered.
- Mesh customMesh = new Mesh(new MeshData(
- GeometryData.Create(positions, colors, indices),
- materialData, Vector.Zero));
-
- // Have a look at camera, which looks at the center from above
- LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
-
- // At runtime all we need to do is to draw ..
- Application.Start(delegate
- {
- Grid.Draw();
- customMesh.Draw();
- });
- }
- #endregion
-
- #region DrawCustomGeometryManually (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 4: Draw Custom Geometry manually
- /// Difficulty: Hard
- /// Description: Draw a customly generated geometry out of vectors by doing
- /// all the required steps one by one for maximum flexibility.
- /// See DrawCustomGeometry for an easier and less flexible way.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
- /// </summary>
- [Test]
- public static void DrawCustomGeometryManually()
- {
- // Create a bunch of custom points
- List<Vector> positions = new List<Vector>();
- positions.Add(new Vector(0.1f, 0.1f, 0.0f));
- positions.Add(new Vector(0.7f, 0.2f, 0.0f));
- positions.Add(new Vector(0.8f, 0.9f, 0.0f));
- positions.Add(new Vector(0.2f, 0.8f, 0.0f));
-
- // Plus their indices for a simple quad
- List<ushort> indices = new List<ushort>();
- indices.Add(1);
- indices.Add(2);
- indices.Add(0);
- indices.Add(2);
- indices.Add(3);
- indices.Add(0);
-
- // Used color, each position could have its own color.
- Color usedColor = Color.Red;
-
- // Have a look at camera, which looks at the center from above
- LookAtCamera cam = new LookAtCamera(new Vector(2, 1, 1));
-
- // Next get the shader for the required vertex format. Because we don't
- // use a texture here, use LineShader3D for just 3D vertices with color.
- Shader shader = Shader.Create("LineShader3D");
-
- // Also create a material for the mesh below (makes rendering easier)
- MaterialData materialData = new MaterialData();
- // Note: If texture is used this is much easier as we can just create a
- // new material with the given texture name, rest will be created for us.
- materialData.DiffuseMapName = "";
- materialData.ShaderName = "LineShader3D";
-
- // And finally create the geometry out of the custom positions
- GeometryData geometry = new GeometryData(
- "<DrawCustomGeometry>", positions.Count, shader.VertexFormat,
- indices.Count, true, false);
-
- // Add all vertices from the positions
- for (int num = 0; num < positions.Count; num++)
- {
- // Note: Only position and color is used here, rest is ignored.
- geometry.SetVertexData(num, positions[num], Point.Zero, usedColor,
- Vector.UnitZ, Vector.UnitZ);
- }
- // Set the indices
- geometry.Indices = indices.ToArray();
-
- // And finally create the mesh out of all this
- // Note: You must change the MeshData constructor to public here!
- Mesh customMesh = new Mesh(
- new MeshData(geometry, materialData, Vector.Zero));
-
- // At runtime all we need to do is to draw ..
- Application.Start(delegate
- {
- Grid.Draw();
- customMesh.Draw();
- });
- }
- #endregion
-
- #region TestSimpleMinecraftClone (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 5: Test simple Minecraft clone
- /// Difficulty: Easy
- /// Description: Draw a 5x5x5 grid of 3D boxes with grass, mud and sand as
- /// a very simple prototype for a Minecraft-based 3D game.
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial5.png
- /// </summary>
- [Test]
- public static void TestSimpleMinecraftClone()
- {
- var grass = Mesh.CreateBox("Grass", 1, 1, 1,
- new MaterialData()
- {
- DiffuseMapName = "GrassTile"
- });
- var mud = Mesh.CreateBox("Mud", 1, 1, 1,
- new MaterialData()
- {
- DiffuseMapName = "MudTile"
- });
- var sand = Mesh.CreateBox("Sand", 1, 1, 1,
- new MaterialData()
- {
- DiffuseMapName = "SandTile"
- });
- Mesh unused = null;
-
- // Note: Real games are obviously more complex than this ^^
- var boxes = new[, ,]
- {
- {
- {
- grass, grass, grass, grass, grass,
- },
- {
- grass, sand, sand, sand, mud,
- },
- {
- sand, sand, grass, sand, mud,
- },
- {
- sand, grass, grass, mud, mud,
- },
- {
- sand, sand, sand, mud, mud,
- },
- },
- {
- {
- grass, grass, grass, grass, grass,
- },
- {
- grass, sand, sand, sand, mud,
- },
- {
- sand, sand, grass, sand, mud,
- },
- {
- sand, grass, grass, mud, mud,
- },
- {
- sand, sand, sand, mud, mud,
- },
- },
- {
- {
- grass, grass, grass, grass, grass,
- },
- {
- grass, sand, sand, sand, mud,
- },
- {
- sand, sand, grass, sand, mud,
- },
- {
- sand, grass, grass, mud, mud,
- },
- {
- sand, sand, sand, mud, mud,
- },
- },
- {
- {
- grass, grass, grass, grass, grass,
- },
- {
- grass, unused, unused, unused, mud,
- },
- {
- unused, unused, grass, unused, mud,
- },
- {
- unused, grass, grass, mud, mud,
- },
- {
- unused, unused, unused, mud, mud,
- },
- },
- {
- {
- grass, grass, grass, grass, grass,
- },
- {
- grass, unused, unused, unused, unused,
- },
- {
- unused, unused, grass, unused, unused,
- },
- {
- unused, grass, grass, unused, unused,
- },
- {
- unused, unused, unused, unused, unused,
- },
- },
- };
-
- LookAtCamera cam = new LookAtCamera(new Vector(7, 7, 7));
- Application.Start(delegate
- {
- // Show a 5x5x5 box grid
- for (int z = 0; z < boxes.GetLength(0); z++)
- {
- for (int y = 0; y < boxes.GetLength(1); y++)
- {
- for (int x = 0; x < boxes.GetLength(2); x++)
- {
- if (boxes[z, y, x] == null)
- {
- continue;
- }
- Matrix renderMatrix = Matrix.CreateTranslation(x, y, z);
- boxes[z, y, x].Draw(ref renderMatrix);
- }
- }
- }
- });
- }
- #endregion
-
- /*TODO: we don't have uploaded mesh content for this yet!
- #region DrawStaticModel (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 4: Draw Static Model
- /// Difficulty: Easy
- /// Description: Draw a static model in 3d.
- /// Note: Content is currently missing!
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial4.png
- /// </summary>
- [Test]
- public static void DrawStaticModel()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- //Note: Content is currently missing!
- Mesh angel = new Mesh("Angel");
-
- Application.Start(delegate
- {
- angel.Draw();
- });
- }
- #endregion
- */
-
- /*TODO: we don't have uploaded mesh content for this yet!
- #region DrawTransformedStaticModel (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 5: Draw Transformed Static Model
- /// Difficulty: Easy
- /// Description: Draw a static model in 3d with transformation.
- /// Note: Content is currently missing!
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial5.png
- /// </summary>
- [Test]
- public static void DrawTransformedStaticModel()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh angel = new Mesh("Angel");
- Matrix transformation = Matrix.CreateScale(1f, 2f, 0.5f) *
- Matrix.CreateTranslation(0f, 5f, 0f);
-
- Application.Start(delegate
- {
- angel.Draw(ref transformation);
- });
- }
- #endregion
- */
-
- /*TODO: we don't have uploaded mesh content for this yet!
- #region DrawAnimatedModel (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 6: Draw Animated Model
- /// Difficulty: Easy
- /// Description: Draw an animated model in 3d.
- /// Note: Content is currently missing!
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial6.png
- /// </summary>
- [Test]
- public static void DrawAnimatedModel()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh angel = new Mesh("AngelAnimated");
- Application.Start(delegate
- {
- angel.Draw();
- });
- }
- #endregion
- */
-
- /*TODO: we don't have uploaded mesh content for this yet!
- #region DrawTransformedAnimatedModel (Static)
- /// <summary>
- /// Tutorial: 3D Model Rendering Tutorial 7: Draw Transformed Animated Model
- /// Difficulty: Easy
- /// Description: Draw an animated model in 3d with transformation.
- /// Note: Content is currently missing!
- /// Image: http://DeltaEngine.net/Icons/ModelTutorial7.png
- /// </summary>
- [Test]
- public static void DrawTransformedAnimatedModel()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(22, 15, 15));
- Mesh angel = new Mesh("AngelAnimated");
- Matrix transformation = Matrix.CreateScale(1f, 2f, 0.5f) *
- Matrix.CreateTranslation(0f, 5f, 0f);
-
- Application.Start(delegate
- {
- angel.Draw(ref transformation);
- });
- }
- #endregion
- */
- }
- }