Migrate to Factory/Wrapper - Diamond complete
This commit is contained in:
@@ -39,6 +39,7 @@ namespace Diamond.Buffers
|
||||
public class GLBuffer<T> : 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<T>();
|
||||
_vdi = VertexDataInfo.GetInfo<T>();
|
||||
}
|
||||
|
||||
public void Data(T[] data) => _buffer.Data(_size, data);
|
||||
@@ -64,6 +66,24 @@ namespace Diamond.Buffers
|
||||
|
||||
public void Data(SubArray<T> 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<T> : GLBuffer<T> where T : struct
|
||||
{
|
||||
private readonly VertexDataInfo _vdi;
|
||||
private readonly GLBufferWrapper _buffer;
|
||||
|
||||
internal VertexBuffer(GLBufferWrapper buffer, string name)
|
||||
: base(buffer, name)
|
||||
{
|
||||
_vdi = VertexDataInfo.GetInfo<T>();
|
||||
_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<T> Empty<T>(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<T>(wrapper, name);
|
||||
|
||||
GLBuffer.Logger.Debug("Created {0}", service);
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
public static VertexBuffer<T> FromData<T>(T[] data, BufferTarget target,
|
||||
BufferUsageHint usage = BufferUsageHint.StaticDraw,
|
||||
string name = null) where T : struct
|
||||
{
|
||||
var service = Empty<T>(target, usage, name);
|
||||
|
||||
service?.Data(data);
|
||||
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -18,7 +18,6 @@ namespace Diamond
|
||||
public string Name { get; set; }
|
||||
|
||||
private static readonly VertexDataInfo tVdi;
|
||||
private static List<VertexPointerAttribute> attribs;
|
||||
|
||||
static Mesh()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user