Moved util files to separate class library

This commit is contained in:
2017-02-26 01:30:27 -05:00
parent 8a68bc6d2d
commit bc584c8f42
15 changed files with 156 additions and 37 deletions

View File

@@ -4,7 +4,10 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using hexworld.Util;
using Diamond;
using Diamond.Buffers;
using Diamond.Shaders;
using Diamond.Textures;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenTK;

View File

@@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
namespace hexworld.Util
{
public abstract class GLObject : IDisposable
{
public uint Id { get; protected set; }
protected GLObject(uint id)
{
Id = id;
}
protected abstract void Delete();
private bool _disposed = false;
public void Dispose()
{
if (_disposed) return;
_disposed = true;
Delete();
GC.SuppressFinalize(this);
}
~GLObject()
{
Dispose();
}
public static explicit operator uint(GLObject o) => o.Id;
public static explicit operator int(GLObject o) => (int) o.Id;
}
}

View File

@@ -1,115 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL4;
namespace hexworld.Util
{
public class Program : GLObject
{
private Dictionary<string, int> uniforms = new Dictionary<string, int>();
private Dictionary<string, int> attributes = new Dictionary<string, int>();
public string Log => GL.GetProgramInfoLog((int) Id);
public Program()
: base((uint) GL.CreateProgram())
{
}
protected override void Delete()
{
GL.DeleteProgram(Id);
}
public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id);
public bool LinkStatus
{
get
{
GL.GetProgram(Id, GetProgramParameterName.LinkStatus, out int success);
return success != 0;
}
}
public bool Link()
{
uniforms.Clear();
attributes.Clear();
GL.LinkProgram(Id);
GL.GetProgram(Id, GetProgramParameterName.LinkStatus, out int success);
if (success == 0) return false;
GL.GetProgram(Id, GetProgramParameterName.ActiveUniforms, out int uniformcount);
for (var i = 0; i < uniformcount; i++)
{
var sb = new StringBuilder(256);
GL.GetActiveUniformName((int) Id, i, sb.Capacity, out int length, sb);
uniforms[sb.ToString()] = i;
}
GL.GetProgram(Id, GetProgramParameterName.ActiveAttributes, out int attributecount);
for (var i = 0; i < attributecount; i++)
{
var sb = new StringBuilder(256);
GL.GetActiveAttrib((int) Id, i, sb.Capacity, out int length, out int size,
out ActiveAttribType type, sb);
attributes[sb.ToString()] = i;
}
return true;
}
public bool TryGetUniform(string name, out int id)
{
return uniforms.TryGetValue(name, out id);
}
public int GetUniform(string name)
{
if (!TryGetUniform(name, out int id))
throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'");
return id;
}
public bool TryGetAttribute(string name, out int id)
{
return attributes.TryGetValue(name, out id);
}
public int GetAttribute(string name)
{
if (!TryGetAttribute(name, out int id))
throw new ShaderException($"Shader Program {Id} does not contain id '{name}'");
return id;
}
public void EnableAllAttribArrays()
{
foreach (var loc in attributes.Values)
GL.EnableVertexAttribArray(loc);
}
public void DisableAllAttribArrays()
{
foreach (var loc in attributes.Values)
GL.DisableVertexAttribArray(loc);
}
public void Use() => GL.UseProgram(Id);
public static void UseDefault() => GL.UseProgram(0);
public static Program FromShaders(params Shader[] shaders)
{
var p = new Program();
foreach (var shader in shaders)
{
p.Attach(shader);
}
p.Link();
return p;
}
}
}

View File

@@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
using System.IO;
namespace hexworld.Util
{
public class Shader : GLObject
{
public readonly ShaderType Type;
public string Source
{
get
{
var sb = new StringBuilder(1024);
GL.GetShaderSource(Id, sb.Capacity, out int length, sb);
return sb.ToString();
}
set { GL.ShaderSource((int) Id, value); }
}
public string Log => GL.GetShaderInfoLog((int) Id);
public bool Compiled
{
get
{
GL.GetShader(Id, ShaderParameter.CompileStatus, out int success);
return success != 0;
}
}
public Shader(ShaderType type)
: base((uint) GL.CreateShader(type))
{
Type = type;
}
protected override void Delete()
{
GL.DeleteShader(Id);
}
public bool Compile()
{
GL.CompileShader(Id);
GL.GetShader(Id, ShaderParameter.CompileStatus, out int success);
return success != 0;
}
public static Shader FromFile(string path, ShaderType type)
{
return FromFile(path, type, out bool success);
}
public static Shader FromFile(string path, ShaderType type, out bool success)
{
var s = new Shader(type);
s.Source = File.ReadAllText(path);
success = s.Compile();
return s;
}
}
}

View File

@@ -1,30 +0,0 @@
using System;
using System.Runtime.Serialization;
namespace hexworld.Util
{
[Serializable]
public class ShaderException : Exception
{
public ShaderException()
{
}
public ShaderException(string message)
: base(message)
{
}
public ShaderException(string message, Exception inner)
: base(message, inner)
{
}
protected ShaderException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
}

View File

@@ -1,77 +0,0 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
namespace hexworld.Util
{
public class Texture : GLObject
{
public TextureTarget Target;
public Texture(TextureTarget target = TextureTarget.Texture2D)
: base((uint) GL.GenTexture())
{
Target = target;
}
protected override void Delete()
{
GL.DeleteTexture(Id);
}
public void Bind()
{
GL.BindTexture(Target, Id);
}
public void Bind(TextureUnit unit)
{
GL.ActiveTexture(unit);
Bind();
}
public void Bind(int unit)
{
GL.ActiveTexture(TextureUnit.Texture0 + unit);
Bind();
}
public void Unbind()
{
GL.BindTexture(Target, 0);
}
public void Unbind(TextureUnit unit)
{
GL.ActiveTexture(unit);
Unbind();
}
public void Unbind(int unit)
{
GL.ActiveTexture(TextureUnit.Texture0 + unit);
Unbind();
}
public static Texture FromBitmap(Bitmap bmp)
{
var tex = new Texture(TextureTarget.Texture2D);
tex.Bind();
GL.TexParameter(tex.Target, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Nearest);
GL.TexParameter(tex.Target, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Nearest);
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(tex.Target, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, PixelFormat.Bgra,
PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);
tex.Unbind();
return tex;
}
}
}

View File

@@ -1,81 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL4;
namespace hexworld.Util
{
public static class VBO
{
public static void Unbind()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
}
public class VBO<T> : GLObject where T : struct
{
public VBO()
: base((uint) GL.GenBuffer())
{
}
protected override void Delete()
{
GL.DeleteBuffer(Id);
}
public void Bind()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, Id);
}
public void Data(T[] data, BufferUsageHint usage = BufferUsageHint.StaticDraw)
{
Bind();
var size = Marshal.SizeOf(typeof(T));
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (size * data.Length), data, usage);
VBO.Unbind();
}
private static readonly int Stride;
private static readonly VertexPointerAttribute[] Attributes;
static VBO()
{
var attribList = new List<VertexPointerAttribute>();
Stride = Marshal.SizeOf(typeof(T));
foreach (var fieldInfo in typeof(T).GetFields())
{
var attrs = fieldInfo.GetCustomAttributes(typeof(VertexPointerAttribute), false);
if (attrs.Length == 0) continue;
var offset = (int) Marshal.OffsetOf(typeof(T), fieldInfo.Name);
foreach (var attr in attrs)
{
var vpa = (VertexPointerAttribute) attr;
vpa.Offset = offset;
attribList.Add(vpa);
}
}
Attributes = attribList.ToArray();
}
public void AttribPointers(Program pgm)
{
Bind();
foreach (var attr in Attributes)
{
if (!pgm.TryGetAttribute(attr.Name, out int loc)) continue;
GL.VertexAttribPointer(loc, attr.Size, attr.Type, attr.Normalized, Stride, attr.Offset);
GL.VertexAttribDivisor(loc, attr.Divisor);
}
VBO.Unbind();
}
}
}

View File

@@ -1,22 +0,0 @@
using System;
using OpenTK.Graphics.OpenGL4;
namespace hexworld.Util
{
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class VertexPointerAttribute : Attribute
{
public string Name { get; }
public int Size { get; }
public VertexAttribPointerType Type { get; set; } = VertexAttribPointerType.Float;
public bool Normalized { get; set; } = false;
public int Divisor { get; set; } = 0;
public int Offset { get; set; } = 0;
public VertexPointerAttribute(string name, int size)
{
Name = name;
Size = size;
}
}
}

View File

@@ -4,7 +4,8 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using hexworld.Util;
using Diamond;
using Diamond.Buffers;
using Newtonsoft.Json;
using OpenTK;

View File

@@ -55,13 +55,6 @@
<Compile Include="Driver.cs" />
<Compile Include="HexRender.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\GLObject.cs" />
<Compile Include="Util\Program.cs" />
<Compile Include="Util\Shader.cs" />
<Compile Include="Util\ShaderException.cs" />
<Compile Include="Util\Texture.cs" />
<Compile Include="Util\VBO.cs" />
<Compile Include="Util\VertexPointerAttribute.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
@@ -88,5 +81,11 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Diamond\Diamond.csproj">
<Project>{59761eb7-87e1-4ff1-808a-18b7bfff602e}</Project>
<Name>Diamond</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>