Shader loader
This commit is contained in:
@@ -1,24 +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 Extensions
|
||||
{
|
||||
public static byte[] ToBytes<T>(this T[] arr) where T : struct
|
||||
{
|
||||
var size = Marshal.SizeOf<T>()*arr.Length;
|
||||
var bytes = new byte[size];
|
||||
var ptr = Marshal.AllocHGlobal(size);
|
||||
Marshal.StructureToPtr(arr, ptr, false);
|
||||
Marshal.Copy(ptr, bytes, 0, size);
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,15 @@ namespace hexworld.Util
|
||||
|
||||
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();
|
||||
@@ -86,5 +95,16 @@ namespace hexworld.Util
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using System.IO;
|
||||
|
||||
namespace hexworld.Util
|
||||
{
|
||||
@@ -23,6 +24,15 @@ namespace hexworld.Util
|
||||
|
||||
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))
|
||||
{
|
||||
@@ -35,5 +45,18 @@ namespace hexworld.Util
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ namespace hexworld.Util
|
||||
public void Data(T[] data, BufferUsageHint usage = BufferUsageHint.StaticDraw)
|
||||
{
|
||||
Bind();
|
||||
var size = Marshal.SizeOf<T>();
|
||||
var size = Marshal.SizeOf(typeof(T));
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (size * data.Length), data, usage);
|
||||
VBO.Unbind();
|
||||
}
|
||||
@@ -43,13 +43,14 @@ namespace hexworld.Util
|
||||
static VBO()
|
||||
{
|
||||
var attribList = new List<VertexPointerAttribute>();
|
||||
Stride = Marshal.SizeOf<T>();
|
||||
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<T>(fieldInfo.Name);
|
||||
|
||||
var offset = (int) Marshal.OffsetOf(typeof(T), fieldInfo.Name);
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
var vpa = (VertexPointerAttribute) attr;
|
||||
|
||||
Reference in New Issue
Block a user