From 3c598d57f38ccc3aa338744260859ca8d3f65408 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 Dec 2023 20:18:47 +0100 Subject: [PATCH] Updated structure and took first steps for a better architecture --- Nebulix/Nebulix.csproj | 4 +- Nebulix/Rendering/Material.cs | 13 ++++ Nebulix/Rendering/Mesh.cs | 60 ++++++++++++++++ {src => Nebulix/Rendering}/Shaders/Shader.cs | 2 +- .../Shaders/ShaderCompileException.cs | 2 +- .../Rendering}/Shaders/ShaderLinkException.cs | 2 +- src/Engine_silk.NET.csproj | 1 + src/Program.cs | 5 +- src/Sphere.cs | 69 ++++++++++--------- 9 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 Nebulix/Rendering/Material.cs create mode 100644 Nebulix/Rendering/Mesh.cs rename {src => Nebulix/Rendering}/Shaders/Shader.cs (96%) rename {src => Nebulix/Rendering}/Shaders/ShaderCompileException.cs (93%) rename {src => Nebulix/Rendering}/Shaders/ShaderLinkException.cs (91%) diff --git a/Nebulix/Nebulix.csproj b/Nebulix/Nebulix.csproj index ddec386..590dd7c 100644 --- a/Nebulix/Nebulix.csproj +++ b/Nebulix/Nebulix.csproj @@ -1,13 +1,15 @@ - net6.0 + net8.0 enable enable + true + diff --git a/Nebulix/Rendering/Material.cs b/Nebulix/Rendering/Material.cs new file mode 100644 index 0000000..4c4941c --- /dev/null +++ b/Nebulix/Rendering/Material.cs @@ -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) + { + } +} diff --git a/Nebulix/Rendering/Mesh.cs b/Nebulix/Rendering/Mesh.cs new file mode 100644 index 0000000..d6f5d9b --- /dev/null +++ b/Nebulix/Rendering/Mesh.cs @@ -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 verticesData = new(vertices); + gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); + gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(verticesData.Length * sizeof(float)), verticesData, BufferUsageARB.StaticDraw); + + ReadOnlySpan 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))); + } + } +} diff --git a/src/Shaders/Shader.cs b/Nebulix/Rendering/Shaders/Shader.cs similarity index 96% rename from src/Shaders/Shader.cs rename to Nebulix/Rendering/Shaders/Shader.cs index 50ffa76..571c17c 100644 --- a/src/Shaders/Shader.cs +++ b/Nebulix/Rendering/Shaders/Shader.cs @@ -2,7 +2,7 @@ using Silk.NET.OpenGL; using System.Numerics; -namespace Engine_silk.NET.Shaders; +namespace Nebulix.Rendering; public class Shader { diff --git a/src/Shaders/ShaderCompileException.cs b/Nebulix/Rendering/Shaders/ShaderCompileException.cs similarity index 93% rename from src/Shaders/ShaderCompileException.cs rename to Nebulix/Rendering/Shaders/ShaderCompileException.cs index 17bf8db..65d4244 100644 --- a/src/Shaders/ShaderCompileException.cs +++ b/Nebulix/Rendering/Shaders/ShaderCompileException.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Engine_silk.NET.Shaders; +namespace Nebulix.Rendering; [Serializable] diff --git a/src/Shaders/ShaderLinkException.cs b/Nebulix/Rendering/Shaders/ShaderLinkException.cs similarity index 91% rename from src/Shaders/ShaderLinkException.cs rename to Nebulix/Rendering/Shaders/ShaderLinkException.cs index b5d328c..27a3f8b 100644 --- a/src/Shaders/ShaderLinkException.cs +++ b/Nebulix/Rendering/Shaders/ShaderLinkException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Engine_silk.NET.Shaders; +namespace Nebulix.Rendering; [Serializable] diff --git a/src/Engine_silk.NET.csproj b/src/Engine_silk.NET.csproj index e07a5e3..6e39abd 100644 --- a/src/Engine_silk.NET.csproj +++ b/src/Engine_silk.NET.csproj @@ -32,6 +32,7 @@ + diff --git a/src/Program.cs b/src/Program.cs index 9e1ef8c..cd65e8e 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -2,6 +2,7 @@ using Engine_silk.NET.Textures; using Nebulix.InputSystem; +using Nebulix.Rendering; using Silk.NET.Input; using Silk.NET.Maths; using Silk.NET.OpenGL; @@ -23,7 +24,7 @@ public static class Program private static IWindow _window; private static GL _gl; - private static Shaders.Shader _shader; + private static Nebulix.Rendering.Shader _shader; private static Texture2D _texture; private static Camera _cam; private static Vector2 _lastMousePosition; @@ -147,7 +148,7 @@ public static class Program //fixed (uint* buffer = indices) // _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), buffer, BufferUsageARB.StaticDraw); - _shader = new Shaders.Shader(_gl, "shader.vert", "shader.frag"); + _shader = new Nebulix.Rendering.Shader(_gl, "shader.vert", "shader.frag"); _gl.EnableVertexAttribArray(0); _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), (void*)0); diff --git a/src/Sphere.cs b/src/Sphere.cs index 7478598..71d160c 100644 --- a/src/Sphere.cs +++ b/src/Sphere.cs @@ -1,4 +1,5 @@ -using Silk.NET.Maths; +using Nebulix.Rendering; +using Silk.NET.Maths; using Silk.NET.OpenGL; namespace Engine_silk.NET @@ -43,35 +44,40 @@ namespace Engine_silk.NET return new ReadOnlySpan(finalArray); } - /// The unit vector in which the side should point. E.g. Vector.Up - private (float[], float[]) GetSide(Vector3D normal) + + [Obsolete("Use Sphere.GetFace(Vector3D) instead")] + private (float[], float[]) GetSide(Vector3D localUp) { - //float upperBound = radius, lowerBound = -radius; + float stepSize = 2f / resolution; List vertices = new((int)(3 * resolution * resolution * 2)); // resolution * resolution == number of rows/columns; 3 * ... == each vertex has 3 positions; 2 * ... == vertex also needs normal - Vector3D position = normal; - for (int row = 0; row <= resolution; row++) + Vector3D position = localUp; + for (uint y = 0; y <= resolution; y++) { - for (int col = 0; col <= resolution; col++) + for (uint x = 0; x <= resolution; x++) { - if (normal.X != 0) + uint i = x + y * resolution; + Vector2D percent = new Vector2D(x, y) / (resolution - 1); + //Vector3D pointOnUnitCube = localUp + (percent.X - 0.5f) * stepSize * + + if (localUp.X != 0) { - position.Y = row * stepSize - 1; - position.Z = col * stepSize - 1; + position.Y = x * stepSize - 1; + position.Z = y * stepSize - 1; } - else if (normal.Y != 0) + else if (localUp.Y != 0) { - position.X = row * stepSize - 1; - position.Z = col * stepSize - 1; + position.X = x * stepSize - 1; + position.Z = y * stepSize - 1; } - else if (normal.Z != 0) + else if (localUp.Z != 0) { - position.X = row * stepSize - 1; - position.Y = col * stepSize - 1; + position.X = x * stepSize - 1; + position.Y = y * stepSize - 1; } - vertices.AddRange([position.X, position.Y, position.Z, normal.X, normal.Y, normal.Z]); + vertices.AddRange([position.X, position.Y, position.Z, localUp.X, localUp.Y, localUp.Z]); } } @@ -97,26 +103,21 @@ namespace Engine_silk.NET return (vertices.ToArray(), []); } - // top left origin, stepsizeX, stepsizeY (maybe index of first vertex?) // https://youtu.be/QN39W020LqU?si=a66D1Lnic1vNaC6l&t=89 - private Face Quad(uint resolution, uint row, uint column) + // https://github.com/SebLague/Procedural-Planets/blob/master/Procedural%20Planet%20E01/Assets/TerrainFace.cs + /// The unit vector in which the side should point. E.g. Vector.UnitX + public Mesh GetFace(Vector3D localUp) { - // TODO: change coordinates according to the direction the quad should be looking - // https://catlikecoding.com/unity/tutorials/procedural-meshes/cube-sphere/ - float[] vertices = [ - 1.0f, 1.0f, 0.0f, // top right - 1.0f, -1.0f, 0.0f, // bottom right - -1.0f, -1.0f, 0.0f, // bottom left - -1.0f, 1.0f, 0.0f // top left - ]; - uint[] indices = [ - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - ]; - - return new Face(vertices, indices); + // this all probably needs to be rewritten to use the "Face" record below and split the stuff better + return new Mesh(); } } - internal record struct Face(float[] Vertices, uint[] Indices); + internal record struct Face + { + public Face() + { + + } + } }