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

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