diff --git a/Diamond/Buffers/GLBuffer.cs b/Diamond/Buffers/GLBuffer.cs index fc69e83..0c49a99 100644 --- a/Diamond/Buffers/GLBuffer.cs +++ b/Diamond/Buffers/GLBuffer.cs @@ -39,6 +39,7 @@ namespace Diamond.Buffers public class GLBuffer : GLObject where T : struct { private readonly GLBufferWrapper _buffer; + private readonly VertexDataInfo _vdi; internal override GLWrapper Wrapper => _buffer; private readonly int _size; @@ -56,6 +57,7 @@ namespace Diamond.Buffers _buffer = buffer; Name = name; _size = Marshal.SizeOf(); + _vdi = VertexDataInfo.GetInfo(); } public void Data(T[] data) => _buffer.Data(_size, data); @@ -64,6 +66,24 @@ namespace Diamond.Buffers public void Data(SubArray data) => Data(data.Offset, data.Length, data.Array); + public void PointTo(Program program) + { + if (_vdi == null) + { + var exception = new InvalidOperationException($"Cannot use type {typeof(T)} to create a VertexBuffer"); + GLBuffer.Logger.Error(exception); + throw exception; + } + + _buffer.Bind(); + foreach (var attr in _vdi.Pointers) + { + var loc = program.AttributeLocation(attr.Name); + if (loc.HasValue) + GL.VertexAttribPointer((int) loc, attr.Size, attr.Type, attr.Normalized, _vdi.Stride, attr.Offset); + } + } + public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} {Name} ({Id})"; } @@ -93,59 +113,4 @@ namespace Diamond.Buffers return service; } } - - public class VertexBuffer : GLBuffer where T : struct - { - private readonly VertexDataInfo _vdi; - private readonly GLBufferWrapper _buffer; - - internal VertexBuffer(GLBufferWrapper buffer, string name) - : base(buffer, name) - { - _vdi = VertexDataInfo.GetInfo(); - _buffer = buffer; - } - - public void PointTo(Program program) - { - _buffer.Bind(); - foreach (var attr in _vdi.Pointers) - { - var loc = program.AttributeLocation(attr.Name); - if (loc.HasValue) - GL.VertexAttribPointer((int) loc, attr.Size, attr.Type, attr.Normalized, _vdi.Stride, attr.Offset); - } - } - } - - public static class VertexBuffer - { - public static VertexBuffer Empty(BufferTarget target, BufferUsageHint usage = BufferUsageHint.StaticDraw, - string name = null) where T : struct - { - if (typeof(T).GetCustomAttributes(typeof(VertexDataAttribute), false).Length == 0) - { - GLBuffer.Logger.Warn("Cannot use type {0} to create a VertexBuffer", typeof(T)); - return null; - } - - var wrapper = new GLBufferWrapper(target, usage); - var service = new VertexBuffer(wrapper, name); - - GLBuffer.Logger.Debug("Created {0}", service); - - return service; - } - - public static VertexBuffer FromData(T[] data, BufferTarget target, - BufferUsageHint usage = BufferUsageHint.StaticDraw, - string name = null) where T : struct - { - var service = Empty(target, usage, name); - - service?.Data(data); - - return service; - } - } } \ No newline at end of file diff --git a/Diamond/Buffers/VertexPointerAttribute.cs b/Diamond/Buffers/VertexPointerAttribute.cs index a121912..53b7dd3 100644 --- a/Diamond/Buffers/VertexPointerAttribute.cs +++ b/Diamond/Buffers/VertexPointerAttribute.cs @@ -65,29 +65,29 @@ namespace Diamond.Buffers public void EnableVertexPointers() { if (Program.Current == null) - { - throw new Exception("Cant render a mesh with no active shader."); // todo make an exception here. - } + throw new InvalidOperationException("Cant render a mesh with no active shader."); foreach (var attr in Pointers) { - if (!Program.Current.TryGetAttribute(attr.Name, out int loc)) continue; - GL.EnableVertexAttribArray(loc); - GL.VertexAttribDivisor(loc, Divisor); + var loc = Program.Current.AttributeLocation(attr.Name); + if (!loc.HasValue) + continue; + GL.EnableVertexAttribArray((int) loc); + GL.VertexAttribDivisor((int) loc, Divisor); } } public void DisableVertexPointers() { if (Program.Current == null) - { - throw new Exception("Cant render a mesh with no active shader."); // todo make an exception here. - } + throw new InvalidOperationException("Cant render a mesh with no active shader."); foreach (var attr in Pointers) { - if (!Program.Current.TryGetAttribute(attr.Name, out int loc)) continue; - GL.DisableVertexAttribArray(loc); + var loc = Program.Current.AttributeLocation(attr.Name); + if (!loc.HasValue) + continue; + GL.DisableVertexAttribArray((int) loc); } } @@ -101,9 +101,7 @@ namespace Diamond.Buffers var vertexDataAttributes = typeof(T).GetCustomAttributes(typeof(VertexDataAttribute), false); if (vertexDataAttributes.Length != 1) - { - throw new Exception("Can't use type {typeof(T)} as mesh data."); // todo make an exception here. - } + return null; var vertdataattrib = (VertexDataAttribute) vertexDataAttributes[0]; var divisor = vertdataattrib.Divisor; diff --git a/Diamond/Mesh.cs b/Diamond/Mesh.cs index bfe8230..d67c315 100644 --- a/Diamond/Mesh.cs +++ b/Diamond/Mesh.cs @@ -18,7 +18,6 @@ namespace Diamond public string Name { get; set; } private static readonly VertexDataInfo tVdi; - private static List attribs; static Mesh() {