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:
Daniel 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"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Silk.NET.Input" Version="2.20.0" /> <PackageReference Include="Silk.NET.Input" Version="2.20.0" />
<PackageReference Include="Silk.NET.OpenGL" Version="2.20.0" />
</ItemGroup> </ItemGroup>
</Project> </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

@ -2,7 +2,7 @@
using Silk.NET.OpenGL; using Silk.NET.OpenGL;
using System.Numerics; using System.Numerics;
namespace Engine_silk.NET.Shaders; namespace Nebulix.Rendering;
public class Shader public class Shader
{ {

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Engine_silk.NET.Shaders; namespace Nebulix.Rendering;
[Serializable] [Serializable]

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Engine_silk.NET.Shaders; namespace Nebulix.Rendering;
[Serializable] [Serializable]

View File

@ -32,6 +32,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="images\" /> <Folder Include="images\" />
<Folder Include="Shaders\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -2,6 +2,7 @@
using Engine_silk.NET.Textures; using Engine_silk.NET.Textures;
using Nebulix.InputSystem; using Nebulix.InputSystem;
using Nebulix.Rendering;
using Silk.NET.Input; using Silk.NET.Input;
using Silk.NET.Maths; using Silk.NET.Maths;
using Silk.NET.OpenGL; using Silk.NET.OpenGL;
@ -23,7 +24,7 @@ public static class Program
private static IWindow _window; private static IWindow _window;
private static GL _gl; private static GL _gl;
private static Shaders.Shader _shader; private static Nebulix.Rendering.Shader _shader;
private static Texture2D _texture; private static Texture2D _texture;
private static Camera _cam; private static Camera _cam;
private static Vector2 _lastMousePosition; private static Vector2 _lastMousePosition;
@ -147,7 +148,7 @@ public static class Program
//fixed (uint* buffer = indices) //fixed (uint* buffer = indices)
// _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), buffer, BufferUsageARB.StaticDraw); // _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.EnableVertexAttribArray(0);
_gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), (void*)0); _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), (void*)0);

View File

@ -1,4 +1,5 @@
using Silk.NET.Maths; using Nebulix.Rendering;
using Silk.NET.Maths;
using Silk.NET.OpenGL; using Silk.NET.OpenGL;
namespace Engine_silk.NET namespace Engine_silk.NET
@ -43,35 +44,40 @@ namespace Engine_silk.NET
return new ReadOnlySpan<float>(finalArray); return new ReadOnlySpan<float>(finalArray);
} }
/// <param name="normal">The unit vector in which the side should point. E.g. Vector.Up</param>
private (float[], float[]) GetSide(Vector3D<float> normal) [Obsolete("Use Sphere.GetFace(Vector3D<float>) instead")]
private (float[], float[]) GetSide(Vector3D<float> localUp)
{ {
//float upperBound = radius, lowerBound = -radius;
float stepSize = 2f / resolution; float stepSize = 2f / resolution;
List<float> vertices = new((int)(3 * resolution * resolution * 2)); // resolution * resolution == number of rows/columns; 3 * ... == each vertex has 3 positions; 2 * ... == vertex also needs normal List<float> 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<float> position = normal; Vector3D<float> position = localUp;
for (int row = 0; row <= resolution; row++) 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<float> percent = new Vector2D<float>(x, y) / (resolution - 1);
//Vector3D<float> pointOnUnitCube = localUp + (percent.X - 0.5f) * stepSize *
if (localUp.X != 0)
{ {
position.Y = row * stepSize - 1; position.Y = x * stepSize - 1;
position.Z = col * stepSize - 1; position.Z = y * stepSize - 1;
} }
else if (normal.Y != 0) else if (localUp.Y != 0)
{ {
position.X = row * stepSize - 1; position.X = x * stepSize - 1;
position.Z = col * stepSize - 1; position.Z = y * stepSize - 1;
} }
else if (normal.Z != 0) else if (localUp.Z != 0)
{ {
position.X = row * stepSize - 1; position.X = x * stepSize - 1;
position.Y = col * 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(), []); return (vertices.ToArray(), []);
} }
// top left origin, stepsizeX, stepsizeY (maybe index of first vertex?)
// https://youtu.be/QN39W020LqU?si=a66D1Lnic1vNaC6l&t=89 // 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
/// <param name="localUp">The unit vector in which the side should point. E.g. Vector.UnitX</param>
public Mesh GetFace(Vector3D<float> localUp)
{ {
// TODO: change coordinates according to the direction the quad should be looking // this all probably needs to be rewritten to use the "Face" record below and split the stuff better
// https://catlikecoding.com/unity/tutorials/procedural-meshes/cube-sphere/ return new Mesh();
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);
} }
} }
internal record struct Face(float[] Vertices, uint[] Indices); internal record struct Face
{
public Face()
{
}
}
} }