More OO-like. a bit easier to read/use things
This commit is contained in:
63
hexworld/Util/Program.cs
Normal file
63
hexworld/Util/Program.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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())
|
||||
{
|
||||
}
|
||||
|
||||
public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id);
|
||||
|
||||
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 int GetUniform(string name)
|
||||
{
|
||||
if (!uniforms.TryGetValue(name, out int id))
|
||||
throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'");
|
||||
else return id;
|
||||
}
|
||||
|
||||
public void Use() => GL.UseProgram(Id);
|
||||
|
||||
public static void UseDefault() => GL.UseProgram(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user