diff --git a/NResUI/Rendering.zip b/NResUI/Rendering.zip new file mode 100644 index 0000000..00430b1 Binary files /dev/null and b/NResUI/Rendering.zip differ diff --git a/NResUI/Rendering/Viewport/Meshes/PrimitiveMeshes.cs b/NResUI/Rendering/Viewport/Meshes/PrimitiveMeshes.cs index 8631d80..e5e529b 100644 --- a/NResUI/Rendering/Viewport/Meshes/PrimitiveMeshes.cs +++ b/NResUI/Rendering/Viewport/Meshes/PrimitiveMeshes.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Numerics; using Silk.NET.OpenGL; namespace NResUI.Rendering.Viewport.Meshes; @@ -8,50 +7,64 @@ public static unsafe class PrimitiveMeshes { public static GpuMesh CreateCube(GL gl) { - // position.xyz, color.rgb - float[] vertices = - { - // front - -1, -1, 1, 1, 0, 0, - 1, -1, 1, 0, 1, 0, - 1, 1, 1, 0, 0, 1, - -1, 1, 1, 1, 1, 0, + var vertices = new List(); + var indices = new List(); - // back - -1, -1, -1, 1, 0, 1, - 1, -1, -1, 0, 1, 1, - 1, 1, -1, 1, 1, 1, - -1, 1, -1, 0.5f, 0.5f, 0.5f, - }; + AddCubeFace( + vertices, indices, + new Vector3(-1, -1, 1), + new Vector3( 1, -1, 1), + new Vector3( 1, 1, 1), + new Vector3(-1, 1, 1), + new Vector3(0, 0, 1), + new Vector3(1, 0, 0)); - uint[] indices = - { - // front - 0, 1, 2, - 2, 3, 0, + AddCubeFace( + vertices, indices, + new Vector3( 1, -1, -1), + new Vector3(-1, -1, -1), + new Vector3(-1, 1, -1), + new Vector3( 1, 1, -1), + new Vector3(0, 0, -1), + new Vector3(0, 1, 0)); - // right - 1, 5, 6, - 6, 2, 1, + AddCubeFace( + vertices, indices, + new Vector3(-1, -1, -1), + new Vector3(-1, -1, 1), + new Vector3(-1, 1, 1), + new Vector3(-1, 1, -1), + new Vector3(-1, 0, 0), + new Vector3(0, 0, 1)); - // back - 5, 4, 7, - 7, 6, 5, + AddCubeFace( + vertices, indices, + new Vector3(1, -1, 1), + new Vector3(1, -1, -1), + new Vector3(1, 1, -1), + new Vector3(1, 1, 1), + new Vector3(1, 0, 0), + new Vector3(1, 1, 0)); - // left - 4, 0, 3, - 3, 7, 4, + AddCubeFace( + vertices, indices, + new Vector3(-1, 1, 1), + new Vector3( 1, 1, 1), + new Vector3( 1, 1, -1), + new Vector3(-1, 1, -1), + new Vector3(0, 1, 0), + new Vector3(1, 0, 1)); - // top - 3, 2, 6, - 6, 7, 3, + AddCubeFace( + vertices, indices, + new Vector3(-1, -1, -1), + new Vector3( 1, -1, -1), + new Vector3( 1, -1, 1), + new Vector3(-1, -1, 1), + new Vector3(0, -1, 0), + new Vector3(0, 1, 1)); - // bottom - 4, 5, 1, - 1, 0, 4, - }; - - return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Triangles); + return CreateColoredIndexedMesh(gl, vertices, indices, PrimitiveType.Triangles); } public static GpuMesh CreateWorldGrid( @@ -72,25 +85,15 @@ public static unsafe class PrimitiveMeshes var vertices = new List(); var indices = new List(); - void AddVertex(float x, float y, float z, float r, float g, float b) - { - vertices.Add(x); - vertices.Add(y); - vertices.Add(z); - vertices.Add(r); - vertices.Add(g); - vertices.Add(b); - } - void AddLine( float x0, float y0, float z0, float x1, float y1, float z1, float r, float g, float b) { - var startIndex = (uint)(vertices.Count / 6); + var startIndex = (uint)(vertices.Count / 9); - AddVertex(x0, y0, z0, r, g, b); - AddVertex(x1, y1, z1, r, g, b); + AddVertex(vertices, x0, y0, z0, r, g, b, 0, 1, 0); + AddVertex(vertices, x1, y1, z1, r, g, b, 0, 1, 0); indices.Add(startIndex); indices.Add(startIndex + 1); @@ -132,30 +135,88 @@ public static unsafe class PrimitiveMeshes return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines); } + private static void AddCubeFace( + List vertices, + List indices, + Vector3 a, + Vector3 b, + Vector3 c, + Vector3 d, + Vector3 normal, + Vector3 color) + { + var start = (uint)(vertices.Count / 9); + + AddVertex(vertices, a.X, a.Y, a.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z); + AddVertex(vertices, b.X, b.Y, b.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z); + AddVertex(vertices, c.X, c.Y, c.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z); + AddVertex(vertices, d.X, d.Y, d.Z, color.X, color.Y, color.Z, normal.X, normal.Y, normal.Z); + + indices.Add(start + 0); + indices.Add(start + 1); + indices.Add(start + 2); + + indices.Add(start + 2); + indices.Add(start + 3); + indices.Add(start + 0); + } public static GpuMesh CreateUnitWireBox(GL gl) { - float[] vertices = + var vertices = new List(); + var indices = new List(); + + var color = new Vector3(1.0f, 0.82f, 0.15f); + + Vector3[] corners = { - -1, -1, -1, 1.0f, 0.82f, 0.15f, - 1, -1, -1, 1.0f, 0.82f, 0.15f, - 1, 1, -1, 1.0f, 0.82f, 0.15f, - -1, 1, -1, 1.0f, 0.82f, 0.15f, - -1, -1, 1, 1.0f, 0.82f, 0.15f, - 1, -1, 1, 1.0f, 0.82f, 0.15f, - 1, 1, 1, 1.0f, 0.82f, 0.15f, - -1, 1, 1, 1.0f, 0.82f, 0.15f, + new(-1, -1, -1), + new( 1, -1, -1), + new( 1, 1, -1), + new(-1, 1, -1), + new(-1, -1, 1), + new( 1, -1, 1), + new( 1, 1, 1), + new(-1, 1, 1), }; - uint[] indices = + foreach (var corner in corners) + { + AddVertex(vertices, corner.X, corner.Y, corner.Z, color.X, color.Y, color.Z); + } + + uint[] lineIndices = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7, }; - return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Lines); + indices.AddRange(lineIndices); + + return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines); + } + + private static void AddVertex( + List vertices, + float x, float y, float z, + float r, float g, float b, + float nx = 0.0f, + float ny = 1.0f, + float nz = 0.0f) + { + vertices.Add(x); + vertices.Add(y); + vertices.Add(z); + + vertices.Add(r); + vertices.Add(g); + vertices.Add(b); + + vertices.Add(nx); + vertices.Add(ny); + vertices.Add(nz); } public static GpuMesh CreateAxes(GL gl, float length = 1.0f) @@ -163,26 +224,25 @@ public static unsafe class PrimitiveMeshes if (length <= 0.0f) throw new ArgumentOutOfRangeException(nameof(length)); - float[] vertices = + var vertices = new List(); + var indices = new List(); + + void AddLine(Vector3 a, Vector3 b, Vector3 color) { - 0, 0, 0, 1.0f, 0.25f, 0.25f, - length, 0, 0, 1.0f, 0.25f, 0.25f, + var start = (uint)(vertices.Count / 9); - 0, 0, 0, 0.25f, 1.0f, 0.25f, - 0, length, 0, 0.25f, 1.0f, 0.25f, + AddVertex(vertices, a.X, a.Y, a.Z, color.X, color.Y, color.Z); + AddVertex(vertices, b.X, b.Y, b.Z, color.X, color.Y, color.Z); - 0, 0, 0, 0.25f, 0.45f, 1.0f, - 0, 0, length, 0.25f, 0.45f, 1.0f, - }; + indices.Add(start); + indices.Add(start + 1); + } - uint[] indices = - { - 0, 1, - 2, 3, - 4, 5, - }; + AddLine(Vector3.Zero, new Vector3(length, 0, 0), new Vector3(1.0f, 0.25f, 0.25f)); + AddLine(Vector3.Zero, new Vector3(0, length, 0), new Vector3(0.25f, 1.0f, 0.25f)); + AddLine(Vector3.Zero, new Vector3(0, 0, length), new Vector3(0.25f, 0.45f, 1.0f)); - return CreateIndexedMesh(gl, vertices, indices, PrimitiveType.Lines); + return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines); } public static GpuMesh CreateColoredIndexedMesh( diff --git a/NResUI/Rendering/Viewport/Msh/MshViewportLoader.cs b/NResUI/Rendering/Viewport/Msh/MshViewportLoader.cs index 1776a68..332bd1d 100644 --- a/NResUI/Rendering/Viewport/Msh/MshViewportLoader.cs +++ b/NResUI/Rendering/Viewport/Msh/MshViewportLoader.cs @@ -178,7 +178,7 @@ public static class MshViewportLoader ref Vector3 boundsMax ) { - var vertexIndex = (uint)(vertices.Count / 6); + var vertexIndex = (uint)(vertices.Count / 9); vertices.Add(position.X); vertices.Add(position.Y); diff --git a/NResUI/Rendering/Viewport/ViewportRenderer.cs b/NResUI/Rendering/Viewport/ViewportRenderer.cs index b1bdc11..facd1a6 100644 --- a/NResUI/Rendering/Viewport/ViewportRenderer.cs +++ b/NResUI/Rendering/Viewport/ViewportRenderer.cs @@ -93,16 +93,11 @@ public sealed class ViewportRenderer if (grid == null || !grid.IsVisible) return; - var model = grid.LocalTransform * sceneRotation; - var mvp = model * view * projection; - _gl.Disable(EnableCap.StencilTest); _gl.PolygonMode(TriangleFace.FrontAndBack, PolygonMode.Fill); - _meshShader.Use(); - _meshShader.SetMatrix4(_mvpLocation, mvp); - grid.Mesh.Draw(); - + var model = grid.LocalTransform * sceneRotation; + DrawMesh(grid.Mesh, model, view, projection); } private void DrawScene(