added shader, finished perspective camera and enabled keyboard support in the input class

This commit is contained in:
2025-08-31 13:31:05 +02:00
parent 54411da2f3
commit 71430c7793
7 changed files with 227 additions and 18 deletions

View File

@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>

View File

@ -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;
}
/// <summary>
@ -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.
}
}

View File

@ -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<float>.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<int> obj)
private void OnResize(Vector2D<int> 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);
}
}

View File

@ -1,38 +1,86 @@
// using System.Numerics;
using Silk.NET.Maths;
using Silk.NET.Maths;
namespace EngineSharp.Core;
public class PerspectiveCamera
{
private readonly Vector3D<float> _position;
private readonly Vector3D<float> _worldUp;
private Vector3D<float> _position;
private Matrix3X3<float> _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<float> 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<float> 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<float> position, float yaw = -90.0f, float pitch = 0, float fov = 45.0f, float nearClippingPlane = 0.1f, float farClippingPlane = 100.0f)
public Matrix4X4<float> 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<float> ProjectionMatrix => Matrix4X4.CreatePerspectiveFieldOfView(Fov, _aspectRatio, NearClippingPlane, FarClippingPlane);
public PerspectiveCamera(Vector3D<float> position, Vector2D<int> 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<float>.UnitY;
UpdateCameraVectors();
UpdateAspect(viewportDimensions);
}
public void ProcessMovement(float deltaTime, Vector3D<float> movementDirection) {
var velocity = MovementSpeed * deltaTime; // velocity not really the right term, but oh well
_position += (movementDirection * velocity) * _intrinsicCoordinates;
}
public void ProcessMouse(Vector2D<float> 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<int> 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;
}
}

View File

@ -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;
}
}

View File

@ -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) { }
}

View File

@ -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<double> value)
{
_gl.Uniform3(_gl.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z);
}
public void SetVector(string name, Vector3D<float> value)
{
_gl.Uniform3(_gl.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z);
}
public void SetMatrix(string name, Matrix4X4<double> matrix)
{
unsafe
{
_gl.UniformMatrix4(_gl.GetUniformLocation(_shaderProgramId, name), 1, false, (double*) &matrix);
}
}
public void SetMatrix(string name, Matrix4X4<float> matrix)
{
unsafe
{
_gl.UniformMatrix4(_gl.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix);
}
}
#endregion
/// <summary>
/// Compiles the given shadercode.
/// </summary>
/// <param name="shaderCode">The shadercode</param>
/// <param name="shaderType">The type of shader to compile</param>
/// <returns>Returns the id of the compiled shader.</returns>
/// <exception cref="ShaderCompileException"></exception>
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;
}
/// <summary>
/// Creates a shader program and links the vertex and fragment shader together.
/// </summary>
/// <returns>Returns the id of the created shader program</returns>
/// <exception cref="ShaderLinkException"></exception>
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;
}
}