From eae459e82678ba07293c03706e8dbf26883c0788 Mon Sep 17 00:00:00 2001 From: RhysWhy <31206745+RhysWhy@users.noreply.github.com> Date: Thu, 16 Jul 2020 16:54:02 +0100 Subject: [PATCH] Improve Tutorial Camera Controls (#240) --- .../MathHelper.cs | 12 +++++++ .../Program.cs | 12 ++----- .../Tutorial 2.2 - Camera/MathHelper.cs | 12 +++++++ .../CSharp/Tutorial 2.2 - Camera/Program.cs | 31 +++++++------------ .../Tutorial 3.1 - Ambient Lighting/Camera.cs | 2 +- .../MathHelper.cs | 4 +-- .../Program.cs | 3 +- .../Tutorial 3.2 - Diffuse Lighting/Camera.cs | 2 +- .../Program.cs | 3 +- .../Camera.cs | 2 +- .../MathHelper.cs | 4 +-- .../Program.cs | 3 +- .../CSharp/Tutorial 3.4 - Materials/Camera.cs | 2 +- .../Tutorial 3.4 - Materials/MathHelper.cs | 4 +-- .../Tutorial 3.4 - Materials/Program.cs | 3 +- 15 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/MathHelper.cs create mode 100644 examples/CSharp/Tutorial 2.2 - Camera/MathHelper.cs diff --git a/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/MathHelper.cs b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/MathHelper.cs new file mode 100644 index 000000000..74dd07e65 --- /dev/null +++ b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/MathHelper.cs @@ -0,0 +1,12 @@ +using System; + +namespace Tutorial +{ + public static class MathHelper + { + public static float DegreesToRadians(float degrees) + { + return MathF.PI / 180f * degrees; + } + } +} \ No newline at end of file diff --git a/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Program.cs b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Program.cs index 7267dcdbf..6597b8694 100644 --- a/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Program.cs +++ b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Program.cs @@ -3,7 +3,6 @@ using Silk.NET.Input.Common; using Silk.NET.OpenGL; using Silk.NET.Windowing; using Silk.NET.Windowing.Common; -using System; using System.Drawing; using System.Numerics; @@ -129,11 +128,11 @@ namespace Tutorial Shader.SetUniform("uTexture0", 0); //Use elapsed time to convert to radians to allow our cube to rotate over time - var difference = window.Time * 100; + var difference = (float)(window.Time * 100); - var model = Matrix4x4.CreateRotationY((float)DegreesToRadians(difference)) * Matrix4x4.CreateRotationX((float)DegreesToRadians(difference)); + var model = Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(difference)) * Matrix4x4.CreateRotationX(MathHelper.DegreesToRadians(difference)); var view = Matrix4x4.CreateLookAt(CameraPosition, CameraTarget, CameraUp); - var projection = Matrix4x4.CreatePerspectiveFieldOfView((float)DegreesToRadians(45.0f), Width / Height, 0.1f, 100.0f); + var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), Width / Height, 0.1f, 100.0f); Shader.SetUniform("uModel", model); Shader.SetUniform("uView", view); @@ -159,10 +158,5 @@ namespace Tutorial window.Close(); } } - - private static double DegreesToRadians(double degrees) - { - return (Math.PI / 180f) * degrees; - } } } \ No newline at end of file diff --git a/examples/CSharp/Tutorial 2.2 - Camera/MathHelper.cs b/examples/CSharp/Tutorial 2.2 - Camera/MathHelper.cs new file mode 100644 index 000000000..74dd07e65 --- /dev/null +++ b/examples/CSharp/Tutorial 2.2 - Camera/MathHelper.cs @@ -0,0 +1,12 @@ +using System; + +namespace Tutorial +{ + public static class MathHelper + { + public static float DegreesToRadians(float degrees) + { + return MathF.PI / 180f * degrees; + } + } +} \ No newline at end of file diff --git a/examples/CSharp/Tutorial 2.2 - Camera/Program.cs b/examples/CSharp/Tutorial 2.2 - Camera/Program.cs index 76c3013d3..1f3c9e3e2 100644 --- a/examples/CSharp/Tutorial 2.2 - Camera/Program.cs +++ b/examples/CSharp/Tutorial 2.2 - Camera/Program.cs @@ -28,7 +28,7 @@ namespace Tutorial //Setup the cameras location, directions, and movement speed private static Vector3 CameraPosition = new Vector3(0.0f, 0.0f, 3.0f); private static Vector3 CameraFront = new Vector3(0.0f, 0.0f, -1.0f); - private static Vector3 CameraUp = new Vector3(0.0f, 1.0f, 3.0f); + private static Vector3 CameraUp = Vector3.UnitY; private static Vector3 CameraDirection = Vector3.Zero; private static float CameraYaw = -90f; private static float CameraPitch = 0f; @@ -37,9 +37,6 @@ namespace Tutorial //Used to track change in mouse movement to allow for moving of the Camera private static PointF LastMousePosition; - //Track when the window started so we can use the time elapsed to rotate the cube - private static DateTime StartTime; - private static readonly float[] Vertices = { //X Y Z U V @@ -109,7 +106,6 @@ namespace Tutorial private static void OnLoad() { - StartTime = DateTime.UtcNow; IInputContext input = window.CreateInput(); primaryKeyboard = input.Keyboards.FirstOrDefault(); if (primaryKeyboard != null) @@ -118,6 +114,7 @@ namespace Tutorial } for (int i = 0; i < input.Mice.Count; i++) { + input.Mice[i].Cursor.CursorMode = CursorMode.Raw; input.Mice[i].MouseMove += OnMouseMove; input.Mice[i].Scroll += OnMouseWheel; } @@ -173,11 +170,11 @@ namespace Tutorial Shader.SetUniform("uTexture0", 0); //Use elapsed time to convert to radians to allow our cube to rotate over time - var difference = (DateTime.UtcNow - StartTime).TotalMilliseconds / 10; + var difference = (float)(window.Time * 100); - var model = Matrix4x4.CreateRotationY((float)DegreesToRadians(difference)) * Matrix4x4.CreateRotationX((float)DegreesToRadians(difference)); + var model = Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(difference)) * Matrix4x4.CreateRotationX(MathHelper.DegreesToRadians(difference)); var view = Matrix4x4.CreateLookAt(CameraPosition, CameraPosition + CameraFront, CameraUp); - var projection = Matrix4x4.CreatePerspectiveFieldOfView((float)DegreesToRadians(CameraZoom), Width / Height, 0.1f, 100.0f); + var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(CameraZoom), Width / Height, 0.1f, 100.0f); Shader.SetUniform("uModel", model); Shader.SetUniform("uView", view); @@ -189,7 +186,7 @@ namespace Tutorial private static unsafe void OnMouseMove(IMouse mouse, PointF position) { - var lookSensitivity = 0.02f; + var lookSensitivity = 0.1f; if (LastMousePosition == default) { LastMousePosition = position; } else { @@ -198,15 +195,14 @@ namespace Tutorial LastMousePosition = position; CameraYaw += xOffset; - CameraPitch += yOffset; + CameraPitch -= 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 - if (CameraPitch > 89.0f) { CameraPitch = 89.0f; } - if (CameraPitch < -89.0f) { CameraPitch = -89.0f; } + CameraPitch = Math.Clamp(CameraPitch, -89.0f, 89.0f); - CameraDirection.X = MathF.Cos((float)DegreesToRadians(CameraYaw)) * MathF.Cos((float)DegreesToRadians(CameraPitch)); - CameraDirection.Y = MathF.Sin((float)DegreesToRadians(CameraPitch)); - CameraDirection.Z = MathF.Sin((float)DegreesToRadians(CameraYaw)) * MathF.Cos((float)DegreesToRadians(CameraPitch)); + CameraDirection.X = MathF.Cos(MathHelper.DegreesToRadians(CameraYaw)) * MathF.Cos(MathHelper.DegreesToRadians(CameraPitch)); + CameraDirection.Y = MathF.Sin(MathHelper.DegreesToRadians(CameraPitch)); + CameraDirection.Z = MathF.Sin(MathHelper.DegreesToRadians(CameraYaw)) * MathF.Cos(MathHelper.DegreesToRadians(CameraPitch)); CameraFront = Vector3.Normalize(CameraDirection); } } @@ -233,10 +229,5 @@ namespace Tutorial window.Close(); } } - - private static double DegreesToRadians(double degrees) - { - return (Math.PI / 180f) * degrees; - } } } \ No newline at end of file diff --git a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Camera.cs b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Camera.cs index 2803dbcf0..2a2a871f0 100644 --- a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Camera.cs +++ b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Camera.cs @@ -33,7 +33,7 @@ namespace Tutorial public void ModifyDirection(float xOffset, float yOffset) { Yaw += xOffset; - Pitch += yOffset; + 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); diff --git a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/MathHelper.cs b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/MathHelper.cs index 36e95f6d5..74dd07e65 100644 --- a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/MathHelper.cs +++ b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/MathHelper.cs @@ -4,9 +4,9 @@ namespace Tutorial { public static class MathHelper { - public static float DegreesToRadians(double degrees) + public static float DegreesToRadians(float degrees) { - return (float)(Math.PI / 180f * degrees); + return MathF.PI / 180f * degrees; } } } \ No newline at end of file diff --git a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Program.cs b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Program.cs index a6a62ca3c..05e3fde29 100644 --- a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Program.cs +++ b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Program.cs @@ -106,6 +106,7 @@ namespace Tutorial } for (int i = 0; i < input.Mice.Count; i++) { + input.Mice[i].Cursor.CursorMode = CursorMode.Raw; input.Mice[i].MouseMove += OnMouseMove; input.Mice[i].Scroll += OnMouseWheel; } @@ -187,7 +188,7 @@ namespace Tutorial private static unsafe void OnMouseMove(IMouse mouse, PointF position) { - var lookSensitivity = 0.02f; + var lookSensitivity = 0.1f; if (LastMousePosition == default) { LastMousePosition = position; } else { diff --git a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Camera.cs b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Camera.cs index 2803dbcf0..2a2a871f0 100644 --- a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Camera.cs +++ b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Camera.cs @@ -33,7 +33,7 @@ namespace Tutorial public void ModifyDirection(float xOffset, float yOffset) { Yaw += xOffset; - Pitch += yOffset; + 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); diff --git a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Program.cs b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Program.cs index a7732dbf0..02dd75c9b 100644 --- a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Program.cs +++ b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Program.cs @@ -107,6 +107,7 @@ namespace Tutorial } for (int i = 0; i < input.Mice.Count; i++) { + input.Mice[i].Cursor.CursorMode = CursorMode.Raw; input.Mice[i].MouseMove += OnMouseMove; input.Mice[i].Scroll += OnMouseWheel; } @@ -206,7 +207,7 @@ namespace Tutorial private static unsafe void OnMouseMove(IMouse mouse, PointF position) { - var lookSensitivity = 0.02f; + var lookSensitivity = 0.1f; if (LastMousePosition == default) { LastMousePosition = position; } else { diff --git a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Camera.cs b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Camera.cs index 2803dbcf0..2a2a871f0 100644 --- a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Camera.cs +++ b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Camera.cs @@ -33,7 +33,7 @@ namespace Tutorial public void ModifyDirection(float xOffset, float yOffset) { Yaw += xOffset; - Pitch += yOffset; + 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); diff --git a/examples/CSharp/Tutorial 3.3 - Specular Lighting/MathHelper.cs b/examples/CSharp/Tutorial 3.3 - Specular Lighting/MathHelper.cs index 36e95f6d5..74dd07e65 100644 --- a/examples/CSharp/Tutorial 3.3 - Specular Lighting/MathHelper.cs +++ b/examples/CSharp/Tutorial 3.3 - Specular Lighting/MathHelper.cs @@ -4,9 +4,9 @@ namespace Tutorial { public static class MathHelper { - public static float DegreesToRadians(double degrees) + public static float DegreesToRadians(float degrees) { - return (float)(Math.PI / 180f * degrees); + return MathF.PI / 180f * degrees; } } } \ No newline at end of file diff --git a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Program.cs b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Program.cs index eb697ad3c..6a4cdd056 100644 --- a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Program.cs +++ b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Program.cs @@ -107,6 +107,7 @@ namespace Tutorial } for (int i = 0; i < input.Mice.Count; i++) { + input.Mice[i].Cursor.CursorMode = CursorMode.Raw; input.Mice[i].MouseMove += OnMouseMove; input.Mice[i].Scroll += OnMouseWheel; } @@ -191,7 +192,7 @@ namespace Tutorial private static unsafe void OnMouseMove(IMouse mouse, PointF position) { - var lookSensitivity = 0.02f; + var lookSensitivity = 0.1f; if (LastMousePosition == default) { LastMousePosition = position; } else { diff --git a/examples/CSharp/Tutorial 3.4 - Materials/Camera.cs b/examples/CSharp/Tutorial 3.4 - Materials/Camera.cs index 2803dbcf0..2a2a871f0 100644 --- a/examples/CSharp/Tutorial 3.4 - Materials/Camera.cs +++ b/examples/CSharp/Tutorial 3.4 - Materials/Camera.cs @@ -33,7 +33,7 @@ namespace Tutorial public void ModifyDirection(float xOffset, float yOffset) { Yaw += xOffset; - Pitch += yOffset; + 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); diff --git a/examples/CSharp/Tutorial 3.4 - Materials/MathHelper.cs b/examples/CSharp/Tutorial 3.4 - Materials/MathHelper.cs index 36e95f6d5..74dd07e65 100644 --- a/examples/CSharp/Tutorial 3.4 - Materials/MathHelper.cs +++ b/examples/CSharp/Tutorial 3.4 - Materials/MathHelper.cs @@ -4,9 +4,9 @@ namespace Tutorial { public static class MathHelper { - public static float DegreesToRadians(double degrees) + public static float DegreesToRadians(float degrees) { - return (float)(Math.PI / 180f * degrees); + return MathF.PI / 180f * degrees; } } } \ No newline at end of file diff --git a/examples/CSharp/Tutorial 3.4 - Materials/Program.cs b/examples/CSharp/Tutorial 3.4 - Materials/Program.cs index 8a51068da..19bf6d70b 100644 --- a/examples/CSharp/Tutorial 3.4 - Materials/Program.cs +++ b/examples/CSharp/Tutorial 3.4 - Materials/Program.cs @@ -112,6 +112,7 @@ namespace Tutorial } for (int i = 0; i < input.Mice.Count; i++) { + input.Mice[i].Cursor.CursorMode = CursorMode.Raw; input.Mice[i].MouseMove += OnMouseMove; input.Mice[i].Scroll += OnMouseWheel; } @@ -212,7 +213,7 @@ namespace Tutorial private static unsafe void OnMouseMove(IMouse mouse, PointF position) { - var lookSensitivity = 0.02f; + var lookSensitivity = 0.1f; if (LastMousePosition == default) { LastMousePosition = position; } else {