From 71430c7793d7d9d6f295d714b6f676d8b7f7d464 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 31 Aug 2025 13:31:05 +0200 Subject: [PATCH] added shader, finished perspective camera and enabled keyboard support in the input class --- .../EngineSharp.Core/EngineSharp.Core.csproj | 1 + .../EngineSharp.Core/Input.cs | 11 ++ .../EngineSharp.Core/OpenGLEngine.cs | 21 +++- .../EngineSharp.Core/PerspectiveCamera.cs | 78 ++++++++++--- .../Exceptions/ShaderCompileException.cs | 17 +++ .../Exceptions/ShaderLinkException.cs | 8 ++ .../EngineSharp.Core/Rendering/Shader.cs | 109 ++++++++++++++++++ 7 files changed, 227 insertions(+), 18 deletions(-) create mode 100644 src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderCompileException.cs create mode 100644 src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderLinkException.cs create mode 100644 src/EngineSharp.Core/EngineSharp.Core/Rendering/Shader.cs diff --git a/src/EngineSharp.Core/EngineSharp.Core/EngineSharp.Core.csproj b/src/EngineSharp.Core/EngineSharp.Core/EngineSharp.Core.csproj index c81548c..e3c652e 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/EngineSharp.Core.csproj +++ b/src/EngineSharp.Core/EngineSharp.Core/EngineSharp.Core.csproj @@ -4,6 +4,7 @@ net9.0 enable enable + true diff --git a/src/EngineSharp.Core/EngineSharp.Core/Input.cs b/src/EngineSharp.Core/EngineSharp.Core/Input.cs index f27433c..db315f1 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/Input.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/Input.cs @@ -11,6 +11,12 @@ public class Input internal Input(IInputContext inputContext) { _context = inputContext; + foreach (var keyboard in _context.Keyboards) + { + keyboard.KeyDown += OnKeyDown; + keyboard.KeyUp += OnKeyUp; + } + _context.ConnectionChanged += OnDeviceConnectionChanges; } /// @@ -49,4 +55,9 @@ public class Input existingSet.Remove(key); } } + + private void OnDeviceConnectionChanges(IInputDevice device, bool connected) + { + // TODO: See how to best utilise this. probably type pattern matching for IKeyboard etc. + } } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs index 16ddc2f..58ae652 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs @@ -9,8 +9,9 @@ namespace EngineSharp.Core; internal class OpenGLEngine : IEngine { private readonly IWindow _window; - private GL _gl = null!; // because between constructing the engine and OnLoad being called nothing should be able to happen, we do this to get rid of warnings + private GL _gl = null!; // because between constructing the engine and OnLoad being called nothing should be able to call these fields. Therefore, we do this to get rid of warnings private Input _input = null!; + private PerspectiveCamera _camera = null!; public OpenGLEngine(WindowOptions windowOptions) { @@ -38,20 +39,32 @@ internal class OpenGLEngine : IEngine _gl.ClearColor(Color.Black); _gl.Enable(EnableCap.DepthTest); + + _camera = new PerspectiveCamera(Vector3D.Zero, _window.FramebufferSize); } private void OnUpdate(double deltaTime) { - // TODO: from here call custom code / game logic + if (_input.IsKeyPressed(Key.Escape)) + { + _window.Close(); + } + // TODO: here call custom code / game logic } private void OnRender(double deltaTime) { + var projectionMatrix = _camera.ProjectionMatrix; + var viewMatrix = _camera.ViewMatrix; + // TODO: Here render all meshes etc. + // On constructing a MeshRenderer, register it somewhere so it can be rendered here (MeshRenderer would then be a component while Mesh would be a normal C# object in the context of a ECS) + // MeshRenderer contains a mesh; Mesh contains a material; Material contains a shader and the values for all uniforms of the shader (apart from the matrices; I am not sure how to best handle the model matrix with this approach) } - private void OnResize(Vector2D obj) + private void OnResize(Vector2D newDimensions) { - // TODO: update aspect of camera; on entity component system the camera should probably read the aspect ratio from some location rather than the engine updating the aspect on the camera + // when using an entity component system the camera should probably read the aspect ratio from a central location or should listen to the "Resize" event rather than the engine updating the aspect on the camera + _camera.UpdateAspect(newDimensions); } } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/PerspectiveCamera.cs b/src/EngineSharp.Core/EngineSharp.Core/PerspectiveCamera.cs index 2463d2d..601c57b 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/PerspectiveCamera.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/PerspectiveCamera.cs @@ -1,38 +1,86 @@ -// using System.Numerics; -using Silk.NET.Maths; +using Silk.NET.Maths; namespace EngineSharp.Core; public class PerspectiveCamera { - private readonly Vector3D _position; private readonly Vector3D _worldUp; + private Vector3D _position; private Matrix3X3 _intrinsicCoordinates; // right | up | front private float _yaw; private float _pitch; - private float _fov; - private float _nearClippingPlane; - private float _farClippingPlane; + private float _aspectRatio; + private bool _updateVectors; - public Matrix4X4 ViewMatrix => Matrix4X4.CreateLookAt(_position, _position + _intrinsicCoordinates.Column3, _worldUp); // TODO: I hope this is Left-Handed. If not I need to update the readme - public Matrix4X4 ProjectionMatrix => Matrix4X4.CreatePerspectiveFieldOfView(_fov, , _nearClippingPlane, _farClippingPlane); + public float Fov { get; set; } + public float NearClippingPlane { get; set; } + public float FarClippingPlane { get; set; } + + public float MovementSpeed { get; set; } = 2.5f; + + public float Yaw + { + get => _yaw; + set + { + _yaw = value; + _updateVectors = true; + } + } + public float Pitch + { + get => _pitch; + set + { + _pitch = value; + _updateVectors = true; + } + } - public PerspectiveCamera(Vector3D position, float yaw = -90.0f, float pitch = 0, float fov = 45.0f, float nearClippingPlane = 0.1f, float farClippingPlane = 100.0f) + public Matrix4X4 ViewMatrix + { + get + { + if (_updateVectors) + { + UpdateCameraVectors(); + } + return Matrix4X4.CreateLookAt(_position, _position + _intrinsicCoordinates.Column3, _worldUp); // TODO: I hope this is Left-Handed. If not I need to update the readme + } + } + public Matrix4X4 ProjectionMatrix => Matrix4X4.CreatePerspectiveFieldOfView(Fov, _aspectRatio, NearClippingPlane, FarClippingPlane); + + public PerspectiveCamera(Vector3D position, Vector2D viewportDimensions, float yaw = -90.0f, float pitch = 0, float fov = 45.0f, float nearClippingPlane = 0.1f, float farClippingPlane = 100.0f) { _position = position; _yaw = yaw; _pitch = pitch; - _fov = fov; - _nearClippingPlane = nearClippingPlane; - _farClippingPlane = farClippingPlane; + Fov = fov; + NearClippingPlane = nearClippingPlane; + FarClippingPlane = farClippingPlane; _worldUp = Vector3D.UnitY; + UpdateCameraVectors(); + UpdateAspect(viewportDimensions); + } + + public void ProcessMovement(float deltaTime, Vector3D movementDirection) { + var velocity = MovementSpeed * deltaTime; // velocity not really the right term, but oh well + _position += (movementDirection * velocity) * _intrinsicCoordinates; + } + public void ProcessMouse(Vector2D offset) { + _yaw += offset.X; + _pitch += offset.Y; + + if (_pitch > 89.0) {_pitch = 89.0f;} + if (_pitch < -89.0) {_pitch = -89.0f;} + UpdateCameraVectors(); } - internal void UpdateAspect() + internal void UpdateAspect(Vector2D newDimensions) { - + _aspectRatio = (float)newDimensions.X / newDimensions.Y; } private void UpdateCameraVectors() @@ -50,5 +98,7 @@ public class PerspectiveCamera right.Y, up.Y, front.Y, right.Z, up.Z, front.Z ); + + _updateVectors = false; } } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderCompileException.cs b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderCompileException.cs new file mode 100644 index 0000000..d80047f --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderCompileException.cs @@ -0,0 +1,17 @@ +using Silk.NET.OpenGL; + +namespace EngineSharp.Core.Rendering.Exceptions; + +public class ShaderCompileException : Exception +{ + protected readonly ShaderType ShaderType; + + public ShaderCompileException(ShaderType shaderType) : base("Unable to compile shader.") { ShaderType = shaderType; } + public ShaderCompileException(ShaderType shaderType, string message) : base(message) { ShaderType = shaderType; } + public ShaderCompileException(ShaderType shaderType, string message, Exception inner) : base(message, inner) { ShaderType = shaderType; } + + public override string ToString() + { + return $"Type of Shader: '{ShaderType}'" + "\n" + Message; + } +} diff --git a/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderLinkException.cs b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderLinkException.cs new file mode 100644 index 0000000..18b7e6f --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Exceptions/ShaderLinkException.cs @@ -0,0 +1,8 @@ +namespace EngineSharp.Core.Rendering.Exceptions; + +public class ShaderLinkException : Exception +{ + public ShaderLinkException() : base("Error occured while trying to link a shader.") { } + public ShaderLinkException(string message) : base(message) { } + public ShaderLinkException(string message, Exception inner) : base(message, inner) { } +} \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/Rendering/Shader.cs b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Shader.cs new file mode 100644 index 0000000..ded7465 --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/Rendering/Shader.cs @@ -0,0 +1,109 @@ +using EngineSharp.Core.Rendering.Exceptions; +using Silk.NET.Maths; +using Silk.NET.OpenGL; + +namespace EngineSharp.Core.Rendering; + +public class Shader +{ + private readonly GL _gl; + private readonly uint _shaderProgramId; + + public Shader(GL openGLContext, string pathToVertexShader, string pathToFragmentShader) + { + _gl = openGLContext; + var vertexCode = File.ReadAllText(pathToVertexShader); + var fragmentCode = File.ReadAllText(pathToFragmentShader); + + var vertexShader = CompileShader(vertexCode, ShaderType.VertexShader); + var fragmentShader = CompileShader(fragmentCode, ShaderType.FragmentShader); + + _shaderProgramId = CreateProgram(vertexShader, fragmentShader); + } + + public void Use() => _gl.UseProgram(_shaderProgramId); + + #region Set uniforms + public void SetInt(string name, int value) + { + _gl.Uniform1(_gl.GetUniformLocation(_shaderProgramId, name), value); + } + + public void SetFloat(string name, float value) + { + _gl.Uniform1(_gl.GetUniformLocation(_shaderProgramId, name), value); + } + + public void SetVector(string name, Vector3D value) + { + _gl.Uniform3(_gl.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z); + } + public void SetVector(string name, Vector3D value) + { + _gl.Uniform3(_gl.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z); + } + + public void SetMatrix(string name, Matrix4X4 matrix) + { + unsafe + { + _gl.UniformMatrix4(_gl.GetUniformLocation(_shaderProgramId, name), 1, false, (double*) &matrix); + } + } + public void SetMatrix(string name, Matrix4X4 matrix) + { + unsafe + { + _gl.UniformMatrix4(_gl.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix); + } + } + #endregion + + + /// + /// Compiles the given shadercode. + /// + /// The shadercode + /// The type of shader to compile + /// Returns the id of the compiled shader. + /// + private uint CompileShader(string shaderCode, ShaderType shaderType) + { + var shader = _gl.CreateShader(shaderType); + _gl.ShaderSource(shader, shaderCode); + _gl.CompileShader(shader); + + _gl.GetShader(shader, ShaderParameterName.CompileStatus, out var status); + + if (status != (int)GLEnum.True) + { + throw new ShaderCompileException(shaderType, $"Failed to compile shader with message: \n {_gl.GetShaderInfoLog(shader)}"); + } + + return shader; + } + + /// + /// Creates a shader program and links the vertex and fragment shader together. + /// + /// Returns the id of the created shader program + /// + private uint CreateProgram(uint vertexShader, uint fragmentShader) + { + var program = _gl.CreateProgram(); + _gl.AttachShader(program, vertexShader); + _gl.AttachShader(program, fragmentShader); + _gl.LinkProgram(program); + + _gl.GetProgram(program, ProgramPropertyARB.LinkStatus, out var status); + if (status != (int)GLEnum.True) + throw new ShaderLinkException("Error occured while trying to link a shader with message: \n" + _gl.GetProgramInfoLog(program)); + + _gl.DetachShader(program, vertexShader); + _gl.DetachShader(program, fragmentShader); + _gl.DeleteShader(vertexShader); + _gl.DeleteShader(fragmentShader); + + return program; + } +} \ No newline at end of file