2024-02-04 22:53:58 +01:00
|
|
|
|
using System.Numerics;
|
2024-01-02 16:02:24 +01:00
|
|
|
|
using Silk.NET.OpenGL;
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
|
|
|
|
namespace Nebulix.Rendering
|
|
|
|
|
{
|
|
|
|
|
public sealed class Mesh
|
|
|
|
|
{
|
2024-02-04 22:53:58 +01:00
|
|
|
|
public Vector3[] Vertices { get => vertices;
|
|
|
|
|
set { vertices = value; regenerate = true; normals = new Vector3[vertices.Length];} }
|
2024-01-03 21:47:37 +01:00
|
|
|
|
public uint[] Indices { get => indices; set { indices = value; regenerate = true; } }
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
2024-02-04 22:53:58 +01:00
|
|
|
|
private uint vao, vbo, ebo;
|
2023-12-22 20:18:47 +01:00
|
|
|
|
private bool regenerate = true;
|
|
|
|
|
|
2024-02-04 22:53:58 +01:00
|
|
|
|
private Vector3[] vertices = [];
|
2024-01-03 21:47:37 +01:00
|
|
|
|
private uint[] indices = [];
|
2024-02-04 22:53:58 +01:00
|
|
|
|
private Vector3[] 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
|
|
|
|
for (int i = 0; i < indices.Length; i += 3)
|
|
|
|
|
{
|
2024-02-01 20:06:53 +01:00
|
|
|
|
uint i0 = indices[i];
|
|
|
|
|
uint i1 = indices[i+1];
|
|
|
|
|
uint i2 = indices[i+2];
|
2024-02-04 22:53:58 +01:00
|
|
|
|
Vector3 v0 = vertices[i0];
|
|
|
|
|
Vector3 v1 = vertices[i1];
|
|
|
|
|
Vector3 v2 = vertices[i2];
|
2024-01-02 16:02:24 +01:00
|
|
|
|
|
2024-02-04 22:53:58 +01:00
|
|
|
|
Vector3 normal = Vector3.Cross(v1-v0, v2-v0);
|
2024-02-01 20:06:53 +01:00
|
|
|
|
// Commenting this out, will result in the normals being weighted based on the triangle area
|
2024-02-04 22:53:58 +01:00
|
|
|
|
normal = Vector3.Normalize(normal);
|
2024-02-01 20:06:53 +01:00
|
|
|
|
normals[i0] += normal;
|
|
|
|
|
normals[i1] += normal;
|
|
|
|
|
normals[i2] += normal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < normals.Length; i++)
|
|
|
|
|
{
|
2024-02-04 22:53:58 +01:00
|
|
|
|
normals[i] = Vector3.Normalize(normals[i]); // smoothing for shared vertices
|
2024-01-02 16:02:24 +01:00
|
|
|
|
}
|
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-03 00:22:58 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="gl"></param>
|
2024-01-03 21:47:37 +01:00
|
|
|
|
public unsafe void Render(GL gl)
|
2023-12-22 20:18:47 +01:00
|
|
|
|
{
|
|
|
|
|
if (regenerate) Generate(gl);
|
|
|
|
|
|
|
|
|
|
gl.BindVertexArray(vao);
|
2024-01-03 21:47:37 +01:00
|
|
|
|
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, null);
|
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();
|
2024-01-03 00:22:58 +01:00
|
|
|
|
gl.BindVertexArray(vao);
|
|
|
|
|
|
2023-12-22 20:18:47 +01:00
|
|
|
|
if(vbo == 0)
|
|
|
|
|
vbo = gl.GenBuffer();
|
|
|
|
|
if(ebo == 0)
|
|
|
|
|
ebo = gl.GenBuffer();
|
|
|
|
|
|
2024-01-03 21:47:37 +01:00
|
|
|
|
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
|
2024-01-04 21:19:55 +01:00
|
|
|
|
for (int i = 0, insert = 0; i < vertices.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
meshData[insert] = vertices[i].X;
|
|
|
|
|
meshData[insert + 1] = vertices[i].Y;
|
|
|
|
|
meshData[insert + 2] = vertices[i].Z;
|
|
|
|
|
|
|
|
|
|
meshData[insert + 3] = normals[i].X;
|
|
|
|
|
meshData[insert + 4] = normals[i].Y;
|
|
|
|
|
meshData[insert + 5] = normals[i].Z;
|
|
|
|
|
insert += 6;
|
|
|
|
|
}
|
2024-02-01 20:06:53 +01:00
|
|
|
|
|
2023-12-22 20:18:47 +01:00
|
|
|
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
2024-01-03 21:47:37 +01:00
|
|
|
|
fixed(void* data = &meshData[0])
|
|
|
|
|
{
|
2024-01-04 21:19:55 +01:00
|
|
|
|
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
|
2024-01-03 21:47:37 +01:00
|
|
|
|
}
|
2023-12-22 20:18:47 +01:00
|
|
|
|
|
|
|
|
|
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
|
2024-01-03 21:47:37 +01:00
|
|
|
|
fixed(void* indicesData = &indices[0])
|
|
|
|
|
{
|
|
|
|
|
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
|
|
|
|
|
}
|
2024-01-03 00:22:58 +01:00
|
|
|
|
|
2023-12-22 20:18:47 +01:00
|
|
|
|
// vertices
|
|
|
|
|
gl.EnableVertexAttribArray(0);
|
2024-01-04 21:19:55 +01:00
|
|
|
|
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), null);
|
2024-01-02 16:02:24 +01:00
|
|
|
|
// normals
|
2024-01-04 21:19:55 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|