Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3.  
  4. namespace DoubleDebug
  5. {
  6. #if WINDOWS || LINUX
  7.     /// <summary>
  8.     /// The main class.
  9.     /// </summary>
  10.     public static class Program
  11.     {
  12.         private const float EPSILON = 0.001f;
  13.  
  14.         /// <summary>
  15.         /// The main entry point for the application.
  16.         /// </summary>
  17.         [STAThread]
  18.         static void Main()
  19.         {
  20.             var view = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
  21.             var proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 1.0f, 0.0001f, 100.0f);
  22.             var viewProj = view * proj;
  23.  
  24.  
  25.             WriteDelimeter("Setup");
  26.             Console.WriteLine($"View: position: {Vector3.Zero}, forward: {Vector3.Forward}, up: {Vector3.Up}");
  27.             Console.WriteLine($"Projection: near 0.0001f, far: 100.0f, fov: 90 degrees");
  28.  
  29.             WriteDelimeter("Near and Far plane");
  30.  
  31.             var point = new Vector3(0, 0, -1);
  32.             var transformed = Transform(point, viewProj);
  33.             Compare($"Point {point}, in front of the camera is transformed to the center of the far plane of the view volume", transformed, new Vector3(0, 0, 1));
  34.  
  35.             point = new Vector3(0, 0, -99);
  36.             transformed = Transform(point, viewProj);
  37.             Compare($"Point {point}, far away of the camera is transformed to the center of the far plane of the view volume", transformed, new Vector3(0, 0, 1));
  38.  
  39.             point = new Vector3(0, 0, -101);
  40.             transformed = Transform(point, viewProj);
  41.             Compare($"Point {point}, further away than the camera's far plane is transformed to the center of far plane of the view volume", transformed, new Vector3(0, 0, 1));
  42.  
  43.             point = new Vector3(0, 0, 1);
  44.             transformed = Transform(point, viewProj);
  45.             Compare($"Point {point}, behind the camera is transformed to the center of far plane of the view volume", transformed, new Vector3(0, 0, 1));
  46.  
  47.             point = new Vector3(0, 0, 99);
  48.             transformed = Transform(point, viewProj);
  49.             Compare($"Point {point}, far behind the camera is transformed to the center of far plane of the view volume", transformed, new Vector3(0, 0, 1));
  50.  
  51.  
  52.             WriteDelimeter("Uncentered");
  53.             point = new Vector3(-1, 0, -2);
  54.             transformed = Transform(point, viewProj);
  55.             Compare($"Point {point}, to the front left of the camera is transformed to the left of the far plane of the view volume", transformed, new Vector3(-0.5f, 0, 1));
  56.  
  57.             point = new Vector3(-1, 0, 2);
  58.             transformed = Transform(point, viewProj);
  59.             Compare($"Point {point}, to the left and behind of the camera is transformed to the right of the far plane of the view volume", transformed, new Vector3(0.5f, 0, 1));
  60.  
  61.  
  62.             WriteDelimeter("Corners");
  63.             var frustum = new BoundingFrustum(viewProj);
  64.             var corners = frustum.GetCorners();
  65.  
  66.             for (var i = 0; i < corners.Length; i++)
  67.             {
  68.                 var corner = corners[i];
  69.                 transformed = Transform(corner, viewProj);
  70.                 Inside($"Corner {corner} of the frustum is inside the view volume when transformed", transformed, -Vector3.One, Vector3.One);
  71.             }
  72.            
  73.             Console.Read();
  74.         }
  75.  
  76.  
  77.         private static Vector3 Transform(Vector3 point, Matrix matrix)
  78.         {
  79.             var t = Vector4.Transform(new Vector4(point, 1), matrix);
  80.             t /= t.W;
  81.  
  82.             return new Vector3(t.X, t.Y, t.Z);
  83.         }
  84.  
  85.         private static void WriteDelimeter(string title)
  86.         {
  87.             Console.ForegroundColor = ConsoleColor.DarkGray;
  88.             Console.WriteLine(new string('*', 20) + $" {title} " + new string('*', 20));
  89.             Console.ResetColor();
  90.         }
  91.  
  92.  
  93.         private static void Compare(string explanation, Vector3 a, Vector3 b)
  94.         {
  95.             var equal = Compare(a.X, b.X);
  96.             equal &= Compare(a.Y, b.Y);
  97.             equal &= Compare(a.Z, b.Z);
  98.  
  99.             Console.Out.WriteLine(explanation);            
  100.             if (!equal)
  101.             {
  102.                 Fail($"\t{a} is not equal to {b}");
  103.             }
  104.  
  105.             if (equal)
  106.             {
  107.                 Success($"\t{a} is equal to {b}");
  108.             }
  109.  
  110.             Console.ResetColor();            
  111.         }
  112.  
  113.         private static void Inside(string explanation, Vector3 point, Vector3 min, Vector3 max)
  114.         {
  115.             Console.Out.WriteLine(explanation);
  116.  
  117.             var box = new BoundingBox(min, max);            
  118.             if (box.Contains(point) != ContainmentType.Disjoint)
  119.             {                
  120.                 Success($"\t{point} is inside {min}, {max}");
  121.             }
  122.             else
  123.             {
  124.                 Fail($"\t{point} is not inside {min}, {max}");
  125.             }
  126.         }
  127.        
  128.  
  129.         private static void Fail(string s)
  130.         {
  131.             Console.ForegroundColor = ConsoleColor.Red;
  132.             Console.Out.WriteLine(s);
  133.             Console.ResetColor();
  134.         }
  135.  
  136.         private static void Success(string s)
  137.         {
  138.             Console.ForegroundColor = ConsoleColor.DarkGreen;
  139.             Console.Out.WriteLine(s);
  140.             Console.ResetColor();
  141.         }
  142.  
  143.         private static bool Compare(float a, float b) => Math.Abs(a - b) < EPSILON;
  144.     }
  145. #endif
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement