using Silk.NET.Maths; using Silk.NET.OpenGL; namespace Nebulix.Rendering { public sealed class Mesh { public Vector3D[] Vertices { get => vertices; set { vertices = 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[] vertices = []; private uint[] indices = []; private Vector3D[] normals = []; public void Clear() { vertices = []; indices = []; } public void CalculateNormals() { normals = new Vector3D[vertices.Length]; for (int j = 0; j < vertices.Length; j++) { normals[j] = Vector3D.Zero; } return; for (int i = 0; i < indices.Length; i += 3) { Vector3D a = vertices[indices[i] - 1]; Vector3D b = vertices[indices[i + 1] - 1]; Vector3D c = vertices[indices[i + 2] - 1]; Vector3D 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; } } // 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 /// /// Binds the necessary buffers and draws the mesh. Does not use any Shaders /// /// 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]; vertices.ExtractComponents().CopyTo(meshData, 0); //meshData.AddRange(normals.ExtractComponents()); gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); fixed(void* data = &meshData[0]) { gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * 3 * 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, 3 * sizeof(float), null); // normals //gl.EnableVertexAttribArray(1); //gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float))); } } }