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;
|
|
|
|
|
|
2026-06-12 02:06:32 +03:00
|
|
|
public sealed class GpuMesh : IDisposable
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
|
|
|
|
private readonly GL _gl;
|
2026-06-12 02:06:32 +03:00
|
|
|
private bool _disposed;
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
public uint VertexArrayObject { get; }
|
|
|
|
|
public uint VertexBufferObject { get; }
|
|
|
|
|
public uint ElementBufferObject { get; }
|
|
|
|
|
public uint IndexCount { get; }
|
|
|
|
|
public PrimitiveType PrimitiveType { get; }
|
2026-06-06 21:26:56 +03:00
|
|
|
public ViewportMaterial Material { get; }
|
2026-06-05 20:33:21 +03:00
|
|
|
|
|
|
|
|
public GpuMesh(
|
|
|
|
|
GL gl,
|
|
|
|
|
uint vertexArrayObject,
|
|
|
|
|
uint vertexBufferObject,
|
|
|
|
|
uint elementBufferObject,
|
|
|
|
|
uint indexCount,
|
2026-06-06 21:26:56 +03:00
|
|
|
PrimitiveType primitiveType = PrimitiveType.Triangles,
|
|
|
|
|
ViewportMaterial? material = null)
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
|
|
|
|
_gl = gl;
|
|
|
|
|
VertexArrayObject = vertexArrayObject;
|
|
|
|
|
VertexBufferObject = vertexBufferObject;
|
|
|
|
|
ElementBufferObject = elementBufferObject;
|
|
|
|
|
IndexCount = indexCount;
|
|
|
|
|
PrimitiveType = primitiveType;
|
2026-06-06 21:26:56 +03:00
|
|
|
Material = material ?? ViewportMaterial.Untextured;
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-05 20:47:57 +03:00
|
|
|
public unsafe void Draw()
|
2026-06-05 20:33:21 +03:00
|
|
|
{
|
2026-06-12 02:06:32 +03:00
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
2026-06-05 20:33:21 +03:00
|
|
|
_gl.BindVertexArray(VertexArrayObject);
|
|
|
|
|
_gl.DrawElements(PrimitiveType, IndexCount, DrawElementsType.UnsignedInt, null);
|
|
|
|
|
}
|
2026-06-12 02:06:32 +03:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_gl.DeleteBuffer(ElementBufferObject);
|
|
|
|
|
_gl.DeleteBuffer(VertexBufferObject);
|
|
|
|
|
_gl.DeleteVertexArray(VertexArrayObject);
|
|
|
|
|
_disposed = true;
|
|
|
|
|
}
|
2026-06-05 20:33:21 +03:00
|
|
|
}
|