Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- namespace DoubleDebug
- {
- #if WINDOWS || LINUX
- /// <summary>
- /// The main class.
- /// </summary>
- public static class Program
- {
- private const float EPSILON = 0.001f;
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- var view = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
- var proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 1.0f, 0.0001f, 100.0f);
- var viewProj = view * proj;
- WriteDelimeter("Setup");
- Console.WriteLine($"View: position: {Vector3.Zero}, forward: {Vector3.Forward}, up: {Vector3.Up}");
- Console.WriteLine($"Projection: near 0.0001f, far: 100.0f, fov: 90 degrees");
- WriteDelimeter("Near and Far plane");
- var point = new Vector3(0, 0, -1);
- var transformed = Transform(point, viewProj);
- 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));
- point = new Vector3(0, 0, -99);
- transformed = Transform(point, viewProj);
- 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));
- point = new Vector3(0, 0, -101);
- transformed = Transform(point, viewProj);
- 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));
- point = new Vector3(0, 0, 1);
- transformed = Transform(point, viewProj);
- Compare($"Point {point}, behind the camera is transformed to the center of far plane of the view volume", transformed, new Vector3(0, 0, 1));
- point = new Vector3(0, 0, 99);
- transformed = Transform(point, viewProj);
- 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));
- WriteDelimeter("Uncentered");
- point = new Vector3(-1, 0, -2);
- transformed = Transform(point, viewProj);
- 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));
- point = new Vector3(-1, 0, 2);
- transformed = Transform(point, viewProj);
- 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));
- WriteDelimeter("Corners");
- var frustum = new BoundingFrustum(viewProj);
- var corners = frustum.GetCorners();
- for (var i = 0; i < corners.Length; i++)
- {
- var corner = corners[i];
- transformed = Transform(corner, viewProj);
- Inside($"Corner {corner} of the frustum is inside the view volume when transformed", transformed, -Vector3.One, Vector3.One);
- }
- Console.Read();
- }
- private static Vector3 Transform(Vector3 point, Matrix matrix)
- {
- var t = Vector4.Transform(new Vector4(point, 1), matrix);
- t /= t.W;
- return new Vector3(t.X, t.Y, t.Z);
- }
- private static void WriteDelimeter(string title)
- {
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.WriteLine(new string('*', 20) + $" {title} " + new string('*', 20));
- Console.ResetColor();
- }
- private static void Compare(string explanation, Vector3 a, Vector3 b)
- {
- var equal = Compare(a.X, b.X);
- equal &= Compare(a.Y, b.Y);
- equal &= Compare(a.Z, b.Z);
- Console.Out.WriteLine(explanation);
- if (!equal)
- {
- Fail($"\t{a} is not equal to {b}");
- }
- if (equal)
- {
- Success($"\t{a} is equal to {b}");
- }
- Console.ResetColor();
- }
- private static void Inside(string explanation, Vector3 point, Vector3 min, Vector3 max)
- {
- Console.Out.WriteLine(explanation);
- var box = new BoundingBox(min, max);
- if (box.Contains(point) != ContainmentType.Disjoint)
- {
- Success($"\t{point} is inside {min}, {max}");
- }
- else
- {
- Fail($"\t{point} is not inside {min}, {max}");
- }
- }
- private static void Fail(string s)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Out.WriteLine(s);
- Console.ResetColor();
- }
- private static void Success(string s)
- {
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.Out.WriteLine(s);
- Console.ResetColor();
- }
- private static bool Compare(float a, float b) => Math.Abs(a - b) < EPSILON;
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement