2026-06-06 01:24:09 +03:00
|
|
|
using System.Numerics;
|
2026-06-05 20:33:21 +03:00
|
|
|
using Silk.NET.OpenGL;
|
2026-06-06 21:26:56 +03:00
|
|
|
using NResUI.Rendering.Viewport;
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
namespace NResUI.Rendering.Viewport.Meshes;
|
|
|
|
|
|
|
|
|
|
public static unsafe class PrimitiveMeshes
|
|
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
public const int FloatsPerVertex = 11;
|
|
|
|
|
|
2026-06-05 20:33:21 +03:00
|
|
|
public static GpuMesh CreateCube(GL gl)
|
|
|
|
|
{
|
2026-06-06 01:24:09 +03:00
|
|
|
var vertices = new List<float>();
|
|
|
|
|
var indices = new List<uint>();
|
2026-06-05 20:33:21 +03:00
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
AddCubeFace(vertices, indices, new(-1, -1, 1), new( 1, -1, 1), new( 1, 1, 1), new(-1, 1, 1), new(0, 0, 1), new(1, 0, 0));
|
|
|
|
|
AddCubeFace(vertices, indices, new( 1, -1, -1), new(-1, -1, -1), new(-1, 1, -1), new( 1, 1, -1), new(0, 0, -1), new(0, 1, 0));
|
|
|
|
|
AddCubeFace(vertices, indices, new(-1, -1, -1), new(-1, -1, 1), new(-1, 1, 1), new(-1, 1, -1), new(-1, 0, 0), new(0, 0, 1));
|
|
|
|
|
AddCubeFace(vertices, indices, new( 1, -1, 1), new( 1, -1, -1), new( 1, 1, -1), new( 1, 1, 1), new(1, 0, 0), new(1, 1, 0));
|
|
|
|
|
AddCubeFace(vertices, indices, new(-1, 1, 1), new( 1, 1, 1), new( 1, 1, -1), new(-1, 1, -1), new(0, 1, 0), new(1, 0, 1));
|
|
|
|
|
AddCubeFace(vertices, indices, new(-1, -1, -1), new( 1, -1, -1), new( 1, -1, 1), new(-1, -1, 1), new(0, -1, 0), new(0, 1, 1));
|
2026-06-06 01:24:09 +03:00
|
|
|
|
|
|
|
|
return CreateColoredIndexedMesh(gl, vertices, indices, PrimitiveType.Triangles);
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
public static GpuMesh CreateWorldGrid(GL gl, int halfExtent = 20, int minorStep = 1, int majorStep = 5)
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
if (halfExtent <= 0) throw new ArgumentOutOfRangeException(nameof(halfExtent));
|
|
|
|
|
if (minorStep <= 0) throw new ArgumentOutOfRangeException(nameof(minorStep));
|
|
|
|
|
if (majorStep <= 0) throw new ArgumentOutOfRangeException(nameof(majorStep));
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
var vertices = new List<float>();
|
|
|
|
|
var indices = new List<uint>();
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
void AddLine(float x0, float y0, float z0, float x1, float y1, float z1, float r, float g, float b)
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
var startIndex = (uint)(vertices.Count / FloatsPerVertex);
|
|
|
|
|
AddVertex(vertices, x0, y0, z0, r, g, b);
|
|
|
|
|
AddVertex(vertices, x1, y1, z1, r, g, b);
|
2026-06-05 20:33:21 +03:00
|
|
|
indices.Add(startIndex);
|
|
|
|
|
indices.Add(startIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float y = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (var i = -halfExtent; i <= halfExtent; i += minorStep)
|
|
|
|
|
{
|
|
|
|
|
var isAxis = i == 0;
|
|
|
|
|
var isMajor = i % majorStep == 0;
|
|
|
|
|
var brightness = isMajor ? 0.38f : 0.24f;
|
|
|
|
|
var r = brightness;
|
|
|
|
|
var g = brightness;
|
|
|
|
|
var b = brightness;
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
AddLine(-halfExtent, y, i, halfExtent, y, i, isAxis ? 0.25f : r, isAxis ? 0.45f : g, isAxis ? 1.0f : b);
|
|
|
|
|
AddLine(i, y, -halfExtent, i, y, halfExtent, isAxis ? 1.0f : r, isAxis ? 0.25f : g, isAxis ? 0.25f : b);
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-05 20:47:57 +03:00
|
|
|
public static GpuMesh CreateUnitWireBox(GL gl)
|
|
|
|
|
{
|
2026-06-06 01:24:09 +03:00
|
|
|
var vertices = new List<float>();
|
|
|
|
|
var color = new Vector3(1.0f, 0.82f, 0.15f);
|
|
|
|
|
|
|
|
|
|
Vector3[] corners =
|
2026-06-05 20:47:57 +03:00
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
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),
|
2026-06-05 20:47:57 +03:00
|
|
|
};
|
|
|
|
|
|
2026-06-06 01:24:09 +03:00
|
|
|
foreach (var corner in corners)
|
|
|
|
|
AddVertex(vertices, corner.X, corner.Y, corner.Z, color.X, color.Y, color.Z);
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
uint[] indices =
|
2026-06-05 20:47:57 +03:00
|
|
|
{
|
|
|
|
|
0, 1, 1, 2, 2, 3, 3, 0,
|
|
|
|
|
4, 5, 5, 6, 6, 7, 7, 4,
|
|
|
|
|
0, 4, 1, 5, 2, 6, 3, 7,
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
return CreateIndexedMesh(gl, vertices.ToArray(), indices, PrimitiveType.Lines);
|
2026-06-05 20:47:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GpuMesh CreateAxes(GL gl, float length = 1.0f)
|
|
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
if (length <= 0.0f) throw new ArgumentOutOfRangeException(nameof(length));
|
2026-06-05 20:47:57 +03:00
|
|
|
|
2026-06-06 01:24:09 +03:00
|
|
|
var vertices = new List<float>();
|
|
|
|
|
var indices = new List<uint>();
|
|
|
|
|
|
|
|
|
|
void AddLine(Vector3 a, Vector3 b, Vector3 color)
|
2026-06-05 20:47:57 +03:00
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
var start = (uint)(vertices.Count / FloatsPerVertex);
|
2026-06-06 01:24:09 +03:00
|
|
|
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);
|
|
|
|
|
indices.Add(start);
|
|
|
|
|
indices.Add(start + 1);
|
|
|
|
|
}
|
2026-06-05 20:47:57 +03:00
|
|
|
|
2026-06-06 01:24:09 +03:00
|
|
|
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));
|
2026-06-05 20:47:57 +03:00
|
|
|
|
2026-06-06 01:24:09 +03:00
|
|
|
return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), PrimitiveType.Lines);
|
2026-06-05 20:47:57 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
public static GpuMesh CreateColoredIndexedMesh(GL gl, IReadOnlyList<float> vertices, IReadOnlyList<uint> indices, PrimitiveType primitiveType = PrimitiveType.Triangles, ViewportMaterial? material = null)
|
|
|
|
|
{
|
|
|
|
|
return CreateIndexedMesh(gl, vertices.ToArray(), indices.ToArray(), primitiveType, material);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddCubeFace(List<float> vertices, List<uint> indices, Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3 normal, Vector3 color)
|
|
|
|
|
{
|
|
|
|
|
var start = (uint)(vertices.Count / FloatsPerVertex);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddVertex(List<float> 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, float u = 0.0f, float v = 0.0f)
|
2026-06-05 21:12:25 +03:00
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
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);
|
|
|
|
|
vertices.Add(u); vertices.Add(v);
|
2026-06-05 21:12:25 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
private static GpuMesh CreateIndexedMesh(GL gl, float[] vertices, uint[] indices, PrimitiveType primitiveType, ViewportMaterial? material = null)
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
|
|
|
|
var vao = gl.GenVertexArray();
|
|
|
|
|
var vbo = gl.GenBuffer();
|
|
|
|
|
var ebo = gl.GenBuffer();
|
|
|
|
|
|
|
|
|
|
gl.BindVertexArray(vao);
|
|
|
|
|
|
|
|
|
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
|
|
|
|
fixed (float* vertexPtr = vertices)
|
|
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), vertexPtr, BufferUsageARB.StaticDraw);
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
|
|
|
|
|
fixed (uint* indexPtr = indices)
|
|
|
|
|
{
|
2026-06-06 21:26:56 +03:00
|
|
|
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), indexPtr, BufferUsageARB.StaticDraw);
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
const uint stride = FloatsPerVertex * sizeof(float);
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
gl.EnableVertexAttribArray(0);
|
2026-06-06 21:26:56 +03:00
|
|
|
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, null);
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
gl.EnableVertexAttribArray(1);
|
2026-06-06 21:26:56 +03:00
|
|
|
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float)));
|
|
|
|
|
|
2026-06-06 00:42:07 +03:00
|
|
|
gl.EnableVertexAttribArray(2);
|
2026-06-06 21:26:56 +03:00
|
|
|
gl.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, stride, (void*)(6 * sizeof(float)));
|
|
|
|
|
|
|
|
|
|
gl.EnableVertexAttribArray(3);
|
|
|
|
|
gl.VertexAttribPointer(3, 2, VertexAttribPointerType.Float, false, stride, (void*)(9 * sizeof(float)));
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
gl.BindVertexArray(0);
|
|
|
|
|
|
2026-06-06 21:26:56 +03:00
|
|
|
return new GpuMesh(gl, vao, vbo, ebo, (uint)indices.Length, primitiveType, material);
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
}
|