diff --git a/src/Program.cs b/src/Program.cs index 76375f4..319e236 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -32,7 +32,7 @@ public static class Program private static Camera _cam; private static Vector2 _lastMousePosition; private static uint _vao, _vbo; - private static IcoSphere sphere; + private static Sphere sphere; public static void Main(string[] args) { @@ -140,7 +140,7 @@ public static class Program // Sphere - sphere = new IcoSphere(10); + sphere = new Sphere(10); sphere.CreateSphere(); _sphereShader = new Nebulix.Rendering.Shader(_gl, "./Shaders/Sphere/sphere.vert", "./Shaders/Sphere/sphere.frag"); diff --git a/src/Sphere.cs b/src/Sphere.cs index 23c59db..984e351 100644 --- a/src/Sphere.cs +++ b/src/Sphere.cs @@ -6,9 +6,11 @@ using Silk.NET.OpenGL; namespace Engine_silk.NET { // also maybe make an ISphere and call this class "CubeSphere"? + // Also look into this way of generating a cube sphere: http://www.songho.ca/opengl/gl_sphere.html public class Sphere { - private readonly Mesh mesh = new(); + private readonly Mesh[] mesh = new Mesh[6]; + private readonly Face[] faces = new Face[6]; private readonly uint resolution; public Sphere(uint resolution) @@ -21,7 +23,6 @@ namespace Engine_silk.NET public void CreateSphere() { - // TODO: merge the individual meshes to one mesh Vector3[] directions = [ Vector3.UnitZ, -Vector3.UnitZ, @@ -29,25 +30,15 @@ namespace Engine_silk.NET Vector3.UnitX, -Vector3.UnitX ]; - List vertices = new(); - List indices = new(); for (int i = 0; i < directions.Length; i++) { - Mesh m = new(); - Face f = new(directions[i], m, resolution); - f.GenerateMesh(); - - vertices.AddRange(m.Vertices); - for (int j = 0; j < m.Indices.Length; j++) + if (mesh[i] is null) // linting not working here. mesh[i] can actually be null { - indices.Add(m.Indices[j] + (uint)(m.Vertices.Length * i)); + mesh[i] = new Mesh(); + faces[i] = new Face(directions[i], mesh[i], resolution); } + faces[i].GenerateMesh(); } - // TODO get rid of overlapping vertices maybe - mesh.Clear(); - mesh.Vertices = vertices.ToArray(); - mesh.Indices = indices.ToArray(); - mesh.CalculateNormals(); } /// @@ -55,11 +46,14 @@ namespace Engine_silk.NET /// public void RenderSphere(GL gl) // Will not be needed { - mesh.Render(gl); + for (int i = 0; i < mesh.Length; i++) + { + faces[i].Mesh.Render(gl); + } } } - internal record struct Face + internal readonly record struct Face { public Mesh Mesh => _mesh;