Shader loader
This commit is contained in:
@@ -16,9 +16,11 @@ namespace hexworld
|
||||
public partial class HexRender : GameWindow
|
||||
{
|
||||
private Program pgm;
|
||||
|
||||
// todo: generate texture atlas
|
||||
// or at least embed sub-uvs and materials into json
|
||||
private Texture grass;
|
||||
|
||||
private Texture stone;
|
||||
|
||||
private Matrix4 view;
|
||||
@@ -67,25 +69,32 @@ namespace hexworld
|
||||
tileVbo = new VBO<Tile>();
|
||||
tileVbo.Data(tiles, BufferUsageHint.DynamicDraw);
|
||||
|
||||
var vs = new Shader(ShaderType.VertexShader)
|
||||
{
|
||||
Source = File.ReadAllText("s.vs.glsl")
|
||||
};
|
||||
vs.Compile();
|
||||
Console.Out.WriteLine(vs.Log);
|
||||
var vs = Shader.FromFile("s.vs.glsl", ShaderType.VertexShader);
|
||||
var fs = Shader.FromFile("s.fs.glsl", ShaderType.FragmentShader);
|
||||
pgm = Program.FromShaders(vs, fs);
|
||||
|
||||
var fs = new Shader(ShaderType.FragmentShader)
|
||||
if (!vs.Compiled | !fs.Compiled)
|
||||
{
|
||||
Source = File.ReadAllText("s.fs.glsl")
|
||||
};
|
||||
fs.Compile();
|
||||
Console.Out.WriteLine(fs.Log);
|
||||
Console.Out.WriteLine("Failed to compile shaders:");
|
||||
if (!vs.Compiled)
|
||||
Console.Out.WriteLine("Vertex Shader:\n" + vs.Log.Trim());
|
||||
if (!fs.Compiled)
|
||||
Console.Out.WriteLine("Fragment Shader:\n" + fs.Log.Trim());
|
||||
Console.Out.WriteLine("Press any key to exit.");
|
||||
Console.ReadKey();
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
pgm = new Program();
|
||||
pgm.Attach(vs);
|
||||
pgm.Attach(fs);
|
||||
pgm.Link();
|
||||
Console.Out.WriteLine(pgm.Log);
|
||||
if (!pgm.LinkStatus)
|
||||
{
|
||||
Console.Out.WriteLine("Failed to link program:");
|
||||
Console.Out.WriteLine(pgm.Log.Trim());
|
||||
Console.Out.WriteLine("Press any key to exit.");
|
||||
Console.ReadKey();
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
cubeVbo.AttribPointers(pgm);
|
||||
tileVbo.AttribPointers(pgm);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -55,7 +55,6 @@
|
||||
<Compile Include="Driver.cs" />
|
||||
<Compile Include="HexRender.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Util\Extensions.cs" />
|
||||
<Compile Include="Util\GLObject.cs" />
|
||||
<Compile Include="Util\Program.cs" />
|
||||
<Compile Include="Util\Shader.cs" />
|
||||
|
||||
Reference in New Issue
Block a user