diff --git a/FModel/FModel.csproj b/FModel/FModel.csproj index f052d827..9f143a85 100644 --- a/FModel/FModel.csproj +++ b/FModel/FModel.csproj @@ -121,6 +121,9 @@ + + + diff --git a/FModel/Helper.cs b/FModel/Helper.cs index 1f4e55e4..93103474 100644 --- a/FModel/Helper.cs +++ b/FModel/Helper.cs @@ -100,4 +100,9 @@ public static class Helper var d = (Math.Abs(d1) + Math.Abs(d2) + 10) * 1.0e-15; return -d < n && d > n; } + + public static float DegreesToRadians(float degrees) + { + return MathF.PI / 180f * degrees; + } } diff --git a/FModel/MainWindow.xaml.cs b/FModel/MainWindow.xaml.cs index dd3e8854..f30583bd 100644 --- a/FModel/MainWindow.xaml.cs +++ b/FModel/MainWindow.xaml.cs @@ -11,6 +11,7 @@ using FModel.Settings; using FModel.ViewModels; using FModel.Views; using FModel.Views.Resources.Controls; +using FModel.Views.Snooper; using ICSharpCode.AvalonEdit.Editing; namespace FModel; @@ -52,6 +53,10 @@ public partial class MainWindow ApplicationService.ApiEndpointView.FModelApi.CheckForUpdates(UserSettings.Default.UpdateMode); #endif + var v = new Viewer(); + v.Run(); + return; + switch (UserSettings.Default.AesReload) { case EAesReload.Always: diff --git a/FModel/Views/Snooper/Camera.cs b/FModel/Views/Snooper/Camera.cs new file mode 100644 index 00000000..69f71276 --- /dev/null +++ b/FModel/Views/Snooper/Camera.cs @@ -0,0 +1,58 @@ +using System; +using System.Numerics; + +namespace FModel.Views.Snooper; + +public class Camera +{ + public Vector3 Position { get; set; } + public Vector3 Front { get; set; } + + public Vector3 Up { get; private set; } + public float AspectRatio { get; set; } + + public float Yaw { get; set; } = -90f; + public float Pitch { get; set; } + + private float Zoom = 45f; + + public Camera(Vector3 position, Vector3 front, Vector3 up, float aspectRatio) + { + Position = position; + AspectRatio = aspectRatio; + Front = front; + Up = up; + } + + public void ModifyZoom(float zoomAmount) + { + //We don't want to be able to zoom in too close or too far away so clamp to these values + Zoom = Math.Clamp(Zoom - zoomAmount, 1.0f, 45f); + } + + public void ModifyDirection(float xOffset, float yOffset) + { + Yaw += xOffset; + Pitch -= yOffset; + + //We don't want to be able to look behind us by going over our head or under our feet so make sure it stays within these bounds + Pitch = Math.Clamp(Pitch, -89f, 89f); + + var cameraDirection = Vector3.Zero; + cameraDirection.X = MathF.Cos(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch)); + cameraDirection.Y = MathF.Sin(Helper.DegreesToRadians(Pitch)); + cameraDirection.Z = MathF.Sin(Helper.DegreesToRadians(Yaw)) * MathF.Cos(Helper.DegreesToRadians(Pitch)); + + Front = Vector3.Normalize(cameraDirection); + } + + public Matrix4x4 GetViewMatrix() + { + return Matrix4x4.CreateLookAt(Position, Position + Front, Up); + } + + public Matrix4x4 GetProjectionMatrix() + { + return Matrix4x4.CreatePerspectiveFieldOfView(Helper.DegreesToRadians(Zoom), AspectRatio, 0.1f, 100.0f); + } +} diff --git a/FModel/Views/Snooper/Viewer.cs b/FModel/Views/Snooper/Viewer.cs new file mode 100644 index 00000000..eced863f --- /dev/null +++ b/FModel/Views/Snooper/Viewer.cs @@ -0,0 +1,67 @@ +using System; +using System.Numerics; +using Silk.NET.Input; +using Silk.NET.Maths; +using Silk.NET.OpenGL; +using Silk.NET.Windowing; + +namespace FModel.Views.Snooper; + +public class Viewer +{ + private IWindow _window; + private GL _gl; + private Camera _camera; + private IKeyboard _keyboard; + + public int Width { get; } + public int Height { get; } + + private const double _ratio = .75; + + public Viewer() + { + var x = System.Windows.SystemParameters.MaximizedPrimaryScreenWidth; + var y = System.Windows.SystemParameters.MaximizedPrimaryScreenHeight; + Width = Convert.ToInt32(x * _ratio); + Height = Convert.ToInt32(y * _ratio); + + var options = WindowOptions.Default; + options.Size = new Vector2D(Width, Height); + options.Title = "Snooper"; + _window = Window.Create(options); + + _window.Load += OnLoad; + _window.Closing += OnClose; + } + + public void Run() + { + _window.Run(); + } + + private void OnLoad() + { + var input = _window.CreateInput(); + _keyboard = input.Keyboards[0]; + _keyboard.KeyDown += KeyDown; + + _gl = GL.GetApi(_window); + + //Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis + _camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, Width / Height); + } + + private void OnClose() + { + + } + + private void KeyDown(IKeyboard keyboard, Key key, int arg3) + { + if (key == Key.Escape) + { + _window.Close(); + } + } +}