Updated structure and took first steps for a better architecture
Some checks failed
Gitea Actions Demo / Scan the project (push) Failing after 5s

This commit is contained in:
2023-12-22 20:18:47 +01:00
parent 4b681e8a94
commit 3c598d57f3
9 changed files with 118 additions and 40 deletions

View File

@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Silk.NET.Input" Version="2.20.0" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.20.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,13 @@
using Silk.NET.Core.Native;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nebulix.Rendering
{
public record class Material(Shader Shader)
{
}
}

60
Nebulix/Rendering/Mesh.cs Normal file
View File

@ -0,0 +1,60 @@
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nebulix.Rendering
{
public sealed class Mesh
{
public float[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public float[] Indices { get => indices; set { indices = value; regenerate = true; } }
public float[] Normals { get => normals; }
private uint vao = 0, vbo = 0, ebo = 0;
private bool regenerate = true;
private float[] vertices = [];
private float[] indices = [];
private float[] normals = [];
// getting called by "Engine" which currently is in other assembly, meaning I probably need to make this public
internal void Use(GL gl)
{
if (regenerate) Generate(gl);
gl.BindVertexArray(vao);
}
private unsafe void Generate(GL gl)
{
if(vao == 0)
vao = gl.CreateVertexArray();
if(vbo == 0)
vbo = gl.GenBuffer();
if(ebo == 0)
ebo = gl.GenBuffer();
gl.BindVertexArray(vao);
// TODO: verticesData needs to also contain "normals" not just vertices (and uv coords if I decide to add some)
ReadOnlySpan<float> verticesData = new(vertices);
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(verticesData.Length * sizeof(float)), verticesData, BufferUsageARB.StaticDraw);
ReadOnlySpan<float> indicesData = new(indices);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
// vertices
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)0);
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
}
}
}

View File

@ -0,0 +1,94 @@
using Silk.NET.Maths;
using Silk.NET.OpenGL;
using System.Numerics;
namespace Nebulix.Rendering;
public class Shader
{
private readonly GL _glContext;
private readonly uint _shaderProgramId;
public Shader(GL openGLContext, string pathToVertexShader, string pathToFragmentShader)
{
_glContext = 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() { _glContext.UseProgram(_shaderProgramId); }
#region Set uniforms
public void SetInt(string name, int value)
{
_glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value);
}
public void SetFloat(string name, float value)
{
_glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value);
}
public unsafe void SetMatrix(string name, Matrix4x4 matrix)
{
_glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix);
}
public unsafe void SetMatrix(string name, Matrix4X4<float> matrix)
{
_glContext.UniformMatrix4(_glContext.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>
protected uint CompileShader(string shaderCode, ShaderType shaderType)
{
uint shader = _glContext.CreateShader(shaderType);
_glContext.ShaderSource(shader, shaderCode);
_glContext.CompileShader(shader);
_glContext.GetShader(shader, ShaderParameterName.CompileStatus, out int status);
if (status != (int)GLEnum.True)
throw new ShaderCompileException(shaderType, $"Failed to compile shader with message: \n {_glContext.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>
protected uint CreateProgram(uint vertexShader, uint fragmentShader)
{
uint program = _glContext.CreateProgram();
_glContext.AttachShader(program, vertexShader);
_glContext.AttachShader(program, fragmentShader);
_glContext.LinkProgram(program);
_glContext.GetProgram(program, ProgramPropertyARB.LinkStatus, out int lStatus);
if (lStatus != (int)GLEnum.True)
throw new ShaderLinkException("Error occured while trying to link a shader with message: \n" + _glContext.GetProgramInfoLog(program));
// should be done right after compiling and linking
_glContext.DetachShader(program, vertexShader);
_glContext.DetachShader(program, fragmentShader);
_glContext.DeleteShader(vertexShader);
_glContext.DeleteShader(fragmentShader);
return program;
}
}

View File

@ -0,0 +1,27 @@
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nebulix.Rendering;
[Serializable]
public class ShaderCompileException : Exception
{
protected 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; }
protected ShaderCompileException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
public override string ToString()
{
return $"Type of Shader: '{_shaderType}'" + "\n" + Message;
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nebulix.Rendering;
[Serializable]
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) { }
protected ShaderLinkException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}