diff --git a/build/props/common.props b/build/props/common.props index 700b47a0d0..8aafb47205 100644 --- a/build/props/common.props +++ b/build/props/common.props @@ -24,13 +24,13 @@ - + true true true snupkg - + diff --git a/examples/CSharp/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj b/examples/CSharp/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj index b297a9d61b..19b0ecd447 100644 --- a/examples/CSharp/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj +++ b/examples/CSharp/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial diff --git a/examples/CSharp/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj b/examples/CSharp/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj index 7a76e2d548..03db8d02f3 100644 --- a/examples/CSharp/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj +++ b/examples/CSharp/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 1.3 - Abstractions/Tutorial 1.3 - Abstraction.csproj b/examples/CSharp/Tutorial 1.3 - Abstractions/Tutorial 1.3 - Abstraction.csproj index 445eab3047..6b5f9b63ac 100644 --- a/examples/CSharp/Tutorial 1.3 - Abstractions/Tutorial 1.3 - Abstraction.csproj +++ b/examples/CSharp/Tutorial 1.3 - Abstractions/Tutorial 1.3 - Abstraction.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 1.4 - Textures/Tutorial 1.4 - Textures.csproj b/examples/CSharp/Tutorial 1.4 - Textures/Tutorial 1.4 - Textures.csproj index c18b3c2ceb..2c39ae8a8b 100644 --- a/examples/CSharp/Tutorial 1.4 - Textures/Tutorial 1.4 - Textures.csproj +++ b/examples/CSharp/Tutorial 1.4 - Textures/Tutorial 1.4 - Textures.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 1.5 - Transformations/Tutorial 1.5 - Transformations.csproj b/examples/CSharp/Tutorial 1.5 - Transformations/Tutorial 1.5 - Transformations.csproj index c18b3c2ceb..2c39ae8a8b 100644 --- a/examples/CSharp/Tutorial 1.5 - Transformations/Tutorial 1.5 - Transformations.csproj +++ b/examples/CSharp/Tutorial 1.5 - Transformations/Tutorial 1.5 - Transformations.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Tutorial 2.1 - Co-ordinate Systems.csproj b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Tutorial 2.1 - Co-ordinate Systems.csproj index c18b3c2ceb..2c39ae8a8b 100644 --- a/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Tutorial 2.1 - Co-ordinate Systems.csproj +++ b/examples/CSharp/Tutorial 2.1 - Co-ordinate Systems/Tutorial 2.1 - Co-ordinate Systems.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 2.2 - Camera/Tutorial 2.2 - Camera.csproj b/examples/CSharp/Tutorial 2.2 - Camera/Tutorial 2.2 - Camera.csproj index c18b3c2ceb..2c39ae8a8b 100644 --- a/examples/CSharp/Tutorial 2.2 - Camera/Tutorial 2.2 - Camera.csproj +++ b/examples/CSharp/Tutorial 2.2 - Camera/Tutorial 2.2 - Camera.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Tutorial 3.1 - Ambient Lighting.csproj b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Tutorial 3.1 - Ambient Lighting.csproj index 3c61da8429..866ee4544c 100644 --- a/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Tutorial 3.1 - Ambient Lighting.csproj +++ b/examples/CSharp/Tutorial 3.1 - Ambient Lighting/Tutorial 3.1 - Ambient Lighting.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Tutorial 3.2 - Diffuse Lighting.csproj b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Tutorial 3.2 - Diffuse Lighting.csproj index 3c61da8429..866ee4544c 100644 --- a/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Tutorial 3.2 - Diffuse Lighting.csproj +++ b/examples/CSharp/Tutorial 3.2 - Diffuse Lighting/Tutorial 3.2 - Diffuse Lighting.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Tutorial 3.3 - Specular Lighting.csproj b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Tutorial 3.3 - Specular Lighting.csproj index 3c61da8429..866ee4544c 100644 --- a/examples/CSharp/Tutorial 3.3 - Specular Lighting/Tutorial 3.3 - Specular Lighting.csproj +++ b/examples/CSharp/Tutorial 3.3 - Specular Lighting/Tutorial 3.3 - Specular Lighting.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 3.4 - Materials/Tutorial 3.4 - Materials.csproj b/examples/CSharp/Tutorial 3.4 - Materials/Tutorial 3.4 - Materials.csproj index 3c61da8429..866ee4544c 100644 --- a/examples/CSharp/Tutorial 3.4 - Materials/Tutorial 3.4 - Materials.csproj +++ b/examples/CSharp/Tutorial 3.4 - Materials/Tutorial 3.4 - Materials.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/examples/CSharp/Tutorial 3.5 - Lighting Maps/Tutorial 3.5 - Lighting Maps.csproj b/examples/CSharp/Tutorial 3.5 - Lighting Maps/Tutorial 3.5 - Lighting Maps.csproj index 7b3932f1d3..0ac7ffa7a1 100644 --- a/examples/CSharp/Tutorial 3.5 - Lighting Maps/Tutorial 3.5 - Lighting Maps.csproj +++ b/examples/CSharp/Tutorial 3.5 - Lighting Maps/Tutorial 3.5 - Lighting Maps.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Tutorial true diff --git a/src/Core/Silk.NET.BuildTools/Silk.NET.BuildTools.csproj b/src/Core/Silk.NET.BuildTools/Silk.NET.BuildTools.csproj index 769e26fe28..82e6406be1 100644 --- a/src/Core/Silk.NET.BuildTools/Silk.NET.BuildTools.csproj +++ b/src/Core/Silk.NET.BuildTools/Silk.NET.BuildTools.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 Silk.NET.BuildTools false true diff --git a/src/Core/Silk.NET.Core/Loader/LibraryLoader.cs b/src/Core/Silk.NET.Core/Loader/LibraryLoader.cs index ebff33968b..692a2109da 100644 --- a/src/Core/Silk.NET.Core/Loader/LibraryLoader.cs +++ b/src/Core/Silk.NET.Core/Loader/LibraryLoader.cs @@ -3,7 +3,7 @@ // You may modify and distribute Silk.NET under the terms // of the MIT license. See the LICENSE file for details. -#if NETCOREAPP3_0 +#if NETCOREAPP3_1 using System.Reflection; using NativeLibrary3 = System.Runtime.InteropServices.NativeLibrary; #else @@ -303,8 +303,8 @@ public void FreeNativeLibrary(IntPtr handle) /// A LibraryLoader suitable for loading libraries. public static LibraryLoader GetPlatformDefaultLoader() { -#if NETCOREAPP3_0 - return new NetCoreNativeLibraryLoader(); +#if NETCOREAPP3_1 + return new NetNextNativeLibraryLoader(); #else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) @@ -333,12 +333,12 @@ private static void PlatformNotSupported() throw new PlatformNotSupportedException("This platform cannot load native libraries."); } -#if NETCOREAPP3_0 - private class NetCoreNativeLibraryLoader : LibraryLoader +#if NETCOREAPP3_1 + private class NetNextNativeLibraryLoader : LibraryLoader { protected override IntPtr CoreLoadNativeLibrary(string name) { - if (NativeLibrary3.TryLoad(name, Assembly.GetCallingAssembly(), null, out var lib)) + if (NativeLibrary3.TryLoad(name, out var lib)) { return lib; } diff --git a/src/Core/Silk.NET.Core/Silk.NET.Core.csproj b/src/Core/Silk.NET.Core/Silk.NET.Core.csproj index f95dbf6e81..e761158598 100644 --- a/src/Core/Silk.NET.Core/Silk.NET.Core.csproj +++ b/src/Core/Silk.NET.Core/Silk.NET.Core.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.0;netcoreapp3.1 true preview diff --git a/src/Core/Silk.NET.SilkTouch/Silk.NET.SilkTouch.csproj b/src/Core/Silk.NET.SilkTouch/Silk.NET.SilkTouch.csproj index 7a13ccd969..c828fd1845 100644 --- a/src/Core/Silk.NET.SilkTouch/Silk.NET.SilkTouch.csproj +++ b/src/Core/Silk.NET.SilkTouch/Silk.NET.SilkTouch.csproj @@ -2,11 +2,15 @@ netstandard2.0 - preview enable false + false + + https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json ;$(RestoreAdditionalProjectSources) + + @@ -19,4 +23,6 @@ + + diff --git a/src/Input/Silk.NET.Input.Common/InputWindowExtensions.cs b/src/Input/Silk.NET.Input.Common/InputWindowExtensions.cs index f1fb0f27f7..ec8effdfbe 100644 --- a/src/Input/Silk.NET.Input.Common/InputWindowExtensions.cs +++ b/src/Input/Silk.NET.Input.Common/InputWindowExtensions.cs @@ -15,12 +15,36 @@ namespace Silk.NET.Input /// public static class InputWindowExtensions { + private static List _platforms = new List(); + /// /// Gets the input platforms currently registered with the input system. /// - public static IReadOnlyList Platforms { get; } = new List(); + public static IReadOnlyList Platforms + { + get + { + if (!_initializedFirstPartyPlatforms) + { + DoLoadFirstPartyPlatformsViaReflection(); + _initializedFirstPartyPlatforms = true; + } + + return _platforms; + } + } + + private static bool _initializedFirstPartyPlatforms = false; - static InputWindowExtensions() + public static void ShouldLoadFirstPartyPlatforms(bool shouldLoad) + { + if (_initializedFirstPartyPlatforms) + throw new InvalidOperationException("Input Platforms already loaded, cannot change first party loading"); + + _initializedFirstPartyPlatforms = !shouldLoad; + } + + private static void DoLoadFirstPartyPlatformsViaReflection() { TryAdd("Silk.NET.Input.Glfw"); TryAdd("Silk.NET.Input.Sdl"); @@ -52,13 +76,13 @@ public static IInputContext CreateInput(this IView view) /// Adds this input platform to the platform list. Shouldn't be used unless writing your own input backend. /// /// The platform to add. - public static void Add(IInputPlatform platform) => ((List) Platforms).Add(platform); + public static void Add(IInputPlatform platform) => _platforms.Add(platform); /// /// Removes this input platform from the platform list. Shouldn't be used unless writing your own input backend. /// /// The platform to remove. - public static void Remove(IInputPlatform platform) => ((List) Platforms).Remove(platform); + public static void Remove(IInputPlatform platform) => _platforms.Remove(platform); /// /// Attempts to load the given assembly by name, checks for a , if one is found it diff --git a/src/Input/Silk.NET.Input.Glfw/GlfwInput.cs b/src/Input/Silk.NET.Input.Glfw/GlfwInput.cs new file mode 100644 index 0000000000..c21a4ce8d6 --- /dev/null +++ b/src/Input/Silk.NET.Input.Glfw/GlfwInput.cs @@ -0,0 +1,19 @@ +// This file is part of Silk.NET. +// +// You may modify and distribute Silk.NET under the terms +// of the MIT license. See the LICENSE file for details. + +using Silk.NET.Windowing; +using Silk.NET.Windowing.Glfw; + +namespace Silk.NET.Input.Glfw +{ + public static class GlfwInput + { + public static void RegisterPlatform() + { + Window.Add(new GlfwPlatform()); + InputWindowExtensions.Add(new GlfwInputPlatform()); + } + } +} diff --git a/src/Input/Silk.NET.Input.Sdl/SdlInput.cs b/src/Input/Silk.NET.Input.Sdl/SdlInput.cs new file mode 100644 index 0000000000..284049ec28 --- /dev/null +++ b/src/Input/Silk.NET.Input.Sdl/SdlInput.cs @@ -0,0 +1,19 @@ +// This file is part of Silk.NET. +// +// You may modify and distribute Silk.NET under the terms +// of the MIT license. See the LICENSE file for details. + +using Silk.NET.Windowing; +using Silk.NET.Windowing.Sdl; + +namespace Silk.NET.Input.Sdl +{ + public static class SdlInput + { + public static void RegisterPlatform() + { + Window.Add(new SdlPlatform()); + InputWindowExtensions.Add(new SdlInputPlatform()); + } + } +} diff --git a/src/Lab/BlankWindow/BlankWindow.csproj b/src/Lab/BlankWindow/BlankWindow.csproj index d2ee015bd5..925b0b66fd 100644 --- a/src/Lab/BlankWindow/BlankWindow.csproj +++ b/src/Lab/BlankWindow/BlankWindow.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3 + netcoreapp3.1 true diff --git a/src/Lab/CoreRTTest/BufferObject.cs b/src/Lab/CoreRTTest/BufferObject.cs new file mode 100644 index 0000000000..dba6db47e2 --- /dev/null +++ b/src/Lab/CoreRTTest/BufferObject.cs @@ -0,0 +1,36 @@ +using Silk.NET.OpenGL; +using System; + +namespace Tutorial +{ + public class BufferObject : IDisposable + where TDataType : unmanaged + { + private uint _handle; + private BufferTargetARB _bufferType; + private GL _gl; + + public unsafe BufferObject(GL gl, Span data, BufferTargetARB bufferType) + { + _gl = gl; + _bufferType = bufferType; + + _handle = _gl.GenBuffer(); + Bind(); + fixed (void* d = data) + { + _gl.BufferData(bufferType, (UIntPtr)(data.Length * sizeof(TDataType)), d, BufferUsageARB.StaticDraw); + } + } + + public void Bind() + { + _gl.BindBuffer(_bufferType, _handle); + } + + public void Dispose() + { + _gl.DeleteBuffer(_handle); + } + } +} diff --git a/src/Lab/CoreRTTest/Camera.cs b/src/Lab/CoreRTTest/Camera.cs new file mode 100644 index 0000000000..2a2a871f0a --- /dev/null +++ b/src/Lab/CoreRTTest/Camera.cs @@ -0,0 +1,59 @@ +using System; +using System.Numerics; + +namespace Tutorial +{ + 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(MathHelper.DegreesToRadians(Yaw)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch)); + cameraDirection.Y = MathF.Sin(MathHelper.DegreesToRadians(Pitch)); + cameraDirection.Z = MathF.Sin(MathHelper.DegreesToRadians(Yaw)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch)); + + Front = Vector3.Normalize(cameraDirection); + } + + public Matrix4x4 GetViewMatrix() + { + return Matrix4x4.CreateLookAt(Position, Position + Front, Up); + } + + public Matrix4x4 GetProjectionMatrix() + { + return Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(Zoom), AspectRatio, 0.1f, 100.0f); + } + } +} \ No newline at end of file diff --git a/src/Lab/CoreRTTest/CoreRTTest.csproj b/src/Lab/CoreRTTest/CoreRTTest.csproj index fb71e17439..736f294732 100644 --- a/src/Lab/CoreRTTest/CoreRTTest.csproj +++ b/src/Lab/CoreRTTest/CoreRTTest.csproj @@ -1,19 +1,9 @@ - - - - 3.3 - Specular Lighting - - - - - $(MSBuildThisFileDirectory)../../../examples/CSharp/Tutorial $(Tutorial)/ - Exe - netcoreapp3.0 + net5.0 Tutorial true @@ -28,14 +18,35 @@ - - - - - + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + Speed + false + false + false + false + false + true + + https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json;$(RestoreAdditionalProjectSources) diff --git a/src/Lab/CoreRTTest/MathHelper.cs b/src/Lab/CoreRTTest/MathHelper.cs new file mode 100644 index 0000000000..74dd07e659 --- /dev/null +++ b/src/Lab/CoreRTTest/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/src/Lab/CoreRTTest/Program.cs b/src/Lab/CoreRTTest/Program.cs new file mode 100644 index 0000000000..ff813f9d1c --- /dev/null +++ b/src/Lab/CoreRTTest/Program.cs @@ -0,0 +1,268 @@ +using Silk.NET.Input; +using Silk.NET.OpenGL; +using Silk.NET.Windowing; +using System; +using System.Drawing; +using System.Linq; +using System.Numerics; + +namespace Tutorial +{ + class Program + { + private static IWindow window; + private static GL Gl; + private static IKeyboard primaryKeyboard; + + private const int Width = 800; + private const int Height = 700; + + private static BufferObject Vbo; + private static BufferObject Ebo; + private static VertexArrayObject VaoCube; + private static Shader LightingShader; + private static Shader LampShader; + private static Vector3 LampPosition = new Vector3(1.2f, 1.0f, 2.0f); + + private static Texture DiffuseMap; + private static Texture SpecularMap; + + private static Camera Camera; + + //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 Normals U V + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f + }; + + private static readonly uint[] Indices = + { + 0, 1, 3, + 1, 2, 3 + }; + + private static void Main() + { + Silk.NET.Input.Glfw.GlfwInput.RegisterPlatform(); + Silk.NET.Input.Sdl.SdlInput.RegisterPlatform(); + + foreach (var s in Window.Platforms.Select + (x => $"IsApplicable: {x.IsApplicable} | IsViewOnly: {x.IsViewOnly}")) + Console.WriteLine(s); + + var options = new WindowOptions + ( + true, new Point(50, 50), new Size(1280, 720), 0.0, 0.0, GraphicsAPI.Default, "Silk.NET Window AOT", + WindowState.Normal, WindowBorder.Resizable, true, true, VideoMode.Default + ); + options.Size = new Size(800, 600); + options.Title = "LearnOpenGL with Silk.NET"; + window = Window.Create(options); + + window.Load += OnLoad; + window.Update += OnUpdate; + window.Render += OnRender; + window.Closing += OnClose; + + window.Run(); + } + + private static void OnLoad() + { + StartTime = DateTime.UtcNow; + IInputContext input = window.CreateInput(); + primaryKeyboard = input.Keyboards.FirstOrDefault(); + if (primaryKeyboard != null) + { + primaryKeyboard.KeyDown += KeyDown; + } + 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; + } + + Gl = GL.GetApi(window); + + Ebo = new BufferObject(Gl, Indices, BufferTargetARB.ElementArrayBuffer); + Vbo = new BufferObject(Gl, Vertices, BufferTargetARB.ArrayBuffer); + VaoCube = new VertexArrayObject(Gl, Vbo, Ebo); + + VaoCube.VertexAttributePointer(0, 3, VertexAttribPointerType.Float, 8, 0); + VaoCube.VertexAttributePointer(1, 3, VertexAttribPointerType.Float, 8, 3); + VaoCube.VertexAttributePointer(2, 2, VertexAttribPointerType.Float, 8, 6); + + //The lighting shader will give our main cube its colour multiplied by the lights intensity + LightingShader = new Shader(Gl, "shader.vert", "lighting.frag"); + //The Lamp shader uses a fragment shader that just colours it solid white so that we know it is the light source + LampShader = new Shader(Gl, "shader.vert", "shader.frag"); + + //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); + + DiffuseMap = new Texture(Gl, "silkBoxed.png"); + SpecularMap = new Texture(Gl, "silkSpecular.png"); + } + + private static unsafe void OnUpdate(double deltaTime) + { + var moveSpeed = 2.5f * (float)deltaTime; + + if (primaryKeyboard.IsKeyPressed(Key.W)) + { + //Move forwards + Camera.Position += moveSpeed * Camera.Front; + } + if (primaryKeyboard.IsKeyPressed(Key.S)) + { + //Move backwards + Camera.Position -= moveSpeed * Camera.Front; + } + if (primaryKeyboard.IsKeyPressed(Key.A)) + { + //Move left + Camera.Position -= Vector3.Normalize(Vector3.Cross(Camera.Front, Camera.Up)) * moveSpeed; + } + if (primaryKeyboard.IsKeyPressed(Key.D)) + { + //Move right + Camera.Position += Vector3.Normalize(Vector3.Cross(Camera.Front, Camera.Up)) * moveSpeed; + } + } + + private static unsafe void OnRender(double deltaTime) + { + Gl.Enable(EnableCap.DepthTest); + Gl.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit)); + + VaoCube.Bind(); + LightingShader.Use(); + + //Bind the diffuse map and and set to use texture0. + DiffuseMap.Bind(TextureUnit.Texture0); + //Bind the diffuse map and and set to use texture10. + SpecularMap.Bind(TextureUnit.Texture1); + + //Setup the coordinate systems for our view + LightingShader.SetUniform("uModel", Matrix4x4.Identity); + LightingShader.SetUniform("uView", Camera.GetViewMatrix()); + LightingShader.SetUniform("uProjection", Camera.GetProjectionMatrix()); + //Let the shaders know where the Camera is looking from + LightingShader.SetUniform("viewPos", Camera.Position); + //Configure the materials variables. + //Diffuse is set to 0 because our diffuseMap is bound to Texture0 + LightingShader.SetUniform("material.diffuse", 0); + //Specular is set to 1 because our diffuseMap is bound to Texture1 + LightingShader.SetUniform("material.specular", 1); + LightingShader.SetUniform("material.shininess", 32.0f); + + var diffuseColor = new Vector3(0.5f); + var ambientColor = diffuseColor * new Vector3(0.2f); + + LightingShader.SetUniform("light.ambient", ambientColor); + LightingShader.SetUniform("light.diffuse", diffuseColor); // darkened + LightingShader.SetUniform("light.specular", new Vector3(1.0f, 1.0f, 1.0f)); + LightingShader.SetUniform("light.position", LampPosition); + + //We're drawing with just vertices and no indicies, and it takes 36 verticies to have a six-sided textured cube + Gl.DrawArrays(PrimitiveType.Triangles, 0, 36); + + LampShader.Use(); + + //The Lamp cube is going to be a scaled down version of the normal cubes verticies moved to a different screen location + var lampMatrix = Matrix4x4.Identity; + lampMatrix *= Matrix4x4.CreateScale(0.2f); + lampMatrix *= Matrix4x4.CreateTranslation(LampPosition); + + LampShader.SetUniform("uModel", lampMatrix); + LampShader.SetUniform("uView", Camera.GetViewMatrix()); + LampShader.SetUniform("uProjection", Camera.GetProjectionMatrix()); + + Gl.DrawArrays(PrimitiveType.Triangles, 0, 36); + } + + private static unsafe void OnMouseMove(IMouse mouse, PointF position) + { + const float lookSensitivity = 0.1f; + if (LastMousePosition == default) { LastMousePosition = position; } + else + { + var xOffset = (position.X - LastMousePosition.X) * lookSensitivity; + var yOffset = (position.Y - LastMousePosition.Y) * lookSensitivity; + LastMousePosition = position; + + Camera.ModifyDirection(xOffset, yOffset); + } + } + + private static unsafe void OnMouseWheel(IMouse mouse, ScrollWheel scrollWheel) + { + Camera.ModifyZoom(scrollWheel.Y); + } + + private static void OnClose() + { + Vbo.Dispose(); + Ebo.Dispose(); + VaoCube.Dispose(); + LightingShader.Dispose(); + LampShader.Dispose(); + DiffuseMap.Dispose(); + } + + private static void KeyDown(IKeyboard keyboard, Key key, int arg3) + { + if (key == Key.Escape) + { + window.Close(); + } + } + } +} \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Linux64_Debug.sh b/src/Lab/CoreRTTest/RunTest_Linux64_Debug.sh index b1e7d47b9f..1051dbd9d7 100644 --- a/src/Lab/CoreRTTest/RunTest_Linux64_Debug.sh +++ b/src/Lab/CoreRTTest/RunTest_Linux64_Debug.sh @@ -1,5 +1,10 @@ #!/bin/sh dotnet publish -r linux-x64 -c Debug -cp ./bin/Debug/netcoreapp3.0/linux-x64/libglfw.so.3 ./bin/Debug/netcoreapp3.0/linux-x64/native/libglfw.so.3 -cd ./bin/Debug/netcoreapp3.0/linux-x64/native/ +cp ./bin/Debug/net5.0/linux-x64/libglfw.so.3 ./bin/Debug/net5.0/linux-x64/native/libglfw.so.3 +cp ./bin/Debug/net5.0/linux-x64/lighting.frag ./bin/Debug/net5.0/linux-x64/native/lighting.frag +cp ./bin/Debug/net5.0/linux-x64/shader.frag ./bin/Debug/net5.0/linux-x64/native/shader.frag +cp ./bin/Debug/net5.0/linux-x64/shader.vert ./bin/Debug/net5.0/linux-x64/native/shader.vert +cp ./bin/Debug/net5.0/linux-x64/silkBoxed.png ./bin/Debug/net5.0/linux-x64/native/silkBoxed.png +cp ./bin/Debug/net5.0/linux-x64/silkSpecular.png ./bin/Debug/net5.0/linux-x64/native/silkSpecular.png +cd ./bin/Debug/net5.0/linux-x64/native/ ./CoreRTTest \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Linux64_Release.sh b/src/Lab/CoreRTTest/RunTest_Linux64_Release.sh new file mode 100644 index 0000000000..03a79fd691 --- /dev/null +++ b/src/Lab/CoreRTTest/RunTest_Linux64_Release.sh @@ -0,0 +1,10 @@ +#!/bin/sh +dotnet publish -r linux-x64 -c Release +cp ./bin/Release/net5.0/linux-x64/libglfw.so.3 ./bin/Release/net5.0/linux-x64/native/libglfw.so.3 +cp ./bin/Release/net5.0/linux-x64/lighting.frag ./bin/Release/net5.0/linux-x64/native/lighting.frag +cp ./bin/Release/net5.0/linux-x64/shader.frag ./bin/Release/net5.0/linux-x64/native/shader.frag +cp ./bin/Release/net5.0/linux-x64/shader.vert ./bin/Release/net5.0/linux-x64/native/shader.vert +cp ./bin/Release/net5.0/linux-x64/silkBoxed.png ./bin/Release/net5.0/linux-x64/native/silkBoxed.png +cp ./bin/Release/net5.0/linux-x64/silkSpecular.png ./bin/Release/net5.0/linux-x64/native/silkSpecular.png +cd ./bin/Release/net5.0/linux-x64/native/ +./CoreRTTest \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Mac64_Debug.sh b/src/Lab/CoreRTTest/RunTest_Mac64_Debug.sh index 7d7563a818..142007839f 100644 --- a/src/Lab/CoreRTTest/RunTest_Mac64_Debug.sh +++ b/src/Lab/CoreRTTest/RunTest_Mac64_Debug.sh @@ -1,5 +1,10 @@ #!/bin/sh dotnet publish -r osx-x64 -c Debug -cp ./bin/Debug/netcoreapp3.0/osx-x64/libglfw.3.dylib ./bin/Debug/netcoreapp3.0/osx-x64/native/libglfw.3.dylib -cd ./bin/Debug/netcoreapp3.0/osx-x64/native/ +cp ./bin/Debug/net5.0/osx-x64/libglfw.3.dylib ./bin/Debug/net5.0/osx-x64/native/libglfw.3.dylib +cp ./bin/Debug/net5.0/osx-x64/lighting.frag ./bin/Debug/net5.0/osx-x64/native/lighting.frag +cp ./bin/Debug/net5.0/osx-x64/shader.frag ./bin/Debug/net5.0/osx-x64/native/shader.frag +cp ./bin/Debug/net5.0/osx-x64/shader.vert ./bin/Debug/net5.0/osx-x64/native/shader.vert +cp ./bin/Debug/net5.0/osx-x64/silkBoxed.png ./bin/Debug/net5.0/osx-x64/native/silkBoxed.png +cp ./bin/Debug/net5.0/osx-x64/silkSpecular.png ./bin/Debug/net5.0/osx-x64/native/silkSpecular.png +cd ./bin/Debug/net5.0/osx-x64/native/ ./CoreRTTest \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Mac64_Release.sh b/src/Lab/CoreRTTest/RunTest_Mac64_Release.sh new file mode 100644 index 0000000000..5d5400a7a5 --- /dev/null +++ b/src/Lab/CoreRTTest/RunTest_Mac64_Release.sh @@ -0,0 +1,10 @@ +#!/bin/sh +dotnet publish -r osx-x64 -c Release +cp ./bin/Release/net5.0/osx-x64/libglfw.3.dylib ./bin/Release/net5.0/osx-x64/native/libglfw.3.dylib +cp ./bin/Release/net5.0/osx-x64/lighting.frag ./bin/Release/net5.0/osx-x64/native/lighting.frag +cp ./bin/Release/net5.0/osx-x64/shader.frag ./bin/Release/net5.0/osx-x64/native/shader.frag +cp ./bin/Release/net5.0/osx-x64/shader.vert ./bin/Release/net5.0/osx-x64/native/shader.vert +cp ./bin/Release/net5.0/osx-x64/silkBoxed.png ./bin/Release/net5.0/osx-x64/native/silkBoxed.png +cp ./bin/Release/net5.0/osx-x64/silkSpecular.png ./bin/Release/net5.0/osx-x64/native/silkSpecular.png +cd ./bin/Release/net5.0/osx-x64/native/ +./CoreRTTest \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Win64_Debug.cmd b/src/Lab/CoreRTTest/RunTest_Win64_Debug.cmd index 1c630ee25a..6967927b93 100644 --- a/src/Lab/CoreRTTest/RunTest_Win64_Debug.cmd +++ b/src/Lab/CoreRTTest/RunTest_Win64_Debug.cmd @@ -1,5 +1,11 @@ dotnet publish -r win-x64 -c Debug -copy .\bin\Debug\netcoreapp3.0\win-x64\glfw3.dll .\bin\Debug\netcoreapp3.0\win-x64\native\glfw3.dll -cd .\bin\Debug\netcoreapp3.0\win-x64\native\ +copy .\bin\Debug\net5.0\win-x64\glfw3.dll .\bin\Debug\net5.0\win-x64\native\glfw3.dll +copy .\bin\Release\net5.0\win-x64\sdl2.dll .\bin\Release\net5.0\win-x64\native\sdl2.dll +copy .\bin\Debug\net5.0\win-x64\lighting.frag .\bin\Debug\net5.0\win-x64\native\lighting.frag +copy .\bin\Debug\net5.0\win-x64\shader.frag .\bin\Debug\net5.0\win-x64\native\shader.frag +copy .\bin\Debug\net5.0\win-x64\shader.vert .\bin\Debug\net5.0\win-x64\native\shader.vert +copy .\bin\Debug\net5.0\win-x64\silkBoxed.png .\bin\Debug\net5.0\win-x64\native\silkBoxed.png +copy .\bin\Debug\net5.0\win-x64\silkSpecular.png .\bin\Debug\net5.0\win-x64\native\silkSpecular.png +cd .\bin\Debug\net5.0\win-x64\native\ .\CoreRTTest pause \ No newline at end of file diff --git a/src/Lab/CoreRTTest/RunTest_Win64_Release.cmd b/src/Lab/CoreRTTest/RunTest_Win64_Release.cmd new file mode 100644 index 0000000000..054764fd08 --- /dev/null +++ b/src/Lab/CoreRTTest/RunTest_Win64_Release.cmd @@ -0,0 +1,11 @@ +dotnet publish -r win-x64 -c Release +copy .\bin\Release\net5.0\win-x64\glfw3.dll .\bin\Release\net5.0\win-x64\native\glfw3.dll +copy .\bin\Release\net5.0\win-x64\sdl2.dll .\bin\Release\net5.0\win-x64\native\sdl2.dll +copy .\bin\Release\net5.0\win-x64\lighting.frag .\bin\Release\net5.0\win-x64\native\lighting.frag +copy .\bin\Release\net5.0\win-x64\shader.frag .\bin\Release\net5.0\win-x64\native\shader.frag +copy .\bin\Release\net5.0\win-x64\shader.vert .\bin\Release\net5.0\win-x64\native\shader.vert +copy .\bin\Release\net5.0\win-x64\silkBoxed.png .\bin\Release\net5.0\win-x64\native\silkBoxed.png +copy .\bin\Release\net5.0\win-x64\silkSpecular.png .\bin\Release\net5.0\win-x64\native\silkSpecular.png +cd .\bin\Release\net5.0\win-x64\native\ +.\CoreRTTest +pause \ No newline at end of file diff --git a/src/Lab/CoreRTTest/Shader.cs b/src/Lab/CoreRTTest/Shader.cs new file mode 100644 index 0000000000..601ba12c3c --- /dev/null +++ b/src/Lab/CoreRTTest/Shader.cs @@ -0,0 +1,104 @@ +using System; +using System.IO; +using System.Numerics; +using Silk.NET.OpenGL; + +namespace Tutorial +{ + public class Shader : IDisposable + { + private uint _handle; + private GL _gl; + + public Shader(GL gl, string vertexPath, string fragmentPath) + { + _gl = gl; + + uint vertex = LoadShader(ShaderType.VertexShader, vertexPath); + uint fragment = LoadShader(ShaderType.FragmentShader, fragmentPath); + _handle = _gl.CreateProgram(); + _gl.AttachShader(_handle, vertex); + _gl.AttachShader(_handle, fragment); + _gl.LinkProgram(_handle); + _gl.GetProgram(_handle, GLEnum.LinkStatus, out var status); + if (status == 0) + { + throw new Exception($"Program failed to link with error: {_gl.GetProgramInfoLog(_handle)}"); + } + _gl.DetachShader(_handle, vertex); + _gl.DetachShader(_handle, fragment); + _gl.DeleteShader(vertex); + _gl.DeleteShader(fragment); + } + + public void Use() + { + _gl.UseProgram(_handle); + } + + public void SetUniform(string name, int value) + { + int location = _gl.GetUniformLocation(_handle, name); + if (location == -1) + { + throw new Exception($"{name} uniform not found on shader."); + } + Use(); + _gl.Uniform1(location, value); + } + + public unsafe void SetUniform(string name, Matrix4x4 value) + { + //A new overload has been created for setting a uniform so we can use the transform in our shader. + int location = _gl.GetUniformLocation(_handle, name); + if (location == -1) + { + throw new Exception($"{name} uniform not found on shader."); + } + Use(); + _gl.UniformMatrix4(location, 1, false, (float*)&value); + } + + public void SetUniform(string name, float value) + { + int location = _gl.GetUniformLocation(_handle, name); + if (location == -1) + { + throw new Exception($"{name} uniform not found on shader."); + } + Use(); + _gl.Uniform1(location, value); + } + + public void SetUniform(string name, Vector3 value) + { + int location = _gl.GetUniformLocation(_handle, name); + if (location == -1) + { + throw new Exception($"{name} uniform not found on shader."); + } + Use(); + _gl.Uniform3(location, value.X, value.Y, value.Z); + } + + public void Dispose() + { + _gl.DeleteProgram(_handle); + } + + private uint LoadShader(ShaderType type, string path) + { + string src = File.ReadAllText(path); + uint handle = _gl.CreateShader(type); + _gl.ShaderSource(handle, src); + _gl.CompileShader(handle); + string infoLog = _gl.GetShaderInfoLog(handle); + if (!string.IsNullOrWhiteSpace(infoLog)) + { + throw new Exception($"Error compiling shader of type {type}, failed with error {infoLog}"); + } + + return handle; + } + } +} diff --git a/src/Lab/CoreRTTest/Texture.cs b/src/Lab/CoreRTTest/Texture.cs new file mode 100644 index 0000000000..3e280e95c4 --- /dev/null +++ b/src/Lab/CoreRTTest/Texture.cs @@ -0,0 +1,62 @@ +using Silk.NET.OpenGL; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using System; +using System.Runtime.InteropServices; + +namespace Tutorial +{ + public class Texture : IDisposable + { + private uint _handle; + private GL _gl; + + public unsafe Texture(GL gl, string path) + { + Image img = (Image)Image.Load(path); + img.Mutate(x => x.Flip(FlipMode.Vertical)); + + fixed (void* data = &MemoryMarshal.GetReference(img.GetPixelRowSpan(0))) + { + Load(gl, data, (uint)img.Width, (uint)img.Height); + } + + img.Dispose(); + } + + public unsafe Texture(GL gl, Span data, uint width, uint height) + { + fixed (void* d = &data[0]) + { + Load(gl, d, width, height); + } + } + + private unsafe void Load(GL gl, void* data, uint width, uint height) + { + _gl = gl; + + _handle = _gl.GenTexture(); + Bind(); + + _gl.TexImage2D(TextureTarget.Texture2D, 0, (int)InternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data); + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GLEnum.ClampToEdge); + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GLEnum.ClampToEdge); + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)GLEnum.Linear); + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)GLEnum.Linear); + _gl.GenerateMipmap(TextureTarget.Texture2D); + } + + public void Bind(TextureUnit textureSlot = TextureUnit.Texture0) + { + _gl.ActiveTexture(textureSlot); + _gl.BindTexture(TextureTarget.Texture2D, _handle); + } + + public void Dispose() + { + _gl.DeleteTexture(_handle); + } + } +} diff --git a/src/Lab/CoreRTTest/Transform.cs b/src/Lab/CoreRTTest/Transform.cs new file mode 100644 index 0000000000..0cb3caea40 --- /dev/null +++ b/src/Lab/CoreRTTest/Transform.cs @@ -0,0 +1,24 @@ +using System.Numerics; + +namespace Tutorial +{ + public class Transform + { + //A transform abstraction. + //For a transform we need to have a position a scale and a rotation, + //depending on what application you are creating the type for these may vary. + + //Here we have chosen a vec3 for position, float for scale and quaternion for rotation, + //as that is the most normal to go with. + //Another example could have been vec3, vec3, vec4, so the rotation is an axis angle instead of a quaternion + + public Vector3 Position { get; set; } = new Vector3(0, 0, 0); + + public float Scale { get; set; } = 1f; + + public Quaternion Rotation { get; set; } = Quaternion.Identity; + + //Note: The order here does matter. + public Matrix4x4 ViewMatrix => Matrix4x4.Identity * Matrix4x4.CreateFromQuaternion(Rotation) * Matrix4x4.CreateScale(Scale) * Matrix4x4.CreateTranslation(Position); + } +} diff --git a/src/Lab/CoreRTTest/VertexArrayObject.cs b/src/Lab/CoreRTTest/VertexArrayObject.cs new file mode 100644 index 0000000000..069ee5110a --- /dev/null +++ b/src/Lab/CoreRTTest/VertexArrayObject.cs @@ -0,0 +1,39 @@ +using Silk.NET.OpenGL; +using System; + +namespace Tutorial +{ + public class VertexArrayObject : IDisposable + where TVertexType : unmanaged + where TIndexType : unmanaged + { + private uint _handle; + private GL _gl; + + public VertexArrayObject(GL gl, BufferObject vbo, BufferObject ebo) + { + _gl = gl; + + _handle = _gl.GenVertexArray(); + Bind(); + vbo.Bind(); + ebo.Bind(); + } + + public unsafe void VertexAttributePointer(uint index, int count, VertexAttribPointerType type, uint vertexSize, int offSet) + { + _gl.VertexAttribPointer(index, count, type, false, vertexSize * (uint)sizeof(TVertexType), (void*)(offSet * sizeof(TVertexType))); + _gl.EnableVertexAttribArray(index); + } + + public void Bind() + { + _gl.BindVertexArray(_handle); + } + + public void Dispose() + { + _gl.DeleteVertexArray(_handle); + } + } +} diff --git a/src/Lab/CoreRTTest/lighting.frag b/src/Lab/CoreRTTest/lighting.frag new file mode 100644 index 0000000000..f6ee6a3d5d --- /dev/null +++ b/src/Lab/CoreRTTest/lighting.frag @@ -0,0 +1,42 @@ +#version 330 core +in vec3 fNormal; +in vec3 fPos; +in vec2 fTexCoords; + +struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct Light { + vec3 position; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +uniform Material material; +uniform Light light; +uniform vec3 viewPos; + +out vec4 FragColor; + +void main() +{ + vec3 ambient = light.ambient * texture(material.diffuse, fTexCoords).rgb; + + vec3 norm = normalize(fNormal); + vec3 lightDirection = normalize(light.position - fPos); + float diff = max(dot(norm, lightDirection), 0.0); + vec3 diffuse = light.diffuse * (diff * texture(material.diffuse, fTexCoords).rgb); + + vec3 viewDirection = normalize(viewPos - fPos); + vec3 reflectDirection = reflect(-lightDirection, norm); + float spec = pow(max(dot(viewDirection, reflectDirection), 0.0), material.shininess); + vec3 specular = light.specular * (spec * texture(material.specular, fTexCoords).rgb); + + //The resulting colour should be the amount of ambient colour + the amount of additional colour provided by the diffuse of the lamp + the specular amount + vec3 result = ambient + diffuse + specular; + FragColor = vec4(result, 1.0); +} \ No newline at end of file diff --git a/src/Lab/CoreRTTest/shader.frag b/src/Lab/CoreRTTest/shader.frag new file mode 100644 index 0000000000..0b99ca4e99 --- /dev/null +++ b/src/Lab/CoreRTTest/shader.frag @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0f); +} \ No newline at end of file diff --git a/src/Lab/CoreRTTest/shader.vert b/src/Lab/CoreRTTest/shader.vert new file mode 100644 index 0000000000..cc7c4b9ad8 --- /dev/null +++ b/src/Lab/CoreRTTest/shader.vert @@ -0,0 +1,24 @@ +#version 330 core +layout (location = 0) in vec3 vPos; +layout (location = 1) in vec3 vNormal; +layout (location = 2) in vec2 vTexCoords; + +uniform mat4 uModel; +uniform mat4 uView; +uniform mat4 uProjection; + +out vec3 fNormal; +out vec3 fPos; +out vec2 fTexCoords; + +void main() +{ + //Multiplying our uniform with the vertex position, the multiplication order here does matter. + gl_Position = uProjection * uView * uModel * vec4(vPos, 1.0); + //We want to know the fragments position in World space, so we multiply ONLY by uModel and not uView or uProjection + fPos = vec3(uModel * vec4(vPos, 1.0)); + //The Normal needs to be in World space too, but needs to account for Scaling of the object + fNormal = mat3(transpose(inverse(uModel))) * vPos; + //Pass the texture coordinates straight through to the fragment shader + fTexCoords = vTexCoords; +} \ No newline at end of file diff --git a/src/Lab/CoreRTTest/silkBoxed.png b/src/Lab/CoreRTTest/silkBoxed.png new file mode 100644 index 0000000000..6da772b0be Binary files /dev/null and b/src/Lab/CoreRTTest/silkBoxed.png differ diff --git a/src/Lab/CoreRTTest/silkSpecular.png b/src/Lab/CoreRTTest/silkSpecular.png new file mode 100644 index 0000000000..6a5a415ee4 Binary files /dev/null and b/src/Lab/CoreRTTest/silkSpecular.png differ diff --git a/src/Lab/InputTest/InputTest.csproj b/src/Lab/InputTest/InputTest.csproj index 8ea9cf716a..64cc9bcfe5 100644 --- a/src/Lab/InputTest/InputTest.csproj +++ b/src/Lab/InputTest/InputTest.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3 + netcoreapp3.1 true win-x64 diff --git a/src/Lab/MonitorPlayground/MonitorPlayground.csproj b/src/Lab/MonitorPlayground/MonitorPlayground.csproj index c5bbd224cd..5d9277d7d1 100644 --- a/src/Lab/MonitorPlayground/MonitorPlayground.csproj +++ b/src/Lab/MonitorPlayground/MonitorPlayground.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 diff --git a/src/Lab/SuperInvokeDiagnostics/SuperInvokeDiagnostics.csproj b/src/Lab/SuperInvokeDiagnostics/SuperInvokeDiagnostics.csproj index 968728a141..5e5b6085bb 100644 --- a/src/Lab/SuperInvokeDiagnostics/SuperInvokeDiagnostics.csproj +++ b/src/Lab/SuperInvokeDiagnostics/SuperInvokeDiagnostics.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3 + netcoreapp3.1 diff --git a/src/Lab/VulkanTriangle/VulkanTriangle.csproj b/src/Lab/VulkanTriangle/VulkanTriangle.csproj index 9e39bfd67a..b5be1759f8 100644 --- a/src/Lab/VulkanTriangle/VulkanTriangle.csproj +++ b/src/Lab/VulkanTriangle/VulkanTriangle.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.0 + netcoreapp3.1 true diff --git a/src/Windowing/Silk.NET.Windowing.Common/Structs/WindowOptions.cs b/src/Windowing/Silk.NET.Windowing.Common/Structs/WindowOptions.cs index 42fab47a70..74b2d7432b 100644 --- a/src/Windowing/Silk.NET.Windowing.Common/Structs/WindowOptions.cs +++ b/src/Windowing/Silk.NET.Windowing.Common/Structs/WindowOptions.cs @@ -3,7 +3,9 @@ // You may modify and distribute Silk.NET under the terms // of the MIT license. See the LICENSE file for details. +using System; using System.Drawing; +using System.IO; using System.Reflection; namespace Silk.NET.Windowing @@ -120,26 +122,40 @@ public WindowOptions VSync = isVSync; } + static WindowOptions() + { + string name = "Silk.NET Window"; + try + { + var asmName = Assembly.GetEntryAssembly()?.GetName().Name; + if (asmName is not null) + name = asmName; + } + catch { /* cannot use reflection */ } + + Default = new WindowOptions + ( + true, new Point(50, 50), new Size(1280, 720), 0.0, 0.0, GraphicsAPI.Default, + name, WindowState.Normal, + WindowBorder.Resizable, true, true, VideoMode.Default + ); + + DefaultVulkan = new WindowOptions + ( + true, new Point(50, 50), new Size(1280, 720), 0.0, 0.0, GraphicsAPI.DefaultVulkan, + name, WindowState.Normal, + WindowBorder.Resizable, false, false, VideoMode.Default + ); + } + /// /// Convenience wrapper around creating a new WindowProperties with sensible defaults. /// - public static WindowOptions Default { get; } = new WindowOptions - ( - true, new Point(50, 50), - new Size(1280, 720), 0.0, 0.0, GraphicsAPI.Default, - Assembly.GetEntryAssembly()?.GetName().Name ?? "Silk.NET Window", WindowState.Normal, - WindowBorder.Resizable, true, true, VideoMode.Default - ); + public static WindowOptions Default { get; } /// /// Convenience wrapper around creating a new WindowProperties with sensible values, intended for use with Vulkan. /// - public static WindowOptions DefaultVulkan { get; } = new WindowOptions - ( - true, new Point(50, 50), - new Size(1280, 720), 0.0, 0.0, GraphicsAPI.DefaultVulkan, - Assembly.GetEntryAssembly()?.GetName().Name ?? "Silk.NET Window", WindowState.Normal, - WindowBorder.Resizable, false, false, VideoMode.Default - ); + public static WindowOptions DefaultVulkan { get; } } } diff --git a/src/Windowing/Silk.NET.Windowing.Common/Window.cs b/src/Windowing/Silk.NET.Windowing.Common/Window.cs index 79fb36ad32..ef43cfb3c5 100644 --- a/src/Windowing/Silk.NET.Windowing.Common/Window.cs +++ b/src/Windowing/Silk.NET.Windowing.Common/Window.cs @@ -20,21 +20,45 @@ public static class Window private const string SdlBackendNamespace = "Silk.NET.Windowing.Sdl"; private const string GlfwBackendName = "GlfwPlatform"; private const string SdlBackendName = "SdlPlatform"; - public static IReadOnlyList Platforms { get; } = new List(); - internal static Exception NoPlatformException => new PlatformNotSupportedException - ( - "Couldn't find a suitable window platform. " + - "https://docs.ultz.co.uk/Silk.NET/windowing/troubleshooting.html" - ); + private static List _platforms = new List(); + private static bool _initializedFirstPartyPlatforms = false; + + public static IReadOnlyList Platforms + { + get + { + if (!_initializedFirstPartyPlatforms) + { + DoLoadFirstPartyPlatformsViaReflection(); + _initializedFirstPartyPlatforms = true; + } + + return _platforms; + } + } + + public static void ShouldLoadFirstPartyPlatforms(bool shouldLoad) + { + if (_initializedFirstPartyPlatforms) + throw new InvalidOperationException("Window Platforms already loaded, cannot change first party loading"); - static Window() + _initializedFirstPartyPlatforms = !shouldLoad; + } + + private static void DoLoadFirstPartyPlatformsViaReflection() { // Try add the first-party backends TryAdd(GlfwBackendNamespace); TryAdd(SdlBackendNamespace); } + internal static Exception NoPlatformException => new PlatformNotSupportedException + ( + "Couldn't find a suitable window platform. " + + "https://docs.ultz.co.uk/Silk.NET/windowing/troubleshooting.html" + ); + /// /// Gets the first platform registered that is applicable and isn't view-only. /// @@ -147,8 +171,8 @@ public static void ClearCurrentContexts(IView view) /// public static void Prioritize(IWindowPlatform platform) { - ((List) Platforms).Remove(platform); - ((List) Platforms).Insert(0, platform); + _platforms.Remove(platform); + _platforms.Insert(0, platform); } /// @@ -189,9 +213,9 @@ public static void PrioritizeSdl() /// The platform to add. public static void Add(IWindowPlatform platform) { - if (!((List) Platforms).Contains(platform)) + if (!_platforms.Contains(platform)) { - ((List) Platforms).Add(platform); + _platforms.Add(platform); } } @@ -199,7 +223,7 @@ public static void Add(IWindowPlatform platform) /// Removes this window platform from the platform list. Shouldn't be used unless writing your own windowing backend. /// /// The platform to remove. - public static void Remove(IWindowPlatform platform) => ((List) Platforms).Remove(platform); + public static void Remove(IWindowPlatform platform) => _platforms.Remove(platform); /// /// Attempts to load the given assembly by name, checks for a , if one is found it diff --git a/src/Windowing/Silk.NET.Windowing.Glfw/GlfwPlatform.cs b/src/Windowing/Silk.NET.Windowing.Glfw/GlfwPlatform.cs index 9e3375918b..d9e560067e 100644 --- a/src/Windowing/Silk.NET.Windowing.Glfw/GlfwPlatform.cs +++ b/src/Windowing/Silk.NET.Windowing.Glfw/GlfwPlatform.cs @@ -3,6 +3,7 @@ // You may modify and distribute Silk.NET under the terms // of the MIT license. See the LICENSE file for details. +using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using Silk.NET.GLFW; @@ -30,8 +31,11 @@ public bool IsApplicable { GLFW.Glfw.GetApi(); // activate the class so we can determine if we can activate the class } - catch + catch (Exception ex) { +#if DEBUG + Console.WriteLine($"Can't load GLFW: {ex}"); +#endif return false; } diff --git a/src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindowing.cs b/src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindowing.cs new file mode 100644 index 0000000000..5b90d5adec --- /dev/null +++ b/src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindowing.cs @@ -0,0 +1,15 @@ +// This file is part of Silk.NET. +// +// You may modify and distribute Silk.NET under the terms +// of the MIT license. See the LICENSE file for details. + +namespace Silk.NET.Windowing.Glfw +{ + public static class GlfwWindowing + { + public static void RegisterPlatform() + { + Window.Add(new GlfwPlatform()); + } + } +} diff --git a/src/Windowing/Silk.NET.Windowing.Sdl/SdlPlatform.cs b/src/Windowing/Silk.NET.Windowing.Sdl/SdlPlatform.cs index 8211ff18c7..a16f09f0bd 100644 --- a/src/Windowing/Silk.NET.Windowing.Sdl/SdlPlatform.cs +++ b/src/Windowing/Silk.NET.Windowing.Sdl/SdlPlatform.cs @@ -26,8 +26,11 @@ internal class SdlPlatform : IWindowPlatform { SDL.Sdl.GetApi(); } - catch + catch (Exception ex) { +#if DEBUG + Console.WriteLine($"Can't load SDL: {ex}"); +#endif return false; } diff --git a/src/Windowing/Silk.NET.Windowing.Sdl/SdlWindowing.cs b/src/Windowing/Silk.NET.Windowing.Sdl/SdlWindowing.cs new file mode 100644 index 0000000000..0fec674254 --- /dev/null +++ b/src/Windowing/Silk.NET.Windowing.Sdl/SdlWindowing.cs @@ -0,0 +1,15 @@ +// This file is part of Silk.NET. +// +// You may modify and distribute Silk.NET under the terms +// of the MIT license. See the LICENSE file for details. + +namespace Silk.NET.Windowing.Sdl +{ + public static class SdlWindowing + { + public static void RegisterPlatform() + { + Window.Add(new SdlPlatform()); + } + } +}