2024-01-02 16:02:24 +01:00
|
|
|
|
using Silk.NET.Maths;
|
|
|
|
|
using Silk.NET.OpenGL;
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
|
|
|
|
namespace Nebulix.Rendering
|
|
|
|
|
{
|
|
|
|
|
public sealed class Mesh
|
|
|
|
|
{
|
2024-01-02 16:02:24 +01:00
|
|
|
|
public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
|
2024-01-02 14:17:50 +01:00
|
|
|
|
public nuint[] Indices { get => indices; set { indices = value; regenerate = true; } }
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
|
|
|
|
private uint vao = 0, vbo = 0, ebo = 0;
|
|
|
|
|
private bool regenerate = true;
|
|
|
|
|
|
2024-01-02 16:02:24 +01:00
|
|
|
|
private Vector3D<float>[] vertices = [];
|
2024-01-02 14:17:50 +01:00
|
|
|
|
private nuint[] indices = [];
|
2024-01-02 16:02:24 +01:00
|
|
|
|
private Vector3D<float>[] normals = [];
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
2024-01-02 14:17:50 +01:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
vertices = [];
|
|
|
|
|
indices = [];
|
|
|
|
|
}
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
2024-01-02 14:17:50 +01:00
|
|
|
|
public void CalculateNormals()
|
|
|
|
|
{
|
2024-01-02 16:02:24 +01:00
|
|
|
|
normals = new Vector3D<float>[vertices.Length];
|
|
|
|
|
for (int j = 0; j < vertices.Length; j++)
|
|
|
|
|
{
|
|
|
|
|
normals[j] = Vector3D<float>.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
for (int i = 0; i < indices.Length; i += 3)
|
|
|
|
|
{
|
|
|
|
|
Vector3D<float> a = vertices[indices[i] - 1];
|
|
|
|
|
Vector3D<float> b = vertices[indices[i + 1] - 1];
|
|
|
|
|
Vector3D<float> c = vertices[indices[i + 2] - 1];
|
|
|
|
|
|
|
|
|
|
Vector3D<float> normal = Vector3D.Cross(b-a, c-a);
|
|
|
|
|
normal = Vector3D.Normalize(normal);
|
|
|
|
|
normals[indices[i] - 1] = normal;
|
|
|
|
|
normals[indices[i + 1] - 1] = normal;
|
|
|
|
|
normals[indices[i + 2] - 1] = normal;
|
|
|
|
|
}
|
2024-01-02 14:17:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getting called by "Engine" which currently is in another assembly, meaning I probably need to make this public
|
|
|
|
|
// needs to be change for the real engine
|
2024-01-02 16:02:24 +01:00
|
|
|
|
public void Render(GL gl)
|
2023-12-22 20:18:47 +01:00
|
|
|
|
{
|
|
|
|
|
if (regenerate) Generate(gl);
|
|
|
|
|
|
|
|
|
|
gl.BindVertexArray(vao);
|
2024-01-02 16:02:24 +01:00
|
|
|
|
gl.DrawElements(PrimitiveType.Triangles, (uint)vertices.Length * 3, DrawElementsType.UnsignedInt, 0);
|
2023-12-22 20:18:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private unsafe void Generate(GL gl)
|
|
|
|
|
{
|
2024-01-02 16:02:24 +01:00
|
|
|
|
regenerate = false;
|
|
|
|
|
|
2023-12-22 20:18:47 +01:00
|
|
|
|
if(vao == 0)
|
|
|
|
|
vao = gl.CreateVertexArray();
|
|
|
|
|
if(vbo == 0)
|
|
|
|
|
vbo = gl.GenBuffer();
|
|
|
|
|
if(ebo == 0)
|
|
|
|
|
ebo = gl.GenBuffer();
|
|
|
|
|
|
|
|
|
|
gl.BindVertexArray(vao);
|
|
|
|
|
|
2024-01-02 16:02:24 +01:00
|
|
|
|
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
|
|
|
|
|
meshData.AddRange(vertices.ExtractComponents());
|
|
|
|
|
//meshData.AddRange(normals.ExtractComponents());
|
2023-12-23 11:26:59 +01:00
|
|
|
|
ReadOnlySpan<float> data = new(meshData.ToArray());
|
2023-12-22 20:18:47 +01:00
|
|
|
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
2023-12-23 11:26:59 +01:00
|
|
|
|
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(data.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
2024-01-02 14:17:50 +01:00
|
|
|
|
ReadOnlySpan<nuint> indicesData = new(indices);
|
2023-12-22 20:18:47 +01:00
|
|
|
|
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
|
2024-01-02 16:02:24 +01:00
|
|
|
|
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw);
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
|
|
|
|
// vertices
|
|
|
|
|
gl.EnableVertexAttribArray(0);
|
2024-01-02 16:02:24 +01:00
|
|
|
|
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null);
|
|
|
|
|
// normals
|
|
|
|
|
//gl.EnableVertexAttribArray(1);
|
|
|
|
|
//gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
2023-12-22 20:18:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|