Mesh can now be rendered and therefore also the sphere
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s

This commit is contained in:
2024-01-03 21:47:37 +01:00
parent cc3493627d
commit d5f3a34a59
4 changed files with 45 additions and 56 deletions

View File

@ -6,13 +6,13 @@ namespace Nebulix.Rendering
public sealed class Mesh
{
public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public nuint[] Indices { get => indices; set { indices = value; regenerate = true; } }
public uint[] Indices { get => indices; set { indices = value; regenerate = true; } }
private uint vao = 0, vbo = 0, ebo = 0;
private bool regenerate = true;
private Vector3D<float>[] vertices = [];
private nuint[] indices = [];
private uint[] indices = [];
private Vector3D<float>[] normals = [];
public void Clear()
@ -50,12 +50,12 @@ namespace Nebulix.Rendering
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
/// </summary>
/// <param name="gl"></param>
public void Render(GL gl)
public unsafe void Render(GL gl)
{
if (regenerate) Generate(gl);
gl.BindVertexArray(vao);
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, 0);
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, null);
}
private unsafe void Generate(GL gl)
@ -71,16 +71,20 @@ namespace Nebulix.Rendering
if(ebo == 0)
ebo = gl.GenBuffer();
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
meshData.AddRange(vertices.ExtractComponents());
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
vertices.ExtractComponents().CopyTo(meshData, 0);
//meshData.AddRange(normals.ExtractComponents());
ReadOnlySpan<float> data = new(meshData.ToArray());
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(data.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
fixed(void* data = &meshData[0])
{
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * 3 * sizeof(float)), data, BufferUsageARB.StaticDraw);
}
ReadOnlySpan<nuint> indicesData = new(indices);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw);
fixed(void* indicesData = &indices[0])
{
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
}
// vertices
gl.EnableVertexAttribArray(0);