Daniel
3912f2bd9a
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 21s
111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
using System.Numerics;
|
|
using Silk.NET.OpenGL;
|
|
|
|
namespace Nebulix.Rendering
|
|
{
|
|
public sealed class Mesh
|
|
{
|
|
public Vector3[] Vertices { get => vertices;
|
|
set { vertices = value; regenerate = true; normals = new Vector3[vertices.Length];} }
|
|
public uint[] Indices { get => indices; set { indices = value; regenerate = true; } }
|
|
|
|
private uint vao, vbo, ebo;
|
|
private bool regenerate = true;
|
|
|
|
private Vector3[] vertices = [];
|
|
private uint[] indices = [];
|
|
private Vector3[] normals = [];
|
|
|
|
public void Clear()
|
|
{
|
|
vertices = [];
|
|
indices = [];
|
|
}
|
|
|
|
public void CalculateNormals()
|
|
{
|
|
for (int i = 0; i < indices.Length; i += 3)
|
|
{
|
|
uint i0 = indices[i];
|
|
uint i1 = indices[i+1];
|
|
uint i2 = indices[i+2];
|
|
Vector3 v0 = vertices[i0];
|
|
Vector3 v1 = vertices[i1];
|
|
Vector3 v2 = vertices[i2];
|
|
|
|
Vector3 normal = Vector3.Cross(v1-v0, v2-v0);
|
|
// Commenting this out, will result in the normals being weighted based on the triangle area
|
|
normal = Vector3.Normalize(normal);
|
|
normals[i0] += normal;
|
|
normals[i1] += normal;
|
|
normals[i2] += normal;
|
|
}
|
|
|
|
for (int i = 0; i < normals.Length; i++)
|
|
{
|
|
normals[i] = Vector3.Normalize(normals[i]); // smoothing for shared vertices
|
|
}
|
|
}
|
|
|
|
// 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
|
|
/// <summary>
|
|
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
|
|
/// </summary>
|
|
/// <param name="gl"></param>
|
|
public unsafe void Render(GL gl)
|
|
{
|
|
if (regenerate) Generate(gl);
|
|
|
|
gl.BindVertexArray(vao);
|
|
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, null);
|
|
}
|
|
|
|
private unsafe void Generate(GL gl)
|
|
{
|
|
regenerate = false;
|
|
|
|
if(vao == 0)
|
|
vao = gl.CreateVertexArray();
|
|
gl.BindVertexArray(vao);
|
|
|
|
if(vbo == 0)
|
|
vbo = gl.GenBuffer();
|
|
if(ebo == 0)
|
|
ebo = gl.GenBuffer();
|
|
|
|
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
|
|
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;
|
|
}
|
|
|
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
|
fixed(void* data = &meshData[0])
|
|
{
|
|
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
|
|
}
|
|
|
|
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
|
|
fixed(void* indicesData = &indices[0])
|
|
{
|
|
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
|
|
}
|
|
|
|
// vertices
|
|
gl.EnableVertexAttribArray(0);
|
|
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), null);
|
|
// normals
|
|
gl.EnableVertexAttribArray(1);
|
|
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
}
|
|
}
|
|
}
|