add platformer window infrastructure
This commit is contained in:
20
Platformer/Util/Buffer.cs
Normal file
20
Platformer/Util/Buffer.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Platformer.Util
|
||||
{
|
||||
public class Buffer : GlObj
|
||||
{
|
||||
public Buffer() : base(GL.GenBuffer())
|
||||
{
|
||||
}
|
||||
|
||||
public void SetData<T>(T[] data, BufferUsageHint usage = BufferUsageHint.StaticDraw) where T : struct
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data.Length * Marshal.SizeOf(typeof(T))), data, usage);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Platformer/Util/GlObj.cs
Normal file
14
Platformer/Util/GlObj.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Platformer.Util
|
||||
{
|
||||
public abstract class GlObj
|
||||
{
|
||||
public readonly int Id;
|
||||
|
||||
public GlObj(int id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
public static implicit operator int(GlObj o) => o.Id;
|
||||
}
|
||||
}
|
||||
20
Platformer/Util/Program.cs
Normal file
20
Platformer/Util/Program.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Platformer.Util
|
||||
{
|
||||
public class Program : GlObj
|
||||
{
|
||||
public Program() : base(GL.CreateProgram())
|
||||
{
|
||||
}
|
||||
|
||||
public static Program Link(params Shader[] shaders)
|
||||
{
|
||||
var p = new Program();
|
||||
foreach (var s in shaders)
|
||||
GL.AttachShader(p, s);
|
||||
GL.LinkProgram(p);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Platformer/Util/Shader.cs
Normal file
40
Platformer/Util/Shader.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Platformer.Util
|
||||
{
|
||||
public class Shader : GlObj
|
||||
{
|
||||
private static Dictionary<string, ShaderType> _shaderTypes = new Dictionary<string, ShaderType>()
|
||||
{
|
||||
[".vert"] = ShaderType.VertexShader,
|
||||
[".frag"] = ShaderType.FragmentShader,
|
||||
[".geom"] = ShaderType.GeometryShader,
|
||||
[".comp"] = ShaderType.ComputeShader,
|
||||
};
|
||||
|
||||
public Shader(ShaderType type) : base(GL.CreateShader(type))
|
||||
{
|
||||
}
|
||||
|
||||
public static Shader Compile(ShaderType type, string source)
|
||||
{
|
||||
var s = new Shader(type);
|
||||
GL.ShaderSource(s, source);
|
||||
GL.CompileShader(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
public static Shader Compile(string filename)
|
||||
{
|
||||
var ext = Path.GetExtension(filename);
|
||||
|
||||
if (!_shaderTypes.ContainsKey(ext))
|
||||
throw new InvalidOperationException($"Can't infer shader type for {filename}");
|
||||
|
||||
return Compile(_shaderTypes[ext], File.ReadAllText(filename));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Platformer/Util/VertexArray.cs
Normal file
26
Platformer/Util/VertexArray.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Platformer.Util
|
||||
{
|
||||
public class VertexArray : GlObj
|
||||
{
|
||||
public VertexArray() : base(GL.GenVertexArray())
|
||||
{
|
||||
}
|
||||
|
||||
public void VertexPointer(Buffer buffer, int index, int size,
|
||||
VertexAttribPointerType type = VertexAttribPointerType.Float,
|
||||
bool normalized = false, int stride = 0, int offset = 0)
|
||||
{
|
||||
GL.BindVertexArray(this);
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
|
||||
GL.VertexAttribPointer(index, size, type, normalized, stride, offset);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
|
||||
|
||||
GL.EnableVertexArrayAttrib(this, index);
|
||||
|
||||
GL.BindVertexArray(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user