/Rendering/ModelTests/LevelTests.cs
C# | 251 lines | 130 code | 21 blank | 100 comment | 4 complexity | 508c0853ea2aa8d3bc7690c689780311 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.Datatypes;
- using NUnit.Framework;
-
- namespace Delta.Rendering.ModelTests
- {
- /// <summary>
- /// Level tests
- /// </summary>
- [Category("Visual")]
- internal class LevelTests
- {
- #region Start (Static)
- /// <summary>
- /// Start
- /// </summary>
- [Test]
- public static void Start()
- {
- //SimpleLevelTest();
- //GeneratedLevel();
- //CheckVsFrustum();
- //AdvancedFrustumTest();
- }
- #endregion
-
-
- ///// <summary>
- ///// Simple level test
- ///// </summary>
- //public static void SimpleLevelTest()
- //{
- // Level level = new Level(
- // //"01ImperialPalace_Tokyo", "");
- // "Jungle", "");
- // //LookAtCamera cam = new LookAtCamera(new Vector(50, 370, 20),
- // // new Vector(50, 370, 0));
- // LookAtCamera cam = new LookAtCamera(new Vector(5, -25, 15));
- // Application.BackgroundColor = Color.Black;
- // Application.Start(delegate
- // {
- // level.Draw();
- // Grid.Draw();
- // });
- //}
-
-
- ///// <summary>
- ///// Generated level
- ///// </summary>
- //private static void GeneratedLevel()
- //{
- // LevelMeshInfo[] meshInfos = new LevelMeshInfo[]
- // {
- // new LevelMeshInfo
- // {
- // Name = "StoneFloorB",
- // WorldTransformations = new Matrix[]
- // {
- // Matrix.CreateTranslation(new Vector(0, 0, 0)),
- // },
- // },
- // new LevelMeshInfo
- // {
- // Name = "Twinchurch",
- // WorldTransformations = new Matrix[]
- // {
- // Matrix.CreateTranslation(new Vector(-10, 10, 0)),
- // Matrix.CreateTranslation(new Vector(10, 10, 0)),
- // },
- // },
- // };
-
- // LookAtCamera cam = new LookAtCamera(new Vector(0, -15, 15));
- // Level level = new Level(new LevelData("<GeneratedLevel>", meshInfos));
-
- // Application.Start(delegate
- // {
- // Grid.Draw();
-
- // level.Draw();
- // });
-
- //}
-
-
- #region AdvancedFrustumTest (Static)
- /// <summary>
- /// Check vs frustum
- /// </summary>
- [Test]
- public static void AdvancedFrustumTest()
- {
- // Create cameras
- BaseCamera freeCamera = new FreeCamera(new Vector(0, 0, 1));
- BaseCamera frustumCamera = new LookAtCamera(new Vector(0, 0, 0),
- new Vector(0, 1, 0));
- freeCamera.Activate();
-
- // Create a simple material.
- MaterialData materialData = new MaterialData
- {
- ShaderName = "TexturedShader3D",
- DiffuseMapName = "",
- };
- // Simple sphere mesh used for the testing of the frustum.
- Mesh sphereMesh = Mesh.CreateSphere("sphere", 0.3f, materialData);
-
- // Variables for rotating the second camera.
- Vector target = Vector.Zero;
- float angle = 0.0f;
- Application.Start(delegate
- {
- // Press space too rotate the frustumCamera around.
- if (Input.Keyboard.SpaceIsPressed)
- {
- angle += Time.Delta * 2.0f;
- target.X = (float)Math.Cos(angle);
- target.Y = (float)Math.Sin(angle);
- frustumCamera.Target = target;
- }
- // Set the current camera to be frustumCamera, place it nearly at Zero,
- // then do a run on frustumCamera so the viewMatrix is correct.
- frustumCamera.Activate();
- frustumCamera.Position = new Vector(0.0f, 0.0f, 0.0f);
- frustumCamera.Run();
- // Get the ViewFrustum of the frustumCamera.
- Plane[] frustum = frustumCamera.ViewFrustum;
-
- // Center point on the farPlane.
- Vector farPlanePos = frustumCamera.Position +
- frustumCamera.LookDirection * frustumCamera.FarPlane;
-
- // Center point on the nearPlane.
- Vector nearPlanePos = frustumCamera.Position +
- frustumCamera.LookDirection * frustumCamera.NearPlane;
-
- // The height of the planes from the center.
- float farPlaneHeight = 2.0f *
- (float)Math.Tan(frustumCamera.FieldOfView / 2) *
- frustumCamera.FarPlane;
- float nearPlaneHeight = 2.0f *
- (float)Math.Tan(frustumCamera.FieldOfView / 2) *
- frustumCamera.NearPlane;
- // The width of the planes from the center.
- float farPlaneWidth = farPlaneHeight * ScreenSpace.AspectRatio;
- float nearPlaneWidth = nearPlaneHeight * ScreenSpace.AspectRatio;
-
- // Get the up and the right vectors from the viewMatrix.
- Vector cameraUp = new Vector(frustumCamera.ViewMatrix.M11,
- frustumCamera.ViewMatrix.M21, frustumCamera.ViewMatrix.M31);
- Vector cameraRight = new Vector(frustumCamera.ViewMatrix.M12,
- frustumCamera.ViewMatrix.M22, frustumCamera.ViewMatrix.M32);
-
- // Get all the corners of the frustum planes.
- // Far plane upper left corner
- Vector farUpLeft = farPlanePos +
- (cameraRight * farPlaneHeight / 2) -
- (cameraUp * farPlaneWidth / 2);
- // Far plane upper right corner
- Vector farUpRight = farPlanePos +
- (cameraRight * farPlaneHeight / 2) +
- (cameraUp * farPlaneWidth / 2);
- // Far plane lower right corner
- Vector farDownRight = farPlanePos -
- (cameraRight * farPlaneHeight / 2) +
- (cameraUp * farPlaneWidth / 2);
- // Far plane lower left corner
- Vector farDownLeft = farPlanePos -
- (cameraRight * farPlaneHeight / 2) -
- (cameraUp * farPlaneWidth / 2);
- // Near plane upper left corner
- Vector nearUpLeft = nearPlanePos +
- (cameraRight * nearPlaneHeight / 2) -
- (cameraUp * nearPlaneWidth / 2);
- // Near plane upper right corner
- Vector nearUpRight = nearPlanePos +
- (cameraRight * nearPlaneHeight / 2) +
- (cameraUp * nearPlaneWidth / 2);
- // Near plane lower right corner
- Vector nearDownRight = nearPlanePos -
- (cameraRight * nearPlaneHeight / 2) +
- (cameraUp * nearPlaneWidth / 2);
- // Near plane lower left corner
- Vector nearDownLeft = nearPlanePos -
- (cameraRight * nearPlaneHeight / 2) -
- (cameraUp * nearPlaneWidth / 2);
-
- // set current camera back to freeCamera and then do a run again too
- // calculate viewMatrix and projection.
- freeCamera.Activate();
- freeCamera.Run();
-
- // Draw lines representing the frustum.
- // Near plane
- Line.Draw(nearUpLeft, nearUpRight, Color.White);
- Line.Draw(nearUpRight, nearDownRight, Color.White);
- Line.Draw(nearDownRight, nearDownLeft, Color.White);
- Line.Draw(nearDownLeft, nearUpLeft, Color.White);
- // Far plane
- Line.Draw(farUpLeft, farUpRight, Color.White);
- Line.Draw(farUpRight, farDownRight, Color.White);
- Line.Draw(farDownRight, farDownLeft, Color.White);
- Line.Draw(farDownLeft, farUpLeft, Color.White);
- // Right, left, up, down planes.
- Line.Draw(farUpLeft, nearUpLeft, Color.White);
- Line.Draw(farUpRight, nearUpRight, Color.White);
- Line.Draw(farDownRight, nearDownRight, Color.White);
- Line.Draw(farDownLeft, nearDownLeft, Color.White);
-
- // place boxes all around the place with 2 units in between and then
- // test too see if they are inside the frustum.
- int startX = 25;
- int startY = 25;
- Vector Pos = new Vector(startX, startY, 0.0f);
- for (int i = 0; i < startX; i++)
- {
- Pos.Y = startY;
- for (int j = 0; j < startY; j++)
- {
- Vector boxWorldMax = sphereMesh.Geometry.Data.BoundingBox.Max + Pos;
- Vector boxWorldMin = sphereMesh.Geometry.Data.BoundingBox.Min + Pos;
- // if the sphere bounding box is visible then show a green box.
- if (Level.IsInsideViewFrustum(sphereMesh, frustumCamera, Pos))
- {
- // Draw a green box when it is visible.
- Box.DrawOutline(boxWorldMin, boxWorldMax, Color.Green);
- } // if
- // if the box is not visible then show a red box.
- else
- {
- // Draw a red box when it is NOT visible.
- Box.DrawOutline(boxWorldMin, boxWorldMax, Color.Red);
- } // else
- // Moving the next sphere Y.
- Pos.Y -= 2.0f;
- } // for
- // Moving the next sphere X.
- Pos.X -= 2.0f;
- }
- });
- }
- #endregion
- }
- }